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).


Solution:

public double energy(double mass, double speedOfLight){
  return mass * speedOfLight * speedOfLight;}

in the method main:

double m = 1.0;
double c = 3.0E8;
double e = energy(m, c);

Tracing:

first,

 ---------    ---------
|   1.0  |   |  3.0E8  |
 ---------    ---------
    m             c

 ----------------------------
|  ---------    ----------   |
| |  1.0    |  |   3.0E8  |  |
|  ---------    ----------   |
|   mass       speedOfLight  |
|----------------------------|
|                            |
|                            |
|                            |
|                            |
 ----------------------------
          energy

 ---------
|  9.0E16 |
 ---------
    e

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.

Solution:

public class Bodies{
  private double mass;
  private double speedOfLight;

  public Bodies(double amass, double aspeedOfLight){
    mass = amass, speedOfLight = aspeedOfLight;}

  public double getMass(){return mass;}

  public double getSpeedOfLight(){return speedOfLight;}

  public void setMass(double newMass){mass = newMass;}

  public void setSpeedOfLight(double newSpeedOfLight)
    {speedOfLight = newSpeedOfLight;}

  public double energy(){return mass * speedOfLight * speedOfLight;}
}

In the main method:

Bodies textbook = new Bodies(1.0, 3.0E8);
double e = textbook.energy();
System.out.println(e);

double m = textbook.getMass();
textbook.setMass(2.0 * m);
e = textbook.energy();
System.out.println(e);

Tracing:

on the first line, we call a constructor method:

 ---------------
|  ----------   |
| |  1.0    |   |
|  ----------   |
|    amass      |
|  ----------   |
| |  3.0E8   |  |
|  ----------   |
| aspeedOfLight |
 ---------------
|               |
|               |
|               |
|               |
|               |
|               |
 ---------------
    Bodies

after the first line, we get a new object

 ---------------
|  ----------   |
| |  1.0    |   |
|  ----------   |
|     mass      |
|  ----------   |
| |  3.0E8   |  |
|  ----------   |
|  speedOfLight |
 ---------------
   textbook

after the second line, we get

 ----------
|  3.0E8   |
 ----------
     e

at the third line, we print 3.0E8 to the screen

then, we compute the value

 ----------
|   2.0   |
 ----------
     m

and change the object to:

 ---------------
|  ----------   |
| |  2.0    |   |
|  ----------   |
|     mass      |
|  ----------   |
| |  3.0E8   |  |
|  ----------   |
|  speedOfLight |
 ---------------
   textbook


after that, we change the value of the variable e:

 ----------
| 18.0E16  |
 ----------
    e

and we print 18.0E16 to the screen


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.



Solution:

for (int i = 0; i < m.length; i++)
  {System.out.println(m[i] * c * c);}

Testing:

double[] m = {1.0, 0.0};

Tracing:

The fact that we have defined an array m with 2 elements means, in
effect, that we have defined two variables m[0] and m[1]:

 ----
|  \|
 ---\
  m  \
      \|
      - -------
       |  1.0 |  m[0]
        -------
       |  0.0 |  m[1]
        -------

Next, we get to the for-loop. First, we define a new integer
variable i and assign to it the value 0:

 -------
|   0   |
 -------
   i

We check the condition: the length of our array m is 2, so i is
smaller than this length. Thus, the condition i < m.length is
satisfied, and we implement the operation in the body of the loop.
Since i = 0, we thus perform the operation

  System.out.println(m[0] * c * c);

In other words, we print the value 1.0 * 3.0E8 * 3.0E8 = 9.0E16.

Then, we go to the update statement i++; this statement adds 1 to i,
so we get:

 -------
|   1   |
 -------
    i

The condition i < m.length is still satisfied, so we again go inside
the loop. This time, i is 1, so the statement in the body of the
loop becomes

  System.out.println(m[1] * c * c);

Thus, we print the value 0.0 * 3.0E8 * 3.0E8 = 0.0.

Then, we go to the update statement i++; this statement adds 1 to i,
so we get:

 -------
|   2   |
 -------
    i

The condition i < m.length is no longer satisfied, so we exit the
loop.

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.


public double findHeaviest(double[] m){
  double heaviest = m[0];
  for(int i = 1; i < m.length; i++)
    {if (m[i] > heaviest) heaviest = m[i];}
  return heaviest;
}

in the main method:

double[] textbooks = {1.0, 0.5, 2.0};
double heavy = findHeaviest(textbooks);

tracing:

First, we define an array

 ---------
|        \|
 ---------\
 textbooks \
            \|
            --------
            |  1.0 |  textbooks[0]
             -------
            |  0.5 |  textbooks[1]
             -------
            |  2.0 |  textbooks[2]
             -------

Then, we call a method:

 ---------------------------
|    ----                   |
|   |  \|                   |
|    ---\                   |
|     m  \                  |
|         \|                |
|         - -------         |
|          |  1.0 |  m[0]   |
|           -------         |
|          |  0.5 |  m[1]   |
|           -------         |
|          |  2.0 |  m[2]   |
|           -------         |
|---------------------------|
|                           |
|          ...              |
 ---------------------------
      findHeaviest

Within this method, we define a new variable heaviest and assign
it a value m[0] = 1.0:

 ---------------------
|        1.0          |
 ---------------------
    heaviest

Then, we start a loop by defining a new variable i and assigning it
a value 1:

 ---------------------      ---------
|       1.0           |    |    1    |
 ---------------------      ---------
     heaviest                   i

Since i = 1 < m.length = 3, we go inside the loop. Since i = 1, the
condition 0.5 = m[1] > heaviest = 1.0 is not satisfied, so we do not
do anything. Next, we increase i by 1:

 ---------------------      ---------
|       1.0           |    |    2    |
 ---------------------      ---------
     heaviest                   i

Since i = 2 < m.length = 3, we go inside the loop. Since i = 2, the
condition 2.0 = m[2] > heaviest = 1.0 is satisfied, so we assign,
to the variable heaviest, a new value m[2] = 2.0:

 ---------------------      ---------
|       2.0           |    |    2    |
 ---------------------      ---------
     heaviest                  i

Next, we increase i by 1:

 ---------------------      ---------
|       2.0           |    |    3    |
 ---------------------      ---------
     heaviest                  i

Now, 3 = i is not smaller than m.length = 3, so we exit the loop, and
return the value heaviest = 2.0 to the line

double heavy = findHeaviest(textbooks);

according to which the value 2.0 is assigned to the new variable heavy:

 -------------
|     2.0     |
 -------------
    heavy

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

Windows create actual windows on the screen

Labels indicate the meaning of different parts of the window

Text area is a place where the user types in the input and/or the
program  places the output

A button is what a user pressed to initiate a new process, like
"Calculate" or "Exit"