CMPS 144 Fall 2022
Prog. Assg. #1: Time of Day
Due: 11:59pm, Sunday , October 2

Specification

For this assignment, you are to complete two Java classes, TimeOfDay and TimeOfDay1224, the latter of which is a child of the former. The Java application TimeOfDayTester should be useful for testing purposes.

One common way to refer to a time of day —precise to a minute— is to indicate how many full hours have passed since midnight and how many minutes have passed since the beginning of the current hour. This is called the 24-hour clock. In written form, it usually has the format hh:mm, as in "04:37" and "21:35".

An instance of the TimeOfDay class represents a time of day, described in terms of the 24-hour clock. By that is meant that in all interactions with client programs, a time of day is specified by using numbers indicating hours-since-midnight and minutes-since-beginning-of-hour. For example, the toString() method produces a string having the hh:mm form. Likewise, the setTo() method and one constructor require the client to use hours-since-midnight and minutes-since-beginning-of-hour to specifiy a time of day.

Five of the methods in the version of TimeOfDay provided to you are stubs, meaning that their method bodies are essentially empty. One of them is isBetween(), which you already may have formulated in a lab earlier this semester. The other four are changeByMinutes(), changeByHours(), advanceOneMinute(), and retreatOneMinute(). Among these, changeByMinutes() is really the only one that requires much work, because each of the others is easily expressed in terms of it. More about that method can be found below.

Another, more common, way of referring to a time of day is to use the 12-hour clock, which divides the day into two 12-hour periods, AM (before noon) and PM (after noon). Examples of the corresponding notation are "4:37am" and "9:35pm".

Imagine that we want to have a Java class whose instances represent times of day, but whose objects are capable of interacting with client programs in such a way that times can be described using both the 24-hour and 12-hour standards.

To satisfy this desire, we need not re-implement all the functionality found in the TimeOfDay class. Rather, we can extend/augment that functionality by devising a new child class. That is the intent of TimeOfDay1224.

An instance of TimeOfDay1224 can be in either of two modes: 24-hour mode or 12-hour mode. When in 24-hour mode, its toString() method produces a string in 24-hour notation (e.g., "08:39"). When in 12-hour mode, that method produces a string in 12-hour notation (e.g., "8:39am").

The methods setTo24Mode() and setTo12Mode() allow the client to dictate which mode a TimeOfDay1224 object is in. When such an object is created, its initial mode is dictated by which constructor was used to initialize it, as described in the comments.


The changeByMinutes() Method

This is a mutator method that allows a client program to change a time of day by a number of minutes specified by the parameter, mDelta. If mDelta > 0, this means that the time is to be advanced forward in time. If mDelta < 0, it means going back in time.

There is no restriction upon the magnitude of mDelta's value, which is to say that it could indicate a time change of multiple days. For example, consider the call changeByMinutes(2885). Because there are 1440 minutes per day, and 2885 = 2·1440 + 5, this call should result in the time being advanced by two days and five minutes. But that is the same as advancing by just five minutes! (After all, just like the clock on the wall in our classroom, a TimeOfDay object "knows nothing" about what day it is.)

More generally, any two calls changeByMinutes(k) and changeByMinutes(m) where the values of k and m differ by a multiple of 1440 should have exactly the same effect. What this strongly suggests is the use of Java's mod, (or remainder) operator (%), for the purpose of translating the value of mDelta into one that describes a time change of at most 1439 minutes (i.e., less than one day).

Dealing with negative values of mDelta is a little tricky, due to the fact that Java's % operator does not produce results that are consistent with the usual mathematical definition of remainder when the dividend is negative. For example, in Java −33 % 7 yields −5, reflecting that 7·-4 = -28 and -28 + -5 = -33. However, if we add 7 (the divisor) to the result, we get an answer consistent with mathematics, in this case -5 + 7 = 2.

Using this idea, you can translate any negative-valued mDelta into a positive value that is equivalent, for our purposes. For example, suppose that mDelta = −1500. Then when you divide 1440 into it, Java gives you a remainder of -60, which corresponds to going backwards by one hour. Adding 1440 to -60 yields 1380, which corresponds to going forward by 23 hours. But, on a clock, going backwards one hour is the same as going forward by 23 hours.


Program Submission

Use the Student File Submission/Retrieval Utility on the CMPS 144 course web page to submit the completed Java classes, which must be in files named TimeOfDay.java and TimeOfDay1224.java, into the relevant folder. Don't forget to complete the block of comments at the beginning of each class, which should include your name, the names of any persons who helped you or with whom you collaborated, and a description of any behavioral flaws you noticed in objects of your classes.