Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Modifying the Internal Data Representation of a Class) It would be perfectly re

ID: 3821729 • Letter: #

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 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" ) );
  
  
  
  
  
   }
}

Explanation / Answer

Here is the modified version of Time2.java:

public class Time2
{
  
private int secondsSinceMidNight;
  
public Time2()
{
secondsSinceMidNight=0;
}
  
  
public Time2(int h)
{
secondsSinceMidNight = 60 * 60 * h;
}
  
public Time2(int h, int m)
{
secondsSinceMidNight = 60 * 60 * h + 60 * m;
}
  
public Time2(int h, int m, int s)
{
secondsSinceMidNight = 60 * 60 * h + 60 * m + s;
}
  
public Time2(Time2 Time)
{
this(Time.GetSecondsSinceMidNight());
}
  
/*Don't bother with setHour, setMinute, and setSecond. They make no sense in the current implementation and are deprecated.
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 void SetSecondsSinceMidNight(int sec)
{
   secondsSinceMidNight = sec;
}
  
public int GetSecondsSinceMidNight()
{
   return secondsSinceMidNight;
}
  
public int GetHour()
{
return secondsSinceMidNight / 3600;
}
  
public int GetMinute()
{
return (secondsSinceMidNight % 3600) /60;
}
  
public int GetSecond()
{
return (secondsSinceMidNight % 3600) % 60;
}
  
  
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" ) );
}
}

You can couple it with the tester, and if you face any problem, just post along with the tester code.