In: Computer Science
Java programming.
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number.
For example, 60
seconds:
00:00
00:01
00:02
. . .
00:58
00:59
01:00
Explanation:I have implemented the DecimalTimer class and have provided displayTimer() method that displays the time based on the number of seconds.I have shown the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.
//code starts
public class DecimalTimer {
public void displayTimer(int time) {
int min = 0;
int sec = 0;
String mm = "0";
String ss = "0";
System.out.println(time + "
seconds:");
while (time-- > -1) {
if (sec > 59)
{
min++;
sec = 0;
}
if (min == 24)
{
min = 0;
}
if (min < 10)
{
mm += min;
} else {
mm = "" + min;
}
if (sec < 10)
{
ss += sec;
} else {
ss = "" + sec;
}
sec++;
System.out.println(mm + ":" + ss);
mm = "0";
ss = "0";
}
}
public static void main(String[] args) throws InterruptedException {
DecimalTimer decimalTimer = new
DecimalTimer();
decimalTimer.displayTimer(120);
}
}
Output: