Computer Science 105 Lab 5: Classes and Methods -- Doing Time
Due: March 5, 2002

Objectives and Overview: The purpose of this lab is to gain some experience with procedural and data abstraction in problem solving.  Procedural abstraction is accomplished by defining and calling a Java "method."  Data abstraction is accomplished by defining and using a Java "class."

The lab has three parts. Part 1 uses three classes - Time, Clock, and DoingTime - and explores the relationships between them.  There, you should complete writing the methods of the Time class (in the file Time.java), test those methods, and answer a few questions about what you did.  Part 2 is a new problem that requires you to design a new client program that solves a problem with the help of this new Time class that you developed in Part 1.

Part 1. Exercises with Classes and Methods

Below is a program that exercises the Time and Clock classes  
import cs1.Keyboard;
// This program illustrates basic ideas about classes and methods.
// It uses the Time and Clock classes and their methods.
public class DoingTime {

public static void main (String[] args) {

Clock c = new Clock("Big Ben", 200, 10); // Create a clock in a
// new graphics window
// four Time variables declared and constructed
Time t1 = new Time(0,0), // midnight
t2 = new Time(12,0), // noon
t3 = new Time(0,0), // user's time typed as input
t4 = new Time(); // current time on the computer

// illustration of precedes and display methods
System.out.print("Current time - ");
t4.display();
System.out.println("Is current time prior to noon? " + t4.precedes(t2));

System.out.print("12:00 midnight = ");
t1.display();
System.out.print("12:00 noon = ");
t2.display();

// illustration of advanceClockBy method
t1.advanceClockBy(0, 240);
System.out.print("4:00AM - ");
t1.display();
t1.advanceClockBy(0, -8);
System.out.print("3:52AM - ");
t1.display();

// illustration of the readTime, getEof, and showTime methods
t3.readTime(); // obtain a time from the user
while (!t3.getEof()) {
c.showTime(t3); // display this time on the clock
System.out.print("You entered "); t3.display();
t3.readTime();
}
}
}

The three files needed to run this program are Time.java, Clock.java , and DoingTime.java, which are in the CS105 (Tucker) folder.  So drag a copy of the MyProject folder to the desktop, and then drag copies of these three files into that copy on the desktop.  Now Add all three to your project, change the main class to DoingTime, and Make this program.  

Notice that the Time class is not yet complete... some of its methods' bodies need to be completed, using the guidelines we discussed in class this morning.  After completing these methods, Make the project again to be sure that it is free of syntax errors.

To test your new methods, Run this project.  It should initially open two windows like the ones below:

Follow the instructions in the Java Console window and enter a pair of integers for each new time at the prompt.  When you are finished typing times, type -1 to end the run.  If your methods in the Time class are correct, all the output in the Java console should be correct (note that's not the case in the example above).

Part 1 Questions

1.  The class DoingTime is called a "client" of the Time and Clock classes, in the sense that it declares an object (variable) in each class.  What are the names of the Time and Clock objects in this program?
2.  When a Time object (such as t2) is declared and created, its instance variables are initiated by a so-called "constructor" method inside the Time class.  For example, the statement 
Time t2 = new Time(12, 0);
creates t2 and the constructor method Time(int h, int m) initializes its instance variables to 12 and 0, respectively.  What are the names of the instance variables in the Time class that are associated with these values?
3.  What is a constructor method and what is its use, in general?  For the Time class, what are the two constructors, and how do they differ?
4.  The "local variables" of a method are declared inside the body of the method and are used only to assist the method carry out its limited task.  The "parameters" of a method are enclosed in parentheses right after the method name in its declaration.  These identify additional objects that are needed by the method to carry out its task.  The "result type" of a method is the type of value that the method returns whenever it is called.  Look at the text of the Time class in the file Time.java.  For each of the methods advanceClockBy, readTime , and precedes in that class, identify its local variables, its parameters, and the type (class) of result that it returns.
5.  What is the purpose of the Clock class in this program? Which of its constructors and methods are used by the program DoingTime ?
6.  The program can display several different times of the day in a single run. What particular control structure in the program permits this? What is the condition upon which the program finally stops?
7.  If the user enters 35 or 78 in response to the message "Enter hour" or "Enter minute", what does the program do? Why?

Part 2. Predicting Flight Arrival Times

Incontinent Airlines needs a program to help it compute flight times for its flights that originate in the morning on the East coast and terminate on the West coast or beyond (3 or more time zones away) later the same day. For each departing flight, we know the time of departure, the number of time zones that it crosses while traveling West, and the estimated arrival time.

The program should compute, for each of these flights, the estimated flight time to reach the destination, in terms of the destination's own time zone. For example, if a flight leaves Boston at 7:00AM and is predicted to arrive at 10:30AM San Francisco time (3 time zones to the Wests), the program should determine that the duration of the flight will be 6 hours and 30 minutes.

Finally, the program should stop when the user types -1, and then display the flight time of the shortest flight.  For example, for the two flights illustrated below, the shortest flight will have flight time 3:30.

Your program should use whatever methods of the Time class, including the ones that you wrote in Part 1, to help solve this problem. 

Step 1 - Design a Program to Compute Flight Time

Before writing this program, it is useful to sit down and decide what variables it needs -- what is the input, what is the output, and what steps will need to be repeated for each different set of flight information that the user enters.

Use meaningful names for all the variables you declare. Your program should provide clear and concise output to the screen, both in the form of prompts for user input and displaying results. The program should also be well-documented with explanatory comments.

A typical dialog between the program and the user, for each particular flight, might look like this:

Departure time: Enter hour (or -1 if done): 7
Enter minute: 0
Arrival time: Enter hour (or -1 if done): 10
Enter minute: 30
Number of time zones: 3
Flight time = 6:30

Departure time: Enter hour (or -1 if done): 14
Enter minute: 15
Arrival time: Enter hour (or -1 if done): 16
Enter minutes: 45
Number of time zones: 1
Flight time = 3:30

(Optional) The program may display two clocks -- one showing departure time and the other showing arrival time for each flight as it is entered.  

(Optional) Consider augmenting the program so that it takes flights in either direction - East or West (using an additional user input) and computes the same results.

Step 2 - Type and Run the Program

When you type your program, it should be saved with a file name that identifies you uniquely, such as lab5atucker.java . Be sure to add this program to your project, along with the Time and Clock classes, before you Run the project.

As you test the program, try using the Debug option to single-step through it and observe the instance variables associated with the Time objects as they change.

Lab 5 Deliverables

By 10:00AM on the due date, drag a copy of your completed Time class from Part 1 and program from Part  2 to the Drop Box in the CS105 (Tucker) folder.  A printoout of these two files, along with your written answers to the questions in Part 1, should be submitted in hard copy.

You may work in teams on the programming parts of this project. However, your answers to the questions in Part 1 should be completed individually.