CS 1401, Quiz 4, MW version, based on Test 1

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

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.

(Turn over, please)

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.