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:
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.
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.
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).
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