|
Little Help ---
Heres this homework assignment -- I'm pretty confused on it -- anyone know how to do it?
Define a class called Time to represent 24-hour clock time values consisting of hour and minute components. The class should define the following private instance variables:
1. hour (integer), and
2. minute (integer).
Define the following public instance methods for the class
1. Mutator method setTime(): accepts 2 input parameters representing an hour and a minute values. The method should verify that the passed values are valid, i.e. the hour is between 0 and 23, and the minute is between 0 and 59. If the values are valid, they should be assigned to the corresponding instance variables of the calling object and the method should return true. Otherwise, the method should return false, leaving the instance variables unchanged.
2. Accessor method getHour(): returns the value of the hour instance variable of the calling object.
3. Accessor method getMinute(): returns the value of the minute instance variable of the calling object.
4. getAMPMtime(): returns a String, representing the time value stored in a calling object in the AM/PM format. For example, if the calling object’s hour is 14 and minute is 45, the method should return "2:45 PM". If the calling object’s hour is 10 and minute is 14, the method should return "10:14 AM".
Your class and method names should be exactly as specified above. Be sure to match capitalization precisely.
Furthermore, create a separate class called UseTime that will be used for testing the functionality of the Time class. The UseTime class will consist of a single method - main(). The main method must
1. Prompt the user to enter a time string in the format "hh:mm".
2. Extract the hour and the minute values from the time string.
3. Create a new object of class Time and call the setTime() method to set its instance variables to the hour and minute values obtained from the user. If the value returned by the setTime() method is false, display an appropriate message and continue asking the user to enter time strings, until setTime() returns true.
4. Call methods getHour() and getMinute() and print their return values, as shown in line 7 of the sample interaction below.
5. Print out the string representing the time value in the AM/PM format, using the result returned by getAMPMtime() method.
Here is a sample interaction.
Please enter a time string in the format "hh:mm": 17:70
This time string contains an invalid hour or minute component.
Please enter a time string in the format "hh:mm": 27:07
This time string contains an invalid hour or minute component.
Please enter a time string in the format "hh:mm": 17:20
Hour is 17, minute is 20.
That’s 5:20 PM.
Be sure to have your Time and UseTime classes defined in the same project. When running your program in Eclipse, pick UseTime as the main class.
|