CS120, Fall 2007


Assignment #6

DUE: Thursday, October 25.

Write a class to keep track of a time like a clock. The class should have a constructor for initializing the time, mutator methods for setting it (hours and minutes separately), a toString() method, a method for advancing the time by a given number of hours and minutes, and one for running the clock for a given number of hours and minutes (simply listing the time, minute by minute). Also allow methods to change from Standard Time to Daylight Savings and back (i.e., to "spring forward" and "fall back" by an hour). The name of the class should be Clock. A complete specification can be found at this location. Almost everything you need is here (including method headers -- see the "Method Detail" section -- and documentation!), except for the method bodies. Your job is to write a program with the code filled in. The actual code within each method is very short (sometimes 1, usually around 4, in one case perhaps 6 or 7 lines).

The methods in Clock.java should do what the specification says they should do; nothing more, and nothing less. For example, advance should just advance the time (changing the appropriate instance variables), not print it out. If you want to print out the time after advancing it, it is the job of Time to invoke the toString() method to do so. TIP: A number of these methods (e.g., run and springForward) are much easier to write after the advance method is written.

You can (and should!) test the methods of Clock as you develop them, using BlueJ. But another part of the assignment is to write a "driver" class named Time that allows the user to try out all the methods of Clock at the command line, via a menu. This is an opportunity for using a while-loop and a switch statement. To give you a better idea of how Time should work, a sample run is given below. The Time application should test all features of your program, in particular, it should try every method of the Clock class. This is done (well, almost) in the dialog given below, in which user input is in italics.
Enter initial time for the clock:
Enter hour --> 9
Enter minutes --> 56
Current time 9:56.
Enter 'e' to enter time,
      'p' to print time,
      'a' to advance
      'r' to run,
      'q' to quit.
p
Current time 9:56.
Enter 'e' to enter time,
      'p' to print time,
      'a' to advance
      'r' to run,
      'q' to quit.
a
Enter hours to advance --> 1
Enter minutes to advance --> 55
Current time 11:51.
Enter 'e' to enter time,
      'p' to print time,
      'a' to advance
      'r' to run,
      'q' to quit.
r
Enter hours to run --> 0
Enter minutes to run --> 12
Current time 11:52.
Current time 11:53.
Current time 11:54.
Current time 11:55.
Current time 11:56.
Current time 11:57.
Current time 11:58.
Current time 11:59.
Current time 12:00.
Current time 12:01.
Current time 12:02.
Current time 12:03.
Enter 'e' to enter time,
      'p' to print time,
      'a' to advance
      'r' to run,
      'q' to quit.
p
Current time 12:03.
Enter 'e' to enter time,
      'p' to print time,
      'a' to advance
      'r' to run,
      'q' to quit.
q
Be seeing you!

Back to CS 120 Home Page