The instructions are....starting with an empty string test the hours and append a zero to the string followed by the hours when there is one digit in the hours or append the two digit hours otherwise. Use final variable MIN_2DIGITS for your tests and only use the += operator to append the string being created....the code has to go into the comment code goes here close to the bottom, specifically i need to format the time in a way that the entered hours, minutes, seconds in the ##:##:## format
i have tried this so far but it only outputs 00:00:00 when a user enters hours, minutes, and seconds
public class Clock
{
private static final byte DEFAULT_HOUR = 0,
DEFAULT_MIN = 0,
DEFAULT_SEC = 0,
MAX_HOURS = 24,
MAX_MINUTES = 60,
MAX_SECONDS = 60;
// ------------------
// Instance variables
// ------------------
private byte seconds,
minutes,
hours;
public Clock (byte hours , byte minutes , byte seconds )
{
setTime(hours, minutes, seconds);
}
public Clock ( )
{
setTime(DEFAULT_HOUR, DEFAULT_MIN, DEFAULT_SEC);
}
//----------
// Version 2
//----------
public String toString()
{
final byte MIN_2DIGITS = 10;
String str = "";
// CODE GOES HERE, what i have below didn't work
public String toString()
{
final byte MIN_2DIGITS = 10;
String str = "";
// my input
if (hours < MIN_2DIGITS)
{
str += "0" + hours + ":" ;
}
else
str += hours;
if (minutes < MIN_2DIGITS)
{
str += "0" + minutes + ":" ;
}
else
str += minutes;
if (seconds < MIN_2DIGITS)
{
str += "0" + seconds;
}
else
str += seconds;
//end of my input
return str;
}
return str;
}
} // End of class definition
You were almost there.
The following method needs to be added.
public void setTime(byte hours, byte minutes, byte seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
Changes to the following method.
public String toString()
{
final byte MIN_2DIGITS = 10;
String str = "";
// my input
if (hours < MIN_2DIGITS) {
str += "0" + hours + ":";
} else
str += hours + ":";
if (minutes < MIN_2DIGITS) {
str += "0" + minutes + ":";
} else
str += minutes + ":";
if (seconds < MIN_2DIGITS) {
str += "0" + seconds;
} else
str += seconds;
// end of my input
return str;
}
The method to test the code.
public static void main(String[] args) {
Clock clock = new Clock((byte) 9, (byte) 10, (byte) 35);
System.out.println(clock);
}