CMPS 144L Activity: Complete the TimeOfDay class

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 TimeOfDayTester is provided. Its purpose, as its name suggests, is to test the various features of TimeOfDay. (You are free to modify the test program in any way you see fit.) Another tool that can be used for testing is jGrasp's Workbench utility, as you may be familiar with from past work.

A sample dialog between a user and TimeOfDayTester appears near the end of this document.

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 make the modifications (including filling in the missing bodies of several "stubbed" methods) that are requested below. The comments preceding each method describe its intended behavior. Use TimeOfDayTester each time (you think) you have completed a method in order to test it.

  1. Fix the goForwardByMinute() method. The provided code fails to update the isAM instance variable when such a change is appropriate.
  2. Supply the missing body of the goForwardByHour() method.
  3. Supply the missing body of the goBackwardsByMinute() method. It would probably be a good idea to use your corrected version of the goForwardByMinute() method as a model, as the logic is similar.
  4. Supply the missing body of the goBackwardsByHour() method.
  5. Supply the missing body of the isEarlierThan() method. Here you will want to be able to refer to the instance variables of the TimeOfDay object referred to by the formal parameter t. It is easy to do that. For example, to refer to that object's hour instance variable, one simply uses the expression t.hour.
  6. Supply the missing body of the toString24Format() method. A good way to translate a value of type int into a numeral having a specified number of digits (including leading zero(s) if necessary) is by using the String.format() method.

    As an example, suppose that count is of type int and has a value in the range [0..100). Then the expression String.format("%02d", count) evaluates to a 2-digit numeral corresponding to the value of count. Thus, if count had value 57, the resulting String would be "57", and if count had value 8, the resulting String would be "08".

  7. The toString() method works, but the code therein could be improved. Modify it so that it has this form:

    String minuteStr, amPmStr;
    // < code that assigns an appropriate value to minuteStr >
    // < code that assigns an appropriate value to amPmStr >
    return hour + ":" + minuteStr + amPmStr;

    As with toString24Format(), using String.format() here would work nicely.


Example Dialog with TimeOfDayTester

Welcome to the TimeOfDayTester program.

Enter time of day (e.g., 5:46AM, 12:07PM): 11:58AM
After initialization, time is 11:58AM

Available commands:
-------------------
  Q        (Quit)
  H        (Help)
  FM       (test goForwardByMinute())
  FH       (test goForwardByHour())
  BM       (test goBackwardsByMinute())
  BH       (test goBackwardsByHour())
  E        (test isEarlierThan())
  24       (test toString24Format())

> FM
After calling goForwardByMinute(), the time is 11:59AM

> FM
After calling goForwardByMinute(), the time is 12:00PM

> FM
After calling goForwardByMinute(), the time is 12:01PM

> BM
After calling goBackwardsByMinute(), the time is 12:00PM

> BM
After calling goBackwardsByMinute(), the time is 11:59AM

> FH
After calling goForwardByHour(), the time is 12:59PM

> BH
After calling goBackwardsByHour(), the time is 11:59AM

> E
isEarlierThan() says that 11:59AM is NOT earlier than 8:23AM
isEarlierThan() says that 11:59AM is earlier than 5:17PM

> 24
The 24-hour format for 11:59AM is 11:59

> FM
After calling goForwardByMinute(), the time is 12:00PM

> FM
After calling goForwardByMinute(), the time is 12:01PM

> 24
The 24-hour format for 12:01PM is 12:01

> FH
After calling goForwardByHour(), the time is 1:01PM

> 24
The 24-hour format for 1:01PM is 13:01

> Q
Goodbye.