So currently I coded a program where it displays seconds from midnight. However,
ID: 3839160 • Letter: S
Question
So currently I coded a program where it displays seconds from midnight. However, I did this wrong and instead of doing
private int Hour; // 0 - 23
private int Minute; // 0 - 59
private int Second; // 0 - 59
I'm only supposed to use private int secondsFromMidnight and then implement every constructor and every method in Time2 in terms of secondsFromMidnight, not hours minutes, and seconds. Though there will still be parameters called hours, minutes, and seconds. Just the instance variable will be secondsFromMidnight. I'm having trouble changing from the 3 int into just 1 int called secondsfromMidnight. This is my code so far (note: I've excluded the test class) . Here is my code so far:
http://www.codesend.com/view/b67a4462499e1b80c45f10002305c37a/
Any help or suggestion is appreciated, thank you
Explanation / Answer
Here is the modified version for you:
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" ) );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.