Question
Problem 1 Consider a class Time that represents a time of day. It has attributes for the hour and minute. The hour value ranges from
Problem 1 Consider a class Time that represents a time of day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Use the provided Time.java as a starting point do not change the unit tests (main() and the private utility methods following main()). a. Write three constructors: default constructor and two other constructors. A default constructor initializes the time to 0 hours, 0 minutes. Another two constructors are analogous to the setTime() methods described in Parts c and d. b. Write a private method isValid( hour, minute ) that returns true if the given hour and minute values are in the appropriate range. c. Write a method setTime( hour, minute ) that sets the time if the given values are valid. Valid hour value ranges from 0 to 23. Valid minute value ranges from 0 to 59. d. Write another method setTime( hour, minute, isAm ) that sets the time if the given values are valid. The given hour should be in the range 1 to 12. The parameter isAm is true if the time is an a. m. time and false otherwise. e. Write a method getTime24() that returns a string in 24-hour notation hhmm. For example, if the hour value is 7 and the minute value is 25, return "0725". If the hour value is 0 and the minute value is 5, return "0005". If the hour value is 15 and the minute value is 30, return "1530". f. Write a method getTime12() that returns a string in 12-hour notation h:mm xx. For example, if the hour value is 7 and the minute value is 25, return "7:25 am". If the hour value is 0 and the minute value is 5, return "12:05 am". If the hour value is 15 and the minute value is 30, return "3:30 pm". Comp 1050 - 2017-2su -- Lab 06 - Time -- DMR - 2017-07-08 01.docx P a g e | 2 David M Rosenberg Saturday, July 8, 2017 Notes: You may not use any Java library classes (Date, Time, etc) to manipulate the time. You must use exactly 2 instance variables (int hours; int minutes) you may not have an instance variable to indicate am/pm you always store time in 24-hour format! Complete the class Time and write an interactive program (separate class TestTime) to use your Time class. Your program must prompt for 12- or 24-hour input format or to quit. If 12-hour format is chosen, prompt for hours, minutes, and am or pm, then invoke the 3 parameter version of setTime(). If 24-hour format is chosen, prompt for hours and minutes, then invoke the 2 parameter version of setTime(). Display the time in both 12- and 24-hour formats. Repeat. You must use a switch() statement as the dispatcher to handle the first prompt (12-hour, 24-hour, quit). Example of expected output (partial): Valid test: new Time() Expect: (0, 0) 0000 12:00 am Actual: (0, 0) 0000 12:00 am Valid test: new Time( 0, 0 ) Expect: (0, 0) 0000 12:00 am Actual: (0, 0) 0000 12:00 am Error test: new Time( -1, 1 ) Expect: (0, 0) 0000 12:00 am Actual: (0, 0) 0000 12:00 am Error test: new Time( 1, -1 ) Expect: (0, 0) 0000 12:00 am Actual: (0, 0) 0000 12:00 am Error test: new Time( 24, 1 ) Expect: (0, 0) 0000 12:00 am Actual: (0, 0) 0000 12:00 am Error test: new Time( 1, 60 ) Expect: (0, 0) 0000 12:00 am Actual: (0, 0) 0000 12:00 am Valid test: new Time( 0, 19 ) Expect: (0, 19) 0019 12:19 am Actual: (0, 19) 0019 12:19 am Valid test: new Time( 3, 5 ) Expect: (3, 5) 0305 3:05 am Actual: (3, 5) 0305 3:05 am Valid test: new Time( 12, 1 ) Expect: (12, 1) 1201 12:01 pm Actual: (12, 1) 1201 12:01 pm Both classes (Time and TestTime) must be in a single project. Zip the entire project (.zip) and submit the .zip via Blackboard.
COMPLETE THE CODE BELOW
public class Time { private int hours ; private int minutes ; /** * Creates and initializes a Time object to 12:00 midnight */ public Time() { // TODO: implement this } /** * Creates and initializes a Time object given the time in 24-hour format * * @param initialHours 24-hour format in the range 0..23 * @param initialMinutes minutes in the range 0..59 *
* postconditions: if the specified hours and minutes are * in-range, the object will reflect those value but the time * will be set to 0 hours and 0 minutes (12:00 midnight) if * either or both values are out-of-range */ public Time( int initialHours, int initialMinutes ) { // TODO: implement this } /** * Creates and initializes a Time object given the time in 12-hour format * * @param initialHours whole number in the range 1..12 inclusive * @param initialMinutes whole number in the range 0..59 inclusive * @param isAM true: AM; false: PM *
* postconditions: if the specified hours and minutes are * in-range, the object will reflect those value but the time * will be set to 0 hours and 0 minutes (12:00 midnight) if * either or both values are out-of-range */ public Time( int initialHours, int initialMinutes, boolean isAM ) { // TODO: implement this } /** * Formats the current Time in 12-hour format * @return [h]h:mm {am | pm} */ public String getTime12() { String response = "" ; // TODO: implement this return response ; } /** * Formats the current Time in 24-hour format * @return hhmm where hours and minutes are always 2-digits each with leading 0-fill if needed */ public String getTime24() { String response = "" ;
// TODO: implement this return response ; } /** * Set the current time given the time in 24-hour format * @param newHours 24-hour format in the range 0..23 * @param newMinutes minutes in the range 0..59 */ public void setTime( int newHours, int newMinutes ) { // TODO: implement this }
/** * Set the current time given the time in 12-hour format * @param newHours 12-hour format in the range 1..12 * @param newMinutes minutes in the range 0..59 * @param isAM true: AM; false: PM */ public void setTime( int newHours, int newMinutes, boolean isAM ) { // TODO: implement this } /* * Replace (override) Object's method to produce a meaningful text representation of a Time * object. */ @Override public String toString() { return String.format( "(%d, %d)", hours, minutes ) ; }
/** * Verify that the hours and minutes are in-range (24-hour format) * @param timeHours * @param timeMinutes * @return */ private boolean isValid( int timeHours, int timeMinutes ) { // TODO: implement this return true ; // placeholder: return a meaningful value }
/** * Unit test driver. * @param args -unused- */ public static void main( String[] args ) { final boolean AM = true ; final boolean PM = false ; final boolean VALID_TEST = true ; final boolean ERROR_TEST = false ; Time testTime ; testTime = new Time() ; displayTest( VALID_TEST, "new Time()", testTime, "(0, 0)", "0000", "12:00 am" ) ; testTime = new Time( 0, 0 ) ; displayTest( VALID_TEST, "new Time( 0, 0 )", testTime, "(0, 0)", "0000", "12:00 am" ) ; testTime = new Time( -1, 1 ) ; displayTest( ERROR_TEST, "new Time( -1, 1 )", testTime, "(0, 0)", "0000", "12:00 am" ) ; testTime = new Time( 1, -1 ) ; displayTest( ERROR_TEST, "new Time( 1, -1 )", testTime, "(0, 0)", "0000", "12:00 am" ) ; testTime = new Time( 24, 1 ) ; displayTest( ERROR_TEST, "new Time( 24, 1 )", testTime, "(0, 0)", "0000", "12:00 am" ) ; testTime = new Time( 1, 60 ) ; displayTest( ERROR_TEST, "new Time( 1, 60 )", testTime, "(0, 0)", "0000", "12:00 am" ) ; testTime = new Time( 0, 19 ) ; displayTest( VALID_TEST, "new Time( 0, 19 )", testTime, "(0, 19)", "0019", "12:19 am" ) ; testTime = new Time( 3, 5 ) ; displayTest( VALID_TEST, "new Time( 3, 5 )", testTime, "(3, 5)", "0305", "3:05 am" ) ; testTime = new Time( 12, 1 ) ; displayTest( VALID_TEST, "new Time( 12, 1 )", testTime, "(12, 1)", "1201", "12:01 pm" ) ; testTime = new Time( 12, 1, AM ) ; displayTest( VALID_TEST, "new Time( 12, 1, AM )", testTime, "(0, 1)", "0001", "12:01 am" ) ; testTime = new Time( 12, 1, PM ) ; displayTest( VALID_TEST, "new Time( 12, 1, PM )", testTime, "(12, 1)", "1201", "12:01 pm" ) ; testTime = new Time( 3, 45, AM ) ; displayTest( VALID_TEST, "new Time( 3, 45, AM )", testTime, "(3, 45)", "0345", "3:45 am" ) ; testTime = new Time( 3, 45, PM ) ; displayTest( VALID_TEST, "new Time( 3, 45, PM )", testTime, "(15, 45)", "1545", "3:45 pm" ) ; testTime = new Time( 3, 45 ) ; displayTest( VALID_TEST, "new Time( 3, 45 )", testTime, "(3, 45)", "0345", "3:45 am" ) ; testTime = new Time( 15, 45 ) ; displayTest( VALID_TEST, "new Time( 15, 45 )", testTime, "(15, 45)", "1545", "3:45 pm" ) ; testTime.setTime( 12, 21 ) ; displayTest( VALID_TEST, "setTime( 12, 21 )", testTime, "(12, 21)", "1221", "12:21 pm" ) ; testTime.setTime( -1, 13 ) ; displayTest( ERROR_TEST, "setTime( -1, 13 )", testTime, "(12, 21)", "1221", "12:21 pm" ) ; testTime.setTime( 13, -1 ) ; displayTest( ERROR_TEST, "setTime( 13, -1 )", testTime, "(12, 21)", "1221", "12:21 pm" ) ; testTime.setTime( 24, 42 ) ; displayTest( ERROR_TEST, "setTime( 24, 42 )", testTime, "(12, 21)", "1221", "12:21 pm" ) ; testTime.setTime( 6, 60 ) ; displayTest( ERROR_TEST, "setTime( 6, 60 )", testTime, "(12, 21)", "1221", "12:21 pm" ) ; testTime.setTime( 8, 16, AM ) ; displayTest( VALID_TEST, "setTime( 8, 16, AM )", testTime, "(8, 16)", "0816", "8:16 am" ) ; testTime.setTime( 9, 17, PM ) ; displayTest( VALID_TEST, "setTime( 9, 17, PM )", testTime, "(21, 17)", "2117", "9:17 pm" ) ; testTime.setTime( 0, 15, AM ) ; displayTest( ERROR_TEST, "setTime( 0, 15, AM )", testTime, "(21, 17)", "2117", "9:17 pm" ) ; testTime.setTime( 13, 14, AM ) ; displayTest( ERROR_TEST, "setTime( 13, 14, AM )", testTime, "(21, 17)", "2117", "9:17 pm" ) ; testTime.setTime( 0, 13, PM ) ; displayTest( ERROR_TEST, "setTime( 0, 13, PM )", testTime, "(21, 17)", "2117", "9:17 pm" ) ; testTime.setTime( 13, 12, PM ) ; displayTest( ERROR_TEST, "setTime( 13, 12, PM )", testTime, "(21, 17)", "2117", "9:17 pm" ) ; testTime.setTime( 12, 19, AM ) ; displayTest( VALID_TEST, "setTime( 12, 19, AM )", testTime, "(0, 19)", "0019", "12:19 am" ) ; testTime.setTime( 11, 14, PM ) ; displayTest( VALID_TEST, "setTime( 11, 14, PM )", testTime, "(23, 14)", "2314", "11:14 pm" ) ; testTime.setTime( 11, 59, AM ) ; displayTest( VALID_TEST, "setTime( 11, 59, AM )", testTime, "(11, 59)", "1159", "11:59 am" ) ; testTime.setTime( 11, 59, PM ) ; displayTest( VALID_TEST, "setTime( 11, 59, PM )", testTime, "(23, 59)", "2359", "11:59 pm" ) ; }
/** * * @param isValid * @param testDescription * @param testTime * @param expectedRawTime * @param expected24HourTime * @param expected12HourTime */ private static void displayTest( boolean isValid, String testDescription, Time testTime, String expectedRawTime, String expected24HourTime, String expected12HourTime ) { System.out.println( ( isValid ? "Valid" : "Error" ) + " test: " + testDescription ) ; displayTestExpected( expectedRawTime, expected24HourTime, expected12HourTime ) ; displayTestActual( testTime ) ; System.out.println() ; } /** * * @param testTime */ private static void displayTestActual( Time testTime ) { System.out.printf( "Actual:\t%10s\t%s\t%s ", testTime, testTime.getTime24(), testTime.getTime12() ) ; }
/** * * @param expectedRawTime * @param expected24HourTime * @param expected12HourTime */ private static void displayTestExpected( String expectedRawTime, String expected24HourTime, String expected12HourTime ) { System.out.printf( "Expect:\t%10s\t%s\t%s ", expectedRawTime, expected24HourTime, expected12HourTime ) ; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started