CS 1401, Exam #1, TR version

Date: Tuesday, September 23, 2014
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. On September 23, 1846, astronomers discovered a new planet of Neptune. This was one of the first triumphs of computation: by using Newton's equations, scientists predicted where the new planet could be, and observations confirmed that there is indeed a planet at this location. The corresponding computations actively used logarithms tables.

2. For each of the following sequences of symbols, describe which can be valid Java identifiers and which cannot be; if you believe they cannot be, briefly explain why (e.g., "is a reserved word" or "does not start with a letter"):

3. The following formula enables us to compute the gravitational force F caused by a planet of mass M on a planet of mass m at a distance r: F = GMm/r2. Assuming that G, M, m, and r are already placed in the corresponding variables of type double, write a Java code statement for assigning the corresponding value to the variable F of type double. Explain, step-by-step, which arithmetic operations will be performed first, which next, etc., and trace the computations on the toy example when G = 2.0, M = 3.0, m = 1.0, and r = 3.0. Explain what happens if you simply write GMm in your Java code.

4-5. Nowadays, we can simply use computers to perform multiplication. Write the main method which asks the user for his/her name, asks for the numbers that he/she needs to multiply, and then computes and prints the result. For example, if Bill Gates wants to multiply 3.5 by 2.0, your program should print the following message:
From: Computer
To: Bill Gates

Here is the result of your multiplication:
3.5 X 2.0 = 7.0.

Reminder: to read from the keyboard, you can define the reader as follows:

Scanner reader = new Scanner(System.in);
the header of the main method is:
public static void main(String[] args){

6. Suppose that in your computations, lengths are originally given in meters, and you need to change the units to centimeters. Since 1 m = 100 cm, we need multiply the length in inches by 100. Suppose that the length is stored in the integer variable length. Which of the two lines of code leads to a correct increase: If originally, before each of these two lines, we had length of 2 m, explain what will happen after each of these lines is implemented by Java. What is a clearer way (different from those above) to multiply the variable length by 100?

7. Neptune is one of the largest planets in the Solar system, it is actually third largest by mass. Write a piece of code that decides which of the three given planets is the largest by mass. The names of three planets are stored in the variables pla1, pla2, and pla3, and the masses of these planets are stored in the variables mass1, mass2, and mass3. Use if-then statements to write down a piece of Java code that prints the name of the largest of the three planets.

Comment: There is no need to read anything, assume that all six variables have already been assigned values.

8. A celestial body is called a planet if it is large enough and close enough to the corresponding star. Write down a Java statement that uses the known truth values largeEnough and closeEnough to assign, to a boolean variable is_aPlanet, true or false depending on whether it is a planet or not. Draw the truth tables for "and", "or", and "not". Use these truth tables to find the truth value for Pluto, which is close enough, but not large enough.