Question: (Modifying the Internal Data Representation of a Class) It would be perfectly reasonable for the Time2 class of Fig 8.5 to represent the time internally

(Modifying the Internal Data Representation of a Class) 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 Time2 class as the number of seconds since midnight and show that no change is visible to the clents of the class. [In fact, you can use the original Time2Test class with your modified Time2 class.]

Don't bother with setHour, setMinute, and setSecond. They make no sense in the current implementation and are deprecated.

Our users of the Time2 class went INSANE when we changed the public interface. Ensure that we faithfully replicate all public methods. It turns out that with the correct integer division and the modulo operator, we can implement all methods.

What I have so far, please follow the guidelines above to help fix this code. Also, when I use this code in eclipse I get 4 errors saying "Recursive constructor invocation Time2(int, int, int)":

public class Time2

{

private int Hour; private int Minute; private int Second; public Time2() { Second=0; } public Time2(int h) { this(h, 0, 0); } public Time2(int h, int m) { this(h, m, 0); } public Time2(int h, int m, int s) { this(h, m, s); } public Time2(Time2 Time) { this(Time.GetHour(), Time.GetMinute(), Time.GetSecond()); } public void SetTime(int h, int m, int s) { SetHour(h); SetMinute(m); SetSecond(s); } public void SetHour(int h) { Hour = ((h >= 0 && h < 24)? h:0); } public void SetMinute(int m) { Minute = ((m >= 0 && m < 60)? m:0); } public void SetSecond(int s) { Second = ((s >= 0 && s < 60)? s:0); } public int GetHour() { return Hour; } public int GetMinute() { return Minute; } public int GetSecond() { return Second; } 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" ) ); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!