## CS 1401, Exam #3

Date: Friday, November 3, 2006, 9:30 am
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1a. Write a method named volume for computing the product of three given real numbers h, w, and l.

1b. Call (invoke) your method volume in the main method to compute the product of the three numbers h = 2.0, w = 2.0, and l = 2.0. (You do not need to write the entire main method, just the part that assigns values to h, w, and l, 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.

```
Solution:

public double volume(double height, double width, double length){
return height * width * length;}

in the method main:

double h = 2.0;
double w = 2.0;
double l = 2.0;
double v = volume(h, w, l);

Tracing:

first,

---------    ---------    ---------
|   2.0   |  |   2.0   |  |   2.0   |
---------    ---------    ---------
h             w            l

--------------------------------------
|  --------    ---------    ---------  |
| |   3.0  |  |   5.0   |  |   2.0   | |
|  --------    ---------    ---------  |
|   height       width        length   |
|--------------------------------------|
|                                      |
|                                      |
|                                      |
|                                      |
--------------------------------------
volume

---------
|   8.0   |
---------
v

```
2a. Define a class Box whose elements are box-shaped creatures; each creature is characterized by its name, its height, its width, and its length. Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the volume of the box-shaped creature.

2b. Use your class in the main method to define a new creature "Cuby" of type Box with height = width = length = 1.0. Compute and print Cuby's volume; then, simulate how Cuby grows: replace all the parameters with the larger value 2.0, and compute and print the new volume.

```Solution:

public class Box{
private String name;
private double height;
private double width;
private double length;

public Circle(String aname, double aheight, double awidth; double alength){
name = aname, height = aheight; width = awidth; length = alength;}

public String getName(){return name;}

public double getHeight(){return height;}

public double getWidth(){return width;}

public double getLength(){return length;}

public void setName(String newName){name = newName;}

public void setHeight(double newHeight){height = newHeight;}

public void setWidth(double newWidth){width = newWidth;}

public void setLength(double newLength){length = newLength;}

public double volume(){return height * width * length;}
}

In the main method:

Box cuby = new Box("Cuby", 1.0, 1.0, 1.0);
double v = cuby.volume();
System.out.println(v);
cuby.setHeight(2.0);
cuby.setWidth(2.0);
cuby.setLength(2.0);
v = cuby.volume();
System.out.println(v);

Tracing:

on the first line, we call a constructor method:

--------------
|  ----------  |
| |  Cuby    | |
|  ----------  |
|    aname     |
|  ----------  |
| |   1.0    | |
|  ----------  |
|   aheight    |
|  ----------  |
| |   1.0    | |
|  ----------  |
|    awidth    |
|  ----------  |
| |   1.0    | |
|  ----------  |
|   alength    |
--------------
|              |
|              |
|              |
|              |
|              |
|              |
|              |
|              |
--------------
Box

after the first line, we get a new object

--------------
|  ----------  |
| |  Cuby   | |
|  ----------  |
|    name      |
|  ----------  |
| |   1.0    | |
|  ----------  |
|    height    |
|  ----------  |
| |   1.0    | |
|  ----------  |
|    width     |
|  ----------  |
| |   1.0    | |
|  ----------  |
|    length    |
--------------
cuby

after the second line, we get

----------
|   1.0    |
----------
v

at the third line, we print 1.0 to the screen

after the fourth through sixth lines, the object changes to:

--------------
|  ----------  |
| |  Cuby   | |
|  ----------  |
|    name      |
|  ----------  |
| |   2.0    | |
|  ----------  |
|    height    |
|  ----------  |
| |   2.0    | |
|  ----------  |
|    width     |
|  ----------  |
| |   2.0    | |
|  ----------  |
|    length    |
--------------
cuby

after the seventh line, we change the value of the variable v:

----------
|   8.0    |
----------
v

on the eighth line, we print 8.0 to the screen

```
3a. Write a piece of code that, given an array h of integers, computes the average of all the elements in this array.

3b. To check the correctness of the code you wrote in Part 3a, write a piece of code that defines a new array h consisting of 3 elements of type integer and assigns, to elements of this array, values 1, 3, and 0. Trace step-by-step how the piece of code you wrote in Part 3a will compute the average value of this array. Hint: the desired average is equal to (1 + 3 + 0) / 3 = 1.333...

```Solution:

int sum = 0;
for (int i = 0; i < h.length; i++)
{sum = sum + h[i];}
double average = (double) sum/h.length;

Testing:

int[] h = {1, 3, 0};

Tracing:

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

----
|  \|
---\
h  \
\|
- -------
|   1  |  h[0]
-------
|   3  |  h[1]
-------
|   0  |  h[2]
-------

Then, we define a new variable sum and assigning to it a value 0:

-------
|   0   |
-------
sum

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

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

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

sum = sum + h[0];

In other words, we add the values of sum (0) and h[0] (1) and
place the result 0 + 1 = 1 into the variable sum:

-------   -------
|   1   | |   0   |
-------   -------
sum        i

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

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

The condition i < h.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

sum = sum + h[1];

In other words, we add the values of sum (1) and h[1] (3) and
place the result 1 + 3 = 4 into the variable sum:

-------   -------
|   4   | |   1   |
-------   -------
sum        i

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

-------   -------
|   4   | |   2   |
-------   -------
sum        i

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

sum = sum + h[2];

In other words, we add the values of sum (4) and h[2] (0) and
place the result 4 + 0 = 4 into the variable sum:

-------   -------
|   4   | |   2   |
-------   -------
sum        i

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

-------   -------
|   4   | |   3   |
-------   -------
sum        i

The condition i < h.length is no longer satisfied, since i = 3 and
the length of h is also 3. Thus, we get out of the loop.

Finally, we declare a new variable average and assign to it the
value (double) 4 / 3 = 4.0 / 3 = 1.333...

-------   -------   -------------
|  4    | |   3   | |  1.33333... |
-------   -------   -------------
sum        i         average

```
4a. Write a method that, given an array h of integers, computes the largest element of this array.

4b. To check the correctness of your method, write a piece of code that defines a new array z consisting of 3 elements of type integer and assigns, to elements of this array, values 1, 3, and 0. 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 max(1, 3, 0) = 3.

```
public int largest(int[] h){
int largestSoFar = h[0];
for(int i = 1; i < h.length; i++)
{if (h[i] > largestSoFar) largestSoFar = h[i];}
return largestSoFar;
}

in the main method:

int[] z = {1, 3, 0};
int large = largest(z);

tracing:

First, we define an array

----
|  \|
---\
z  \
\|
- -------
|   1  |  z[0]
-------
|   3  |  z[1]
-------
|   0  |  z[2]
-------

Then, we call a method:

---------------------------
|    ----                   |
|   |  \|                   |
|    ---\                   |
|     h  \                  |
|         \|                |
|         - -------         |
|          |   1  |  h[0]   |
|           -------         |
|          |   3  |  h[1]   |
|           -------         |
|          |   0  |  h[2]   |
|           -------         |
|---------------------------|
|                           |
|          ...              |

Within this method, we define a new variable largestSoFar and assign
it a value h[0] = 1:

---------------------
|        1            |
---------------------
largestSoFar

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

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

Since 1 = i < h.length = 3, we go inside the loop. Since i = 1, the
condition 3 = h[1] > largestSoFar = 1 is satisfied, so we assign, to the
variable largestSoFar, a new value h[1] = 3:

---------------------      ---------
|         3           |    |    1    |
---------------------      ---------
largestSoFar                i

Next, we increase i by 1:

---------------------      ---------
|        3            |    |    2    |
---------------------      ---------
largestSoFar                i

Since 2 = i < h.length = 3, we go inside the loop. Since i = 2, the
condition 0 = h[2] > largestSoFar = 3 is not satisfied, so we do not
do anything. Next, we increase i by 1:

---------------------      ---------
|        3            |    |    3    |
---------------------      ---------
largestSoFar                i

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

int large = largest(z);

according to which the value 3 is assigned to the new variable large:

-------------
|     3       |
-------------
large

```
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"
```