Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 Consider a class Time that represents a time of day. It has attributes

ID: 3856082 • Letter: P

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 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
* <p>
* 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
* <p>
* 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: %10s %s %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: %10s %s %s ",
expectedRawTime,
expected24HourTime,
expected12HourTime ) ;
}

}

Explanation / Answer

Given below is the completed Time class and new TestTime class to test it. If any issues, please post a comment. If happy with the answer, please rate it . Thank you.

Time.java

public class Time

{

private int hours ;

private int minutes ;

  

/**

   * Creates and initializes a Time object to 12:00 midnight

   */

public Time()

{

   hours = 0;

   minutes = 0;

}

  

  

/**

   * 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

   * <p>

   * 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 )

{

   setTime(initialHours, initialMinutes);

      

}

  

  

/**

   * 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

   * <p>

   * 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 )

{

   setTime(initialHours, initialMinutes, isAM);

}

  

  

/**

   * Formats the current Time in 12-hour format

   * @return [h]h:mm {am | pm}

   */

public String getTime12()

{

String response = "" ;

String ampm = "am";

  

int h = hours;

  

if(hours == 0)

   h = 12;

else if(hours >= 12)

{

      

   ampm = "pm";

   if(hours > 12)

       h = hours - 12;

}

  

response = String.format("%d:%02d %s", h, minutes, ampm);

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 = String.format("%02d%02d", hours, minutes);

  

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 )

{

   if(isValid(newHours, newMinutes))

   {

       hours = newHours;

       minutes= newMinutes;

   }

      

}

  

/**

   * 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 )

{

         if(newHours >= 1 && newHours <= 12 && newMinutes >= 0 && newMinutes <= 59)

         {

             hours = newHours;

             minutes = newMinutes;

             if(isAM)

             {

                 if(hours == 12)

                     hours = 0;

             }

             else

             {

                 if(hours < 12)

                     hours += 12;

             }

         }

      

}

  

  

/*

   * 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 )

{

   if( timeHours >= 0 && timeHours <= 23 && timeMinutes >= 0 && timeMinutes <= 59)

       return true;

   else

       return false;

}

  

/**

   * 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: %10s %s %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: %10s %s %s ",

expectedRawTime,

expected24HourTime,

expected12HourTime ) ;

}

}


output

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

Valid test: new Time( 12, 1, AM )

Expect:      (0, 1)   0001   12:01 am

Actual:      (0, 1)   0001   12:01 am

Valid test: new Time( 12, 1, PM )

Expect:   (12, 1)   1201   12:01 pm

Actual:   (12, 1)   1201   12:01 pm

Valid test: new Time( 3, 45, AM )

Expect:   (3, 45)   0345   3:45 am

Actual:   (3, 45)   0345   3:45 am

Valid test: new Time( 3, 45, PM )

Expect:      (15, 45)   1545   3:45 pm

Actual:      (15, 45)   1545   3:45 pm

Valid test: new Time( 3, 45 )

Expect:   (3, 45)   0345   3:45 am

Actual:   (3, 45)   0345   3:45 am

Valid test: new Time( 15, 45 )

Expect:      (15, 45)   1545   3:45 pm

Actual:      (15, 45)   1545   3:45 pm

Valid test: setTime( 12, 21 )

Expect:      (12, 21)   1221   12:21 pm

Actual:      (12, 21)   1221   12:21 pm

Error test: setTime( -1, 13 )

Expect:      (12, 21)   1221   12:21 pm

Actual:      (12, 21)   1221   12:21 pm

Error test: setTime( 13, -1 )

Expect:      (12, 21)   1221   12:21 pm

Actual:      (12, 21)   1221   12:21 pm

Error test: setTime( 24, 42 )

Expect:      (12, 21)   1221   12:21 pm

Actual:      (12, 21)   1221   12:21 pm

Error test: setTime( 6, 60 )

Expect:      (12, 21)   1221   12:21 pm

Actual:      (12, 21)   1221   12:21 pm

Valid test: setTime( 8, 16, AM )

Expect:   (8, 16)   0816   8:16 am

Actual:   (8, 16)   0816   8:16 am

Valid test: setTime( 9, 17, PM )

Expect:      (21, 17)   2117   9:17 pm

Actual:      (21, 17)   2117   9:17 pm

Error test: setTime( 0, 15, AM )

Expect:      (21, 17)   2117   9:17 pm

Actual:      (21, 17)   2117   9:17 pm

Error test: setTime( 13, 14, AM )

Expect:      (21, 17)   2117   9:17 pm

Actual:      (21, 17)   2117   9:17 pm

Error test: setTime( 0, 13, PM )

Expect:      (21, 17)   2117   9:17 pm

Actual:      (21, 17)   2117   9:17 pm

Error test: setTime( 13, 12, PM )

Expect:      (21, 17)   2117   9:17 pm

Actual:      (21, 17)   2117   9:17 pm

Valid test: setTime( 12, 19, AM )

Expect:   (0, 19)   0019   12:19 am

Actual:   (0, 19)   0019   12:19 am

Valid test: setTime( 11, 14, PM )

Expect:      (23, 14)   2314   11:14 pm

Actual:      (23, 14)   2314   11:14 pm

Valid test: setTime( 11, 59, AM )

Expect:      (11, 59)   1159   11:59 am

Actual:      (11, 59)   1159   11:59 am

Valid test: setTime( 11, 59, PM )

Expect:      (23, 59)   2359   11:59 pm

Actual:      (23, 59)   2359   11:59 pm

TestTime.java

import java.util.Scanner;

public class TestTime {

   public static void main(String[] args) {

       Time t = new Time();

       Scanner keybd= new Scanner(System.in);

       int choice = 0;

       int hour, min;

       String ampm;

      

       while (choice != 3)

       {

           System.out.println("1. Input in 12 hour format");

           System.out.println("2. Input in 24 hour format");

           System.out.println("3. Quit");

           System.out.print(" Enter your choice: ");

           choice = keybd.nextInt();

           switch(choice)

           {

               case 1:

                   System.out.print("Enter hours [ 1-12 ]: ");

                   hour = keybd.nextInt();

                   System.out.print("Enter minutes [ 0-59 ]: ");

                   min = keybd.nextInt();

                   System.out.print("Is it am/pm ? ");

                   ampm = keybd.next();

                   t.setTime(hour, min, ampm.equalsIgnoreCase("am"));

                   break;

               case 2:

                   System.out.print("Enter hours [ 0-23 ]: ");

                   hour = keybd.nextInt();

                   System.out.print("Enter minutes [ 0-59 ]: ");

                   min = keybd.nextInt();

                   t.setTime(hour, min);

                  

                   break;

               case 3:

                   break;

               default:

                   System.out.println("Invalid menu option!");

           }

           System.out.println("Time is : (" + t.getTime12() + ") (" + t.getTime24() + ")");

       }

      

   }

}

output

1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 1
Enter hours [ 1-12 ]: 8
Enter minutes [ 0-59 ]: 30
Is it am/pm ? am
Time is : (8:30 am)    (0830)
1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 1
Enter hours [ 1-12 ]: 12
Enter minutes [ 0-59 ]: 45
Is it am/pm ? pm
Time is : (12:45 pm)    (1245)
1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 1
Enter hours [ 1-12 ]: 12
Enter minutes [ 0-59 ]: 15
Is it am/pm ? am
Time is : (12:15 am)    (0015)
1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 2
Enter hours [ 0-23 ]: 6
Enter minutes [ 0-59 ]: 45
Time is : (6:45 am)    (0645)
1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 2
Enter hours [ 0-23 ]: 15
Enter minutes [ 0-59 ]: 35
Time is : (3:35 pm)    (1535)
1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 2
Enter hours [ 0-23 ]: 0
Enter minutes [ 0-59 ]: 30
Time is : (12:30 am)    (0030)
1. Input in 12 hour format
2. Input in 24 hour format
3. Quit

Enter your choice: 3
Time is : (12:30 am)    (0030)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote