## CS 1401, Exam #3, Solutions

Date: Thursday, April 5, 2007
Section (9 am or 10:30 am): ____________________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1a. Write a method named velocity for computing the ratio of two given real numbers d and t.

1b. Call (invoke) your method velocity in the main method to compute the ratio of the numbers d = 45.0 and t = 0.75. (You do not need to write the entire main method, just the part that assigns values to d and t 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 60 miles per hour).

```
Solution:

public double velocity(double distance, double time){
return distance / time;}

in the method main:

double d = 45.0;
double t = 0.75;
double v = velocity(d, t);

Tracing:

first,

---------    ---------
|   45.0  |  |   0.75  |
---------    ---------
d             t

----------------------------
|  ---------    ----------   |
| |  45.0   |  |   0.75   |  |
|  ---------    ----------   |
|   distance       time      |
|----------------------------|
|                            |
|                            |
|                            |
|                            |
----------------------------
velocity

---------
|  60.0   |
---------
v

```
2a. Define a class ShortTrips whose elements are short trips; each trip is characterized by its distance (in miles) and its time (in hours). Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the average velocity (mph).

2b. Use your class in the main method to define a new trip "LasCruces" of type ShortTrips with distance 45 miles and duration 0.75 hours. Compute and print this trip's average velocity; then, make this same trip longer: increase both the distance and the time by 2, and compute and print the new value of velocity.

```
Solution:

public class ShortTrips{
private double distance;
private double time;

public ShortTrip (double adistance, double atime){
distance = adistance, time = atime;}

public double getDistance(){return distance;}

public double getTime(){return time;}

public void setDistance(double newDistance){distance = newDistance;}

public void setTime(double newTime){time = newTime;}

public double velocity(){return distance / time;}
}

In the main method:

ShortTrips lasCruces = new ShortTrips(45.0, 0.75);
double v = lasCruces.velocity();
System.out.println(v);

double d = lasCruces.getDistance();
double t = lasCruces.getTime();
lasCruces.setDistance(2.0 * d);
lasCruces.setTime(2 * t);
v = lasCruces.velocity();
System.out.println(v);

Tracing:

on the first line, we call a constructor method:

--------------
|  ----------  |
| |  45.0    | |
|  ----------  |
|  ----------  |
| |   0.75   | |
|  ----------  |
|    atime     |
--------------
|              |
|              |
|              |
|              |
|              |
|              |
--------------
ShortTrips

after the first line, we get a new object

--------------
|  ----------  |
| |  45.0    | |
|  ----------  |
|   distance   |
|  ----------  |
| |   0.75   | |
|  ----------  |
|     time     |
--------------

lasCruces

after the second line, we get

----------
|   60.0   |
----------
v

at the third line, we print 60.0 to the screen

then, we compute the values

----------
|   45.0   |
----------
d

----------
|   0.75   |
----------
t

and change the object to:

--------------
|  ----------  |
| |  90.0    | |
|  ----------  |
|   distance   |
|  ----------  |
| |   1.5    | |
|  ----------  |
|     time     |
--------------

lasCruces

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

----------
|   45.0   |
----------
v

and we print 8.0 to the screen

```
3a. Write a piece of code that, given an array d of distances of different trips and the array t of their times, computes and prints the velocity of different trips.

3b. To check the correctness of the code you wrote in Part 3a, write a piece of code that defines a new array d consisting of 2 elements of type double and assigns, to elements of this array, values 45.0 and 90.0; defines a new array t consisting of 2 elements of type double and assigns, to elements of this array, values 0.75 and 1.5.

3c. Trace step-by-step how the piece of code you wrote in Part 3a will compute the corresponding velocities. Hint: both velocities are equal to 60.0.

```
Solution:

for (int i = 0; i < d.length; i++)
{System.out.println(d[i] / t[i]);}

Testing:

double[] d = {45.0, 90.0};
double[] t = {0.75, 1.5};

Tracing:

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

----
|  \|
---\
d  \
\|
- -------
| 45.0 |  d[0]
-------
| 90.0 |  d[1]
-------

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

----
|  \|
---\
t  \
\|
- -------
| 0.75 |  t[0]
-------
| 1.50 |  t[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 d is 2, so i is
smaller than this length. Thus, the condition i < d.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(d[0] / t[0]);

In other words, we print the value 45.0 / 0.75 = 60.0.

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

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

The condition i < d.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(d[1] / t[1]);

Thus, we print the value 90.0 / 1.5 = 60.0.

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

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

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

```
4a. Write a method that, given an array h of integers, returns the smallest of all the elements 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, -5, and 0.

4c. Trace step-by-step how the piece of code you wrote in Part 4a will compute the smallest element of this array. Hint: the desired smallest element is min(1, -5, 0) = -5.

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

in the main method:

int[] z = {1, -5, 0};
int small = smallest(z);

tracing:

First, we define an array

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

Then, we call a method:

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

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

---------------------
|        1            |
---------------------
smallestSoFar

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

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

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

---------------------      ---------
|        -5           |    |    1    |
---------------------      ---------
smallestSoFar                i

Next, we increase i by 1:

---------------------      ---------
|        -5           |    |    2    |
---------------------      ---------
smallestSoFar                i

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

---------------------      ---------
|       -5            |    |    3    |
---------------------      ---------
smallestSoFar                i

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

int small = smallest(z);

according to which the value -5 is assigned to the new variable small:

-------------
|     -5       |
-------------
small

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