next up previous
Next: Solution of the exercise Up: Unit 05 Previous: Solution of the exercise

A class to handle the time of the day

Specification:

Design a class containing utilities to handle the time of the day, represented through hours, minutes, and, seconds. The class should define the following methods:

Methods of the class TimeOfDay:

// constructor
public TimeOfDay(int h, int m, int s)
// adds a time
public void add(TimeOfDay t)
// subtracts a time
public void subtract(TimeOfDay t)
// predicate to test for precedence
public boolean precedes(TimeOfDay t)
// predicate to test for equality
public boolean equalTo(TimeOfDay t)
// method toString
public String toString()

Example of usage:

public class TestTimeOfDay {
  public static void main(String[] args) {
    TimeOfDay t1 = new TimeOfDay(10,45,15);
    TimeOfDay t2 = new TimeOfDay(15,00,00);
    if (t1.precedes(t2)) {
      t1.add(new TimeOfDay(0,30,00));
    } else
      t2.subtract(new TimeOfDay(0,30,00));
    System.out.println("TimeOfDay 1 = " + t1);
    System.out.println("TimeOfDay 2 = " + t2);
    System.out.println("TimeOfDay 1 equal to TimeOfDay 2 ? "
                       + t1.equalTo(t2));
  }
}


next up previous
Next: Solution of the exercise Up: Unit 05 Previous: Solution of the exercise