0

I am doing on a java assignment which involves time that displays seconds, minutes and hours.I am required to display time that is moved forward and backwards by few seconds. I am able to enable time to move forward by one second, however, I have difficulty allowing time to be rewinded backwards as my output is kind of weird

For instance, the desired output is supposed to show this:

01:02:03
04:05:06
Hour:·4
Minute:·5
Second:·6
23:59:58
23:59:59
00:00:01
00:00:00
23:59:58

However, my program shows this instead:

01:02:03
04:05:06
Hour: 4
Minute: 5
Second: 6
23:59:58
23:59:59
00:00:01
00:00:00
00:00:-2

The bottom output is the 00:00:-2 which is my output which shows the error. I would appreciate if someone can help me on that. Many thanks!

Attached below is my codings:

public class Time {
private int hour;
private int minute;
private int second;


public Time(int hour, int minute, int second)
{
    this.hour = hour;
    this.minute = minute;
    this.second = second;
}
public int getHour()
{
    return hour;
}
public int getMinute()
{
    return minute;
}
public int getSecond()
{
    return second;
}
public void setHour(int hour)
{
    this.hour = hour;
}
public void setMinute(int minute)
{
    this.minute = minute;
}
public void setSecond(int second)
{
    this.second = second;
}
public void setTime(int hour, int minute, int second)
{
    this.hour = hour;
    this.minute = minute;
    this.second = second;
}
public String toString()
{
    return String.format("%02d:%02d:%02d", hour, minute, second);
}
public Time nextSecond()
{
    second++;
    if (second == 60)
    {
        second = 0;
        minute++;
        if (minute == 60)
        {
            minute = 0;
            hour++;
            if (hour == 24)
            {
                hour = 0;
            }
        }
    }
    return this;

}

public Time previousSecond()
{
    second--;
    if (second == 60)
    {
        second = 0;
        minute--;
        if (minute < 60)
        {
            minute = 0;
            hour--;
            if (hour < 24)
            {
                hour = 0;
            }
        }
    }
    return this;
}
public static void main(String[] args) {
  // Test constructors and toString()
  Time t1 = new Time(1, 2, 3);
  System.out.println(t1);  // toString()

  // Test Setters and Getters
  t1.setHour(4);
  t1.setMinute(5);
  t1.setSecond(6);
  System.out.println(t1);  // toString()
  System.out.println("Hour: " + t1.getHour());
  System.out.println("Minute: " + t1.getMinute());
  System.out.println("Second: " + t1.getSecond());

  // Test setTime()
  t1.setTime(23, 59, 58);
  System.out.println(t1);  // toString()

  // Test nextSecond();
  System.out.println(t1.nextSecond());
  System.out.println(t1.nextSecond().nextSecond());

  // Test previousSecond()
  System.out.println(t1.previousSecond());
  System.out.println(t1.previousSecond().previousSecond());


}

   }
Jtang11
  • 5
  • 5
  • 2
    `second--;` followed by `if (second == 60)`. Shouldn't that be `if (second == -1) { second = 59;`? Same for minute and hour? – Andreas Feb 25 '20 at 02:41
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Feb 25 '20 at 02:43
  • Simply, if your `second` variable is lower than 0 (after the decrementation), then set it to 59 and later set the other time variables accordingly. If the minutes were also 0 then they needs to be set ot 59 and if you decreased the minutes from "0 to 59" then the hour needs to be decreased (if the hours are 0, than decreasing it should giv 23). In other words, you must check top and bottom ranges of each time compounds. – itwasntme Feb 25 '20 at 03:01

0 Answers0