Question: It would be perfectly reasonable for the Time2 class of Fig. 8.5 to represent the time internally as the number of seconds since midnight rather
It would be perfectly reasonable for the Time2 class of Fig. 8.5 to represent the time internally as the number of seconds since midnight rather than the three integer values hour, minute and second. Clients could use the same public methods and get the same results. Modify the Time2 class of Fig. 8.5 to implement the time as the number of seconds since midnight and show that no change is visible to the clients of the class.
Fig. 8.5

I 2 3 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 // 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 // Time2 no-argument constructor: // initializes each instance variable to zero public Time2() { this (0, 0, 0); // invoke constructor with three arguments } // Time2 constructor: hour supplied, minute and second defaulted to 0. public Time2(int hour) { this (hour, 0, 0); // invoke constructor with three arguments } // Time2 constructor: hour and minute supplied, second defaulted to 0 public Time2 (int hour, int minute) { this (hour, minute, 0); // invoke constructor with three arguments } // Time2 constructor: hour, minute and second supplied public Time2(int hour, int minute, int second) { if (hour < 0 11 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; } // Time2 constructor: another Time2 object supplied public Time2 (Time2 time) { // invoke constructor with three arguments this (time.hour, time.minute, time.second); } // 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 11 hour >= 24) { throw new Illegal ArgumentException ("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; } // validate and set hour public void setHour (int hour) { if (hour 0 11 hour >= 24) { throw new Illegal ArgumentException ("hour must be 0-23"); } this.hour hour; } // validate and set minute public void setMinute(int minute) { if (minute = 60) { throw new IllegalArgumentException ("minute must be 0-59"); } 112 113 114 115 116 117 118 119 } 120 } this.minute = minute; } // validate and set second public void set Second(int second) { if (second < 0 || second >=60) { throw new IllegalArgumentException ("second must be 0-59"); } this.second second; } // Get Methods // get hour value public int getHour() {return hour;} // get minute value public int getMinute() {return minute;} //get second value: public int getSecond() {return second;} // convert to String in universal-time format (HH:MM:SS) public String toUniversal String() { 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 11 getHour() == 12) ? 12: getHour () % 12), getMinute(), getSecond(), (getHour() < 12 ? "AM": "PM"));
Step by Step Solution
3.39 Rating (161 Votes )
There are 3 Steps involved in it
To modify the Time2 class to represent the time internally as the number of seconds since midnight while making no visible changes to the clients of the class you will need to perform the following st... View full answer
Get step-by-step solutions from verified subject matter experts
