Lab #12: Writing Class Clock for Recording a Military Time

The goal of this lab is to write an object class Clock for time in the military time (that is, 0 - 23 for the hour and 0 - 59 for the minute).

The class shall use two instance variables, hour and minute, for recording the hour and the minute of the clock time, respectively.

Including one constructor there shall be six methods in the class:

  1. public Clock( int hour, int minute )
    This is the constructor. It assigns the value of each parameter to its corresponding field variable. Since the field variable has the same name as the parameter, the prefix of "this." should be used for distinghishing the field variable from the parameter. The constructor also should test the validity of the parmaeter values; if the value of the parameter hour is either negative or greater than or equal to 24, it throws a new IllegalArgumentException, and if the value of the parameter minute is either negative or greater than or equal to 60, it throws a new IllegalArgumentException.
  2. public int method getHour()
    This is a method for assessing the value of hour.
  3. public int method getMinute()
    This is a method for assessing the value of minute.
  4. public int compareTo( Clock o )
    This is a method for comparing the time stored in the object at hand (represented by the two instance variables) with the other Clock object o. The method returns -1, +1, and 0 if the object at hand is ahead, after, or equal to the time o, respectively.
  5. public void plus( Clock o )
    This is a method for advancing the time stored in the object at hand by the time stored in another Clock object o. The value of this.hour shall increase by the value of hour of o. The value of this.minute shall increae by the value of minute of o. If the latter becomes greater than or equal to 60, there is a carry into the hour; the value of minute shall decrease by 60 and the value of hour shall increase by 1. After these changes, if the value of hour is greater than or equal to 24, the value of hour shall decrease by 24 (we go to the next day, but the information that the time has gone to the next day shall be disregarded).
  6. public void minnute( Clock o )
    This is a method for rewinding the time stored in the object at hand by the time stored in another Clock object o. The value of this.hour shall decrease by the value of hour of o. The value of this.minute shall decreae by the value of minute of o. If the latter becomes negative, there is a borrow from the hour; the value of minute shall increase by 60 and the value of hour shall increase by 1. After these changes, if the value of hour is negative, we go back to the previous day, and so we add 24 to the hour.

There is a class ClockMain that is already written for testing the class Clock. The two programs can be compiled together using the command

% javac Clock.java ClockMain.java

Here is an example of executing the code of ClockMain: % java ClockMain
Enter hour and minute: 10 20
Enter hour and minute: 15 22
Enter hour and minute: 8 10
Clock #1 is 10:20
Clock #2 is 15:22
Clock #3 is 08:10
New Clock #1 is 18:30
New Clock #2 is 07:12

Another example. Here an exception is thrown: % java ClockMain
Enter hour and minute: 10 20
Enter hour and minute: 25 61
Exception in thread "main" java.lang.IllegalArgumentException: An invalid hour value
at Clock.(Clock.java:5)
at ClockMain.main(ClockMain.java:13)

Here are the main class and the template for Clock.java.
ClockMain.java
Clock.java

Go back to the lab main page

Go back to the home page