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 teste 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 fill in the missing bodies of several "stubbed" methods, as described next. Your tasks are listed below, in order from easiest to hardest (at least in the opinion of this author). But you may work on them in any order you wish. Use TimeOfDayTester each time (you think) you have completed a method in order to test it.
As an example, suppose that the parameters passed to the method were 54 (hours) and 150 (minutes). First, translate that into a number of minutes by multiplying 54 by MINUTES_PER_HOUR (i.e., 60), which yields 3240, and then adding 150, producing a result of 3390 minutes. There are 1440 minutes during each day (which one can compute by multiplying HOURS_PER_DAY by MINUTES_PER_HOUR, the two symbolic constants defined in the TimeOfDay class). Because 3390 / 1440 is 2 and 3390 % 1440 is 510, advancing by 3390 minutes means advancing by two days and 510 minutes. But a clock does not distinguish one day from another, and so this is equivalent to advancing by 510 minutes. (Indeed, the quotient 3390 / 1440 is irrelevant; only the remainder is significant.)
At this point, we can compute 510 / MINUTES_PER_HOUR to obtain the number (here, 8) by which the instance variable hour should increase (wrapping around to zero whenever appropriate) and 510 % MINUTES_PER_HOUR to obtain the number (here, 30) by which the instance variable minute should increase (wrapping around to zero whenever appropriate and, in case that it does, necessitating that hour be increased by one more).
When a TimeOfDay object is in 12-hour mode, its toString() method should produce a string describing the time in the traditional (i.e., AM/PM) 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 "9:37" or "19:02". Indeed, the only effect that an object's mode should have is to determine the format of the string produced by toString(). The mode should have no effect upon how the values of instance variables hour and minute are used/interpreted for representing the time of day.
To implement this feature, it is necessary not only to modify the toString() method (which, as given, produces strings in the traditional AM/PM format), but also to provide a means by which a TimeOfDay object is able to "remember" which mode it is in. (Question: How does an object remember something about itself?)
A more clever approach translates a backward movement in time into an equivalent forward movement and then calls advanceBy(). For example, going backward by 120 minutes (two hours) has the same effect as going forward by 1320 minutes (22 hours) (which we get using 1440 - 120, 1440 being the number of minutes in a day, as noted earler).
Of course, you must also take into account that the movement might be more than 1440 minutes (i.e. a whole day). Thus, first take the remainder when 1440 is divided into that number. For example, if the movement backwards is 4000 minutes, that is the same as moving 4000 % 1440 (which is 1120) minutes backwards. Which is the same as going forward by 1440 - 1120, or 320, minutes.
The program maintains two TimeOfDay objects, one referred to as current and the other as prev (for "previous"). Each time a command that calls for a mutator method to be called, prev takes on the value of current and then the relevant mutator method is applied to current. Afterwards, the value of each of them (i.e., the string produced by toString()) is displayed.
Welcome to the TimeOfDayTester program. Enter initial hour and minute values: 15 46 After initialization, current is 15:46 and prev is 0:00 Available commands (h, r, and n stand for natural numbers): ----------------------------------------------------------- Q (Quit) H (Help) C (test compareTo(prev)) E (test isEarlierThan(prev)) L (test isLaterThan(prev)) A1 (test advanceByOneMinute()) A (test advanceBy(h,r)) D1 (test decreaseByOneMinute()) D (test decreaseBy(h,r)) 12 (test setTo12HourMode()) 24 (test setTo24HourMode()) > A1 After calling advanceByOneMinute(), current is 15:47 and prev is 15:46 > A 12 37 After calling advanceBy(12,37), current is 4:24 and prev is 15:47 > D1 After calling decreaseByOneMinute(), current is 4:23 and prev is 4:24 > D 4 34 After calling decreaseBy(4,34), current is 23:49 and prev is 4:23 > E isEarlierThan() says that 23:49 is NOT earlier than 4:23 > L isLaterThan() says that 23:49 is later than 4:23 > C compareTo() says that 23:49 is greater than 4:23 > 12 After calling setTo12HourMode(), current is 11:49PM and prev is 4:23 > 24 After calling setTo24HourMode(), current is 23:49 and prev is 4:23 > Q Goodbye. |