Task: Create a client for the Time class. Be very thorough with your testing (in
ID: 3671097 • Letter: T
Question
Task: Create a client for the Time class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below:
Initial time t1 (alternate constructor invoked) - military format: 08:15:30
Initial time t1 (alternate constructor invoked) - standard format: 8:15:30 AM
Initial time t2 (default constructor invoked) - military format: 00:00:00
Initial time t2 (default constructor invoked) - standard format: 12:00:00 AM
t2 after call to setTime - military format: 09:45:35
t2 after call to setTime - standard format: 9:45:35 AM
After call to equals: times are NOT equal.
After call to lessThan: t1 is less than t2.
Enter hours, minutes, and seconds: 10 11 12
New time t1 after call to setTime - standard format: 10:11:12 AM
New time t1 after call to advanceSecs - standard format: 10:11:13 AM
New t2 after call to copy - standard format: 10:11:13 AM
Test toString for t2: 10:11:13
Explanation / Answer
Please find below the java code :
//Class Time (Time.java)
public class Time {
private int hrs; //store hours
private int mins; //store minutes
private int secs; //store seconds
//Default constructor
public Time() {
hrs = 0;
mins = 0;
secs = 0;
}
//Alternate constructor with parameters, to set the time
public Time(int h, int m, int s) {
hrs = h;
mins = m;
secs = s;
}
//Method to set the time
public void setTime(int h, int m, int s) {
hrs = (h >= 0 && h < 24)? h : 0;
mins = (m >= 0 && m < 60)? m : 0;
secs = (s >= 0 && s < 60)? s : 0;
}
//Method to return the hours
public int getHours() {
return hrs;
}
//Method to return the minutes
public int getMinutes(){
return mins;
}
//Method to return the seconds
public int getSeconds() {
return secs;
}
//Method to print time in military format
//Time is printed in the form HH:MM:SS
public void printTimeMilitary(){
System.out.print((hrs < 10? "0": "") + hrs + ":");
System.out.print((mins < 10? "0": "") + mins + ":");
System.out.print((secs < 10? "0": "") + secs);
}
//Method to print time in standard format
//Time is printed in the form HH:MM:SS AM/PM
public void printTimeStandard(){
System.out.print((hrs == 0 || hrs == 12? 12: hrs % 12) + ":");
System.out.print((mins < 10? "0": "") + mins + ":");
System.out.print((secs < 10? "0": "") + secs + " ");
System.out.print((hrs < 12? "AM": "PM"));
}
//Method toString
public String toString(){
return hrs + ":" + mins + ":" + secs;
}
//Time advanced by one hour.
public void incrementHrs() {
hrs++;
if(hrs > 23)
hrs = 0;
}
//Time advanced by one minute.
public void incrementMins() {
mins++;
if(mins > 59){
mins = 0;
incrementHrs();
}
}
//Time advanced by one second. When 23:59:59 wrap around to 00:00:00
public void incrementSecs(){
secs++;
if(secs > 59){
secs = 0;
incrementMins();
}
}
//Method to compare two times for equality
public boolean equals(Time otherTime) {
return (hrs == otherTime.hrs && mins == otherTime.mins && secs == otherTime.secs);
}
//Method to compare two times for less than
public boolean lessThan(Time t) {
return (hrs < t.hrs || hrs == t.hrs && mins < t.mins || hrs == t.hrs && mins == t.mins && secs < t.secs);
}
//Method to copy the time
public void copy(Time otherTime) {
hrs = otherTime.hrs;
mins = otherTime.mins;
secs = otherTime.secs;
}
//Method to return a copy of the time
public Time getCopy() {
Time temp = new Time();
temp.hrs = hrs;
temp.mins = mins;
temp.secs = secs;
return temp;
//OR: return new Time(hrs, mins, secs);
}
}
//CLIENT #1: Program to test class Time (TimeClient1.java)
import java.util.Scanner;
public class TimeClient1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Time t1 = new Time(8, 15, 30);
Time t2 = new Time();
int hours, minutes, seconds;
System.out.print("Initial time t1 (alternate constructor invoked) - military format: ");
t1.printTimeMilitary();
System.out.println();
System.out.print("Initial time t1 (alternate constructor invoked) - standard format: ");
t1.printTimeStandard();
System.out.println();
System.out.print("Initial time t2 (default constructor invoked) - military format: ");
t2.printTimeMilitary();
System.out.println();
System.out.print("Initial time t2 (default constructor invoked) - standard format: ");
t2.printTimeStandard();
System.out.println();
t2.setTime(9, 45, 35);
System.out.print("t2 after call to setTime - military format: ");
t2.printTimeMilitary();
System.out.println();
System.out.print("t2 after call to setTime - standard format: ");
t2.printTimeStandard();
System.out.println();
if(t1.equals(t2))
System.out.println("After call to equals: times are equal.");
else
System.out.println("After call to equals: times are NOT equal.");
if(t1.lessThan(t2))
System.out.println("After call to lessThan: t1 is less than t2.");
else
System.out.println("After call to lessThan: t1 is NOT less than t2.");
System.out.print("Enter hours, minutes, and seconds: ");
hours = input.nextInt();
minutes = input.nextInt();
seconds = input.nextInt();
//you should call the method getInt()!!!
t1.setTime(hours, minutes, seconds);
System.out.print("New time t1 after call to setTime - standard format: ");
t1.printTimeStandard();
System.out.println();
t1.incrementSecs();
System.out.print("New time t1 after call to increment - standard format: ");
t1.printTimeStandard();
System.out.println();
t2.copy(t1);
System.out.print("New t2 after call to copy - standard format: ");
t2.printTimeStandard();
System.out.println();
System.out.println("Test toString for t2: " + t2);
}
}
OUTPUT:
Initial time t1 (alternate constructor invoked) - military format: 08:15:30
Initial time t1 (alternate constructor invoked) - standard format: 8:15:30 AM
Initial time t2 (default constructor invoked) - military format: 00:00:00
Initial time t2 (default constructor invoked) - standard format: 12:00:00 AM
t2 after call to setTime - military format: 09:45:35
t2 after call to setTime - standard format: 9:45:35 AM
After call to equals: times are NOT equal.
After call to lessThan: t1 is less than t2.
Enter hours, minutes, and seconds: 10 11 12
New time t1 after call to setTime - standard format: 10:11:12 AM
New time t1 after call to increment - standard format: 10:11:13 AM
New t2 after call to copy - standard format: 10:11:13 AM
Test toString for t2: 10:11:13
//CLIENT #2: Program to test class Time (TimeClient2.java)
import java.util.*;
public class TimeClient2 {
public static final int SIZE = 3;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Time[] t = new Time[SIZE];
Time start = new Time();
Time end = new Time(9, 15, 9);
int hours, minutes, seconds;
int i;
System.out.print("Initial time end (alternate constructor invoked) - military format: ");
end.printTimeMilitary();
System.out.println();
System.out.print("Initial time end (alternate constructor invoked) - standard format: ");
end.printTimeStandard();
System.out.println();
System.out.print("Initial time start (default constructor invoked) - military format: ");
start.printTimeMilitary();
System.out.println();
System.out.print("Initial time start (default constructor invoked) - standard format: ");
start.printTimeStandard();
System.out.println();
start.setTime(9, 45, 35);
System.out.print("start after call to setTime - military format: ");
start.printTimeMilitary();
System.out.println();
System.out.print("start after call to setTime - standard format: ");
start.printTimeStandard();
System.out.println();
if(start.equals(end))
System.out.println("After call to equals: times are equal.");
else
System.out.println("After call to equals: times are NOT equal.");
if(start.lessThan(end))
System.out.println("After call to lessThan: start time is less than end time.");
else
System.out.println("After call to lessThan: start time is NOT less than end time.");
//Array processing for times
//Initialize with data entered by the user
for(i = 0; i < t.length; i++) {
//you should call the method getInt()!!!
System.out.print("Enter hours, minutes, and seconds: ");
hours = input.nextInt();
minutes = input.nextInt();
seconds = input.nextInt();
t[i] = new Time(hours, minutes, seconds);
}
// Print the times
System.out.println("The times entered are (standard format):");
for (i = 0; i < t.length; i++) {
t[i].printTimeStandard();
System.out.println();
}
System.out.println("Called setTime for start time. Time initialized with 23 59 55.");
System.out.println("Increment 10 times. The times are (standard format):");
start.setTime(23, 59, 55);
for(i = 1; i <= 10; i++) {
start.printTimeStandard();
System.out.println();
start.incrementSecs();
}
}
}
OUTPUT:
Initial time end (alternate constructor invoked) - military format: 09:15:09
Initial time end (alternate constructor invoked) - standard format: 9:15:09 AM
Initial time start (default constructor invoked) - military format: 00:00:00
Initial time start (default constructor invoked) - standard format: 12:00:00 AM
start after call to setTime - military format: 09:45:35
start after call to setTime - standard format: 9:45:35 AM
After call to equals: times are NOT equal.
After call to lessThan: start time is NOT less than end time.
Enter hours, minutes, and seconds: 1 2 3
Enter hours, minutes, and seconds: 11 22 33
Enter hours, minutes, and seconds: 12 23 34
The times entered are (standard format):
1:02:03 AM
11:22:33 AM
12:23:34 PM
Called setTime for start time. Time initialized with 23 59 55.
Increment 10 times. The times are (standard format):
11:59:55 PM
11:59:56 PM
11:59:57 PM
11:59:58 PM
11:59:59 PM
12:00:00 AM
12:00:01 AM
12:00:02 AM
12:00:03 AM
12:00:04 AM
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.