I am looking for some assistance with completing the assignment of Time2 with a test class involving user input. The assignment is: Modify the Time2 class (below) to implement the time as the number of seconds since midnight. The class should have one data field (an int with the number of seconds since midnight) instead of three. This change should not affect the arguments, behavior, or output of the public methods. Create a Driver class with a main method to test your Time2 class. This program should ask the user to input the number of hours, minutes, and seconds past midnight, creating a Time2 object and using the mutator methods. The program should then use the toString() method to print out the time. I believe the only things that need modifying are the the 3 integers - hour, minute, second - to become one integer (totalseconds) and the set/get methods. I believe my calculations are correct but my test class is where I'm stumbling. I'm wondering if there is an issue between the toString method and the toUniversalString. I'm able to have the test class request the user input but it keeps outputting 12:00:00AM.
public class Time2 {
private int totalseconds;
public Time2(int hour, int minute, int second)setTime
{
this.totalseconds = (hour * 3600);
this.totalseconds += (minute * 60);
this.totalseconds += (second);
}
public Time2(Time2 time)setTime
{
this(time.getHour(), time.getMinute(), time.getSecond());
}
public void setTime(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("Hour must be 0-23");
if (minute < 0 || minute >= 59)
throw new IllegalArgumentException("Minute must be 0-59");
if (second < 0 || second >= 59)
throw new IllegalArgumentException("Hour must be 0-59");
this.totalseconds = (hour * 3600);
this.totalseconds += (minute * 60);
this.totalseconds += (second);
}
public void setHour(int hour)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("Hour must be 0-23");
this.totalseconds = (hour * 3600);
}
public void setMinute(int minute)
{
if (minute < 0 || minute >= 59)
throw new IllegalArgumentException("Minute must be 0-59");
this.totalseconds = (minute * 60);
}
public void setSecond(int second)
{
if (second < 0 || second >= 24)
throw new IllegalArgumentException("Second must be 0-59");
this.totalseconds = (second);
}
public int getHour()
{
return totalseconds / 3600;
}
public int getMinute()
{
return (totalseconds - (3600 * getHour())) / 60;
}
public int getSecond()
{
return totalseconds - (3600 * getHour()) - (60 * getMinute());
}
public String toUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
public String toString()
{
return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
< 12 ? "AM" : "PM"));
}
}
Test class
import java.util.Scanner;
public class Time2Test {
public static void main(String[] args) { // instantiate CommissionEmployee object
Scanner input = new Scanner(System.in);
System.out.print("Enter hour:");
String hour = input.next();
Integer.parseInt(hour);
System.out.print("Enter minute:");
String minute = input.next();
Integer.parseInt(minute);
System.out.print("Enter second:");
String second = input.next();
Integer.parseInt(second);
Time2 clock = new Time2(0, 0, 0);
System.out.printf(clock.toString());
}
}