04: Classes, Objects, Methods
Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.
A. (1 point) Write a minimal definition for a class named MyTime, about which you know nothing else (that is, don’t use the information in the following questions in this answer). Your answer to this question should be just the minimal definition – it will be really short!
B. (2 points) Add a constructor for MyTime that takes an hour, a minute (of the appropriate primitive type), and a boolean that is true when the time is am, and false when the time is pm. The constructor should store these values in appropriately-named instance variables.
If the hour or minute is invalid (hours should be from 1 to 12, and minutes should be from 0 to 59), then your constructor should throw an IllegalArgumentException.
Your answer should be the entire class definition.
C. (2 points) Add another constructor for MyTime that handles military time. It should take an hour and a minute (of the appropriate types). The constructor should inspect the hour, which will now be a value from 0 to 23 (this should be checked!), and convert it to an hour from 1 to 12, and an am boolean (as in part (B)). For instance:
- an hour value of
1will not change andamwill betrue - an hour value of
0should become12andamwill betrue - an hour value of
19should become7andamwill befalse - an hour value of
24should result in anIllegalArgumentExceptionbeing thrown
Your answer should be the entire class definition.
D. (1 point) Write a public toString() method with an appropriate signature that converts the MyTime object to a String representation as exemplified by "12:30 AM" or "4:32 PM".
Your answer should be just the toString() method.