Question: 72025 Modify the Time2 class (below) to implement the time as the number of seconds since midnight. The class should have one data field (an
72025 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.
// Fig. 8.5: Time2.java // Time2 class declaration with overloaded constructors . public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59
public Time2() {this(0, 0, 0);} public Time2(int hour) {this(hour, 0, 0);} public Time2(int hour, int minute) {this(hour, minute, 0);}
// Time2 constructor : hour, minute and second supplied public Time2(int hour, int minute, int second) { if(hour<0||hour>=24) throw new IllegalArgumentException("hour must be 0-23"); if (minute < 0 || minute >= 60) throw new IllegalArgumentException("minute must be 0-59"); if (second < 0 || second >= 60) throw new IllegalArgumentException("second must be 0-59"); this.hour = hour; this.minute = minute; this.second = second; }
public Time2(Time2 time) {this(time.getHour(), time.getMinute(), time.getSecond());}
// Set Methods // set a new time value using universal time; // validate the data 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 >= 60) throw new IllegalArgumentException("minute must be 0-59"); if (second < 0 || second >= 60) throw new IllegalArgumentException("second must be 0-59"); this.hour = hour; this.minute = minute; this.second = second; }
public void setHour(int hour) { if (hour < 0 || hour >= 24) throw new IllegalArgumentException("hour must be 0-23"); this.hour = hour; }
public void setMinute(int minute) { if (minute < 0 && minute >= 60) throw new IllegalArgumentException("minute must be 0-59"); this.minute = minute; }
public void setSecond(int second) { if (second <= 0 || second > 60) throw new IllegalArgumentException("second must be 0-59"); this.second = second; }
public int getHour() {return hour;} public int getMinute() {return minute;} public int getSecond() {return second;}
// convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String .format( "%02d:%02d:%02d", getHour(), getMinute(), getSecond()); }
// convert to String in standard-time format (H:MM:SS AM or PM) public String toString() { return String .format("%d:%02d:%02d %s", ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM")); } } SAMPLE RUN #1: java Driver Enterhours:1 Enterminutes:35 Enterseconds:58 1:35:58AM
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
