CS 1401, Exam #3, 9:30 am version

Date: Friday, November 9, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On November 9, 1921, Albert Einstein, one of the greatest scientists of the 20 century, was awarded the Nobel prize. One of his most well known achievements is his famous formula E = mc^2 which describes the total energy E of a body with mass m; here, c is the speed of light (approximately 300,000 km/s = 3*10^8 m/s). If particular, in metric (SI) units, if you take m in kg and c is m/s, then you get the energy in joules.

1a. Write a method named energy for computing the value mc^2 for two given real numbers m and c.

1b. Call (invoke) your method energy in the main method to compute the energy of a mass m = 1 kg. Assume that c = 3*10^8 m/s. (You do not need to write the entire main method, just the part that assigns values to m and c and calls your method.)

1c. Trace, step by step, how the computer will perform the needed computations, and check that the result is indeed correct (should be 9*10^16 joules).



































2a. Define a class Bodies whose elements are bodies; each body is characterized by its mass (in kg) and the speed of light (in m/s). Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the total energy (in joules).

2b. Use your class in the main method to define a new object "textbook" of type Bodies with mass 1 kg and speed of light 3*10^8 m/s. Compute and print this body's energy; then, make this same body twice heavier, and compute and print the new value of energy.

2c. Trace your program step-by-step.

















































3a. Write a piece of code that, given an array m of masses of different bodies and the value c of the speed of light, computes and prints the energy of different bodies.

3b. To check the correctness of the code you wrote in Part 3a, write a piece of code that defines a new array m consisting of 2 elements of type double and assigns, to elements of this array, values 1.0 and 0.0.

3c. Trace step-by-step how the piece of code you wrote in Part 3a will compute the corresponding energies. Hint: the energies are equal to 9*10^16 and 0 joules.


















































4a. Write a method that, given an array m of masses, finds the mass of the heaviest body.

4b. To check the correctness of your method, write a piece of code that defines a new array textbooks consisting of 3 elements of type double and assigns, to elements of this array, values 1.0, 0.5, and 2.0.

4c. Trace step-by-step how the piece of code you wrote in Part 4a will compute the largest element of this array. Hint: the desired largest element is element No. 2, its mass is max(1.0, 0.5, 2.0) = 2.0.




















































5. Name main GUI components, and briefly explain their use.