CMPS 144L Fall 2022
Lab #2 (Sept. 8/9): TimeOfDay

The Java class TimeOfDay is incomplete, and your task is to bring it as close to completion as you can. To aid you in that, the Java application ToD_Tester is provided. Its purpose, as its name suggests, is to test the various features of TimeOfDay. (You are free to modify it in any way you see fit.) Another tool that can be used for testing is jGrasp's Workbench utility, as has been illustrated in lecture.

It is highly recommended that you take several minutes to study the source code of TimeOfDay in order to come to an understanding of how its features are implemented.

Once you have done that, you will be in a position to fill in the missing bodies of several "stubbed" methods, as described next. Note that it is not necessary for you to work on these in the same order that they are listed. Probably better would be to work your way from the easiest to the hardest, whatever that may be for you and your partner.

  1. Complete the retreatOneMinute() method. It is the inverse of the given advanceOneMinute() method, in that calling one of them, followed by the other, has no net effect upon the "target" TimeOfDay object.
    Note: The object to which a method is applied is sometimes called the "target" object. For example, the object referred to by time is the target object with respect to the method call time.advanceOneMinute(). End of note.
  2. Complete the toString24() method, which produces a string that describes the target object using 24-hour mode. Examples: "08:35", "18:02". This is in contrast to the provided toString() method, which produces a string that is in the traditional 12-hour mode (e.g., "8:35am", "11:07pm").
  3. Complete the isBetween() method. Where time, t1, and t2 are instances of TimeOfDay, the call

    time.isBetween(t1, t2)

    reports whether or not time lies in the interval of time starting at t1 and going to t2. This calculation is somewhat tricky in the case that midnight lies in that interval. For example, 3am is between 11pm and 8am.

    Provided is the isEarlierThan() method. It would behoove you to use it in the isBetween() method.

  4. Complete the setTo24Mode() and setTo12Mode() methods. The idea here is that a TimeOfDay object should be in one of two possible modes —12-hour or 24-hour— according to which of those two methods was called most recently.

    When a TimeOfDay object is in 12-hour mode, its toString() method should produce a string describing the time in the traditional format, as in "8:45am" or "12:06pm". But if such an object is in 24-hour mode, its toString() method should produce a string in the form hh:mm, as in "09:37" or "19:02".

    To implement this feature, it is necessary not only to modify the toString() method, but also to provide a means by which each TimeOfDay object is able to "remember" which mode it is in.


Submitting Your Work

Use the CMPS 144L Student File Submission/Retrieval Utility to submit your TimeOfDay.java file into the lab_02 folder.