## CS 1401, Exam #3

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

```

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

```

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

```

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

```

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