11. Write code for toString and toStandardTime. Note: Output has to be identical
ID: 3910169 • Letter: 1
Question
11. Write code for toString and toStandardTime. Note: Output has to be identical and include the leading Os. (20 pts) public static void nain(Stringtl ares) s ) , 08, "hours"); //Output ClockTime t2 - new ClockTime(24 System.out.println(t2.toString)) /1 12:88 AM ClockTime t3 new ClockTime( 8, 5, "hours")i System.out.printin(t3.tostringO) //12:05 AM ClockTime t4- new ClockTime (10, 35, "hours") System.out.println(t4.toString)); // 18:35 AM ClockTime ts - new ClockTime(12, 15, "hours") System.out.println(t5.toString)); // 12:15 PM ClockTime t6-new ClockTime(13, ee, System.out.printin(t6.toString)): "hours"); //81:00 PM ClockTime t9 - new ClockTime (23, 59, "hours") System.out.println(t9.tostring)) // 11:59 PM ClockTime ta - new ClockTime 9, 30, "AM) System.out.println(ta.tostring)) 89:30 AM ClockTime tb - new ClockTime( 5, 81, "P") System.out.printin(tb.tostring)) / 05:01 PM // A ClockTine object represents an hour:minute time in in either standard time such //as 18:45 AM or 86:27 PM or military time such as 18:45 hours or 18:27 hours public class ClockTime ( private int hour; private int minute; private String label- "hours // Constructs a new time for the given hou /Mabel is "AM" or "PM". If the time is in military time, the label is "hours". r/minute. If time is in standard time, the // returns the field values this.hour h; this.minute n this.label-label; toStandardTime); public int getHour) public int getMinute) public String getLabel) f return hour; return minute; return label;Explanation / Answer
public String toString()
{
String hr = hour+"";
String mn = minute+"";
if(hour < 10)
hr = "0" + hour;
if(minute < 10)
mn = "0" + minute;
return hr + ":" + mn + " " + lable;
}
public void toStandardTime()
{
boolean checkMilitaryTime = false;
if(lable.equalsIgnoreCase("hours"))
checkMilitaryTime = true;
if(checkMilitaryTime)
{
if(hour == 0 || hour == 24)
{
hour = 12;
lable = "AM";
}
else
{
if(hour > 12)
{
lable = "PM";
hour -= 12;
}
else if(hour <= 12)
lable = "AM";
}
}
}
Required Methods are given belowpublic String toString()
{
String hr = hour+"";
String mn = minute+"";
if(hour < 10)
hr = "0" + hour;
if(minute < 10)
mn = "0" + minute;
return hr + ":" + mn + " " + lable;
}
public void toStandardTime()
{
boolean checkMilitaryTime = false;
if(lable.equalsIgnoreCase("hours"))
checkMilitaryTime = true;
if(checkMilitaryTime)
{
if(hour == 0 || hour == 24)
{
hour = 12;
lable = "AM";
}
else
{
if(hour > 12)
{
lable = "PM";
hour -= 12;
}
else if(hour <= 12)
lable = "AM";
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.