Having trouble with the Time 2 source code
public class TimeTO {
private int totalSeconds;
// Constructors
public TimeTO() {
this(0, 0, 0); // Default to midnight
}
public TimeTO(int hour, int minute, int second) {
setTime(hour, minute, second);
}
// Setters
public void setTime(int hour, int minute, int second) {
setHour(hour);
setMinute(minute);
setSecond(second);
}
public void setHour(int hour) {
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("Hour must be 0-23");
totalSeconds = (totalSeconds % 3600) + (hour * 3600);
}
public void setMinute(int minute) {
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("Minute must be 0-59");
totalSeconds = (totalSeconds / 3600) * 3600 + (totalSeconds % 60) + (minute * 60);
}
public void setSecond(int second) {
if (second < 0 || second >= 60)
throw new IllegalArgumentException("Second must be 0-59");
totalSeconds = (totalSeconds / 60) * 60 + second;
}
// Getters
public int getHour() {
return totalSeconds / 3600;
}
public int getMinute() {
return (totalSeconds % 3600) / 60;
}
public int getSecond() {
return totalSeconds % 60;
}
// toString method to return the time as a String in the format HH:MM:SS
@Override
public String toString() {
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
}
public class TimeToTest {
public static void main(String[] args) {
TimeTo time = new TimeTo(13, 27, 6);
System.out.println("The time is: " + time);
time.setHour(23);
time.setMinute(59);
time.setSecond(59);
System.out.println("The updated time is: " + time);
time.setTime(0, 0, 0);
System.out.println("The time reset to: " + time);
}
}