EECS2030 C & E Test 1C Solution

  1. TimeOfDay_solution1.java
  2. TimeOfDay_solution2.java
  3. TimeOfDay_solution3.java
  4. TimeOfDay_solution4.java

TimeOfDay.java Solution 1

This solution uses three fields: an int for the hour on a 12-hour clock, an int for the minute on a 12-hour clock, and a boolean for the AM/PM indicator (true for AM and false for PM).

The advance method works by calling one of two private methods. The first private method is able to advance the time forward by 1 minute accounting for changes in the hour and AM/PM indicator. The second private method is able to reverse the time backward by 1 minute accounting for changes in the hour and AM/PM indicator. advance uses a loop to call the appropriate method minutes times where minutes is the number of minutes that the time should change.

This solution is simple to implement compared to trying to change the time by an arbitrary number of minutes but it is slow if the time changes by a large number of minutes. See solutions 3 and 4 for solutions that do not use a loop.

Note that the constructors, accessor methods, setHour, and setMinute are straightforward to implement using this approach, but advance requires careful attention to detail to implement correctly.

TimeOfDay.java Solution 2

This solution uses one field, an int for the number of minutes after midnight. Because no explicit information is kept about the hour or AM/PM indicator, this information must be computed when required.

The advance method begins by making two observations:

  1. moving the time backwards by x minutes is equivalent to moving the time forward by (24 hours - x minutes).
  2. moving the time forward by 24 hours leaves the time unchanged
Observation 1 means that we can always move the time forward instead of trying to move the time backwards if minutes is negative. Observation 2 means that we can always move the time forward by less than 24 hours and still get the correct time for any value of minutes.

Because the time is kept in minutes, advance only needs to add minutes to this.minute and then ensure that this.minute is less than the number of minutes in one day.

Compared to the previous solution, the constructors, accessor methods, setHour, and setMinute are more difficult to implement but advance is much easier to implement. In particular, setHour requires careful attention to whether the current time is in the AM or PM.

TimeOfDay.java Solution 3

This solution uses three fields: an int for the hour on a 12-hour clock, an int for the minute on a 12-hour clock, and a boolean for the AM/PM indicator (true for AM and false for PM).

Without using a loop, this solution computes the final time after advancing the specified number of minutes. Keeping track of the correct number of hours and minutes to advance the time, as well as computing when the hour and AM/PM indicator change is very difficult to do without extensive unit tests.

TimeOfDay.java Solution 4

This solution uses three fields: an int for the hour on a 12-hour clock, an int for the minute on a 12-hour clock, and a boolean for the AM/PM indicator (true for AM and false for PM).

Without using a loop and without using the remainder operator, this solution computes the final time after advancing the specified number of minutes. In the previous solution, it is very tricky to keep track of when the AM/PM indicator needs to change. This solution avoids the AM/PM indicator problem by internally converting the time to a 24-hour clock.