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();
}
}
}
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?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?
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:
(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.
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.