Problem 1 Consider a class Time that represents a time of day. It has attributes
ID: 3856080 • 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
Below is your classes: -
Time.java
package time;
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) {
if (isValid(initialHours, initialMinutes)) {
this.hours = initialHours;
this.minutes = initialMinutes;
} else {
this.hours = 0;
this.minutes = 0;
}
}
/**
* 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) {
if (initialHours >= 1 && initialHours <= 12) {
if (isAM) {
if (initialHours == 12) {
this.hours = 0;
} else {
this.hours = initialHours;
}
} else {
if (initialHours == 12) {
this.hours = initialHours;
} else {
this.hours = 12 + initialHours;
}
}
} else {
this.hours = 0;
}
if ((initialMinutes >= 0 && initialMinutes <= 59) && (initialHours >= 1 && initialHours <= 12)) {
this.minutes = initialMinutes;
} else {
this.minutes = 0;
}
}
/**
* Formats the current Time in 12-hour format
*
* @return [h]h:mm {am | pm}
*/
public String getTime12() {
String response = "";
if (this.hours == 0) {
response = 12 + ":";
} else if (this.hours <= 12) {
response = this.hours + ":";
} else {
response = (this.hours - 12) + ":";
}
if (this.minutes < 10) {
response = response + "0" + this.minutes;
} else {
response = response + this.minutes;
}
if (this.hours < 12) {
response = response + " " + "am";
} else {
response = response + " " + "pm";
}
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 = "";
if (this.hours < 10) {
response = "0" + this.hours;
} else {
response = "" + this.hours;
}
if (this.minutes < 10) {
response = response + "0" + this.minutes;
} else {
response = response + this.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)) {
this.hours = newHours;
this.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) {
if (isAM) {
if (newHours == 12) {
this.hours = 0;
} else {
this.hours = newHours;
}
} else {
if (newHours == 12) {
this.hours = newHours;
} else {
this.hours = 12 + newHours;
}
}
if ((newMinutes >= 0 && newMinutes <= 59) && (newHours >= 1 && newHours <= 12)) {
this.minutes = newMinutes;
}
}
}
/*
* 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);
}
}
TestTime.java
package time;
import java.util.Scanner;
public class TestTime {
public static void main(String[] args) {
boolean done = false;
Scanner sc = new Scanner(System.in);
int hours, minutes;
boolean isAM;
int option;
while (!done) {
System.out.println(" Choose from the options below (1,2,3):");
System.out.println("1. 12 hour format input");
System.out.println("2. 24 hour format input");
System.out.println("3. Quit");
option = Integer.parseInt(sc.next());
switch (option) {
case 1:
System.out.print("Enter hours: ");
hours = Integer.parseInt(sc.next());
System.out.print("Enter minutes: ");
minutes = Integer.parseInt(sc.next());
System.out.print("is am/pm ?: ");
String amOrPm = sc.next();
if (amOrPm.equalsIgnoreCase("am")) {
isAM = true;
} else {
isAM = false;
}
Time time = new Time(hours, minutes, isAM);
System.out.println(" Time in 12 hours: " + time.getTime12());
System.out.println("Time in 24 hours: " + time.getTime24());
break;
case 2:
System.out.print("Enter hours: ");
hours = Integer.parseInt(sc.next());
System.out.print("Enter minutes: ");
minutes = Integer.parseInt(sc.next());
Time time1 = new Time(hours, minutes);
System.out.println(" Time in 12 hours: " + time1.getTime12());
System.out.println("Time in 24 hours: " + time1.getTime24());
break;
case 3:
done = true;
break;
default:
System.out.println("Enter a valid option. ");
}
}
}
}
Sample Run: -
Output Running Time.java
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
Output Running TestTime.java
Choose from the options below (1,2,3):
1. 12 hour format input
2. 24 hour format input
3. Quit
1
Enter hours: 13
Enter minutes: 40
is am/pm ?: am
Time in 12 hours: 12:00 am
Time in 24 hours: 0000
Choose from the options below (1,2,3):
1. 12 hour format input
2. 24 hour format input
3. Quit
1
Enter hours: 12
Enter minutes: 12
is am/pm ?: pm
Time in 12 hours: 12:12 pm
Time in 24 hours: 1212
Choose from the options below (1,2,3):
1. 12 hour format input
2. 24 hour format input
3. Quit
2
Enter hours: 22
Enter minutes: 22
Time in 12 hours: 10:22 pm
Time in 24 hours: 2222
Choose from the options below (1,2,3):
1. 12 hour format input
2. 24 hour format input
3. Quit
4
Enter a valid option.
Choose from the options below (1,2,3):
1. 12 hour format input
2. 24 hour format input
3. Quit
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.