Questions: TimeOfDay

Write TWO of the following methods to add to the TimeOfDay class below

  1. equals(...). Write an equals method that returns true of the times are equal. The method header should look like this.
        public boolean equals(TimeOfDay t2)

    This would allow a program to do something like this.

        . . .
        TimeOfDay lunchtime = new TimeOfDay(13, 15);
        TimeOfDay dentistAppt = new TimeOfDay(x, y);
        . . .
        if (lunchtime.equals(dentistAppt)) . . .
  2. to12HrString(). Write a method which returns a string giving the time where the hour uses the 12 hour system, and either "AM" or "PM" is appended to the time. Don't worry about displaying the minutes as two digits. For example (using lunchtime from the above problem).
        System.out.println(lunchtime.to12HrString());

    This would print.

        1:15PM
  3. delay(int hr). This adds hr to the current time. The method header would be.
        public void delay(int hr)

    An example call might be.

        lunchtime.delay(1);

    This would move lunch to 14:15.

TimeOfDay class

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
public class TimeOfDay {
    //========================================= instance variables
    private int myHour;
    private int myMinute;

    //================================================ constructor
    public TimeOfDay(int h, int m) {
        myHour   = h;
        myMinute = m;
    }

    //================================================== getters
    public int getHour()   { return myHour;   }
    public int getMinute() { return myMinute; }

    //================================================== setters
    public void setHour(int h)   { myHour = h; }
    public void setMinute(int m) { myMinute = m; }

    //================================================= toString
    public String toString() {
        return myHour + ":" + myMinute;
    }
}