(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.]

First of all, the Time2Test class requires no modification. That’s the whole point of this exercise. Whenever you modify the internals of a class, in this case Time2, you should not break existing clients, in this case Time2Test. Ergo, the Time2Test class requires no modification. (Is that enough links to Time2Test?)

You should make the following changes to Time2.

In the declaration of instance variables, remove the following lines:
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59

Replace it with the following:
private int secondsSinceMidnight;

The following public methods and constructors should work as is, with no changes.
public Time2()
public Time2( int h )
public Time2( int h, int m )
public Time2( int h, int m, int s )
public Time2( Time2 time )
public String toUniversalString()
public String toString()

That means you need to get the following to work.
public void setTime( int h, int m, int s )
public void setHour( int h )
public void setMinute( int m )
public void setSecond( int s )
public int getHour()
public int getMinute()
public int getSecond()

The following is one way to do the setSecond method.
public void setMinute( int m )
{
secondsSinceMidnight = computeSeconds( getHour(),
validateMinute(m),
getSecond() );
}

This kind of problem, changing the internals of a class while keeping the same public interface, is pretty common in real life. The full solution will be available here and here after the end of next week’s class.