CS 1401, Exam #3, TR version

Date: Thursday, April 3, 2008
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On April 3, 1860, the first successful Pony Express, a fast horseback mail service, arrived in California. This service allowed to send mail from coast to coast in ten days. Until the telegraph arrived, this was the fastest way to communicate.

1a. Write a method named speed that, given the distance d in miles and the travel time t in days, returns the average communication speed in miles per hour. (Hint: there are 24 hours in a day.)

1b. Call (invoke) your method speed in the main method to compute the communication speed for the case when 2,400 miles were covered in 10 day. 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. Since 10 days contain 240 hours, you should get 10 miles per hour.

2a. Define a class Route whose elements are different mail routes; each route is characterized by its distance (in miles) and the travel time (in days). Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the average speed (in miles per hour).

2b. Use your class in the main method to define a new object "ponyExpress" of type Route with distance 2,400 miles and travel time 10 days. Compute and print this route's average speed; then, assume that the travel time decreased to 5 days, and compute and print the new value of average speed.

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

3a. Write a piece of code that, given an array d of different distances and the array t of different travel times, computes and prints the average speed of the corresponding routes.

3b. To check the correctness of the code you wrote in Part 3a, write a piece of code that defines a new array d with elements 2,400 and 1,200 and a new array t with elements 10 and 5.

3c. Trace step-by-step how the piece of code you wrote in Part 3a will compute the corresponding speeds. Hint: for both routes, the average speed should be 10 miles per hour.

4a. Write a method that, given an array d of distances, finds the shortest distance.

4b. To check the correctness of your method, write a piece of code that defines a new array distances consisting of 3 elements 2,400, 1,200, and 1,800.

4c. Trace step-by-step how the piece of code you wrote in Part 4a will compute the shortest distance. Hint: the shortest of the three distances is 1,200 miles.

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