Welp, another problem arise albeit small. Just with Decrementing hours, seconds, and minutes. I seem not get the final results right at all. Keep tinkering with it, and still not getting it.

This is my answer 23:00:00
23:00:00
23:00:00.

First is right, but the 2nd and 3rd should be 22:58:00 and 22:57:59

Therefore any help appreciated on with the decrement part. The first program is what i'm working on which is line 105 to 134. The 2nd program is for execution. Line 47 to 58 is what i'm trying to get the answer on. Not sure if the coding will be too long, will happily edit.

public class Clock
{
    private int hr;  //store hours
    private int min; //store minutes
    private int sec; //store seconds



    public Clock()
    {
  	    setTime(0, 0, 0);
    }


    public Clock(int hours, int minutes, int seconds)
    {
	    setTime(hours, minutes, seconds);
    }


    public void setTime(int hours, int minutes, int seconds)
    {
	    if (0 <= hours && hours < 24)
	        hr = hours;
	    else
	        hr = 0;

        if (0 <= minutes && minutes < 60)
	        min = minutes;
	    else
	        min = 0;

	    if (0 <= seconds && seconds < 60)
	        sec = seconds;
	    else
	        sec = 0;
    }

    public int getHours()
    {
	    return hr;
    }


    public int getMinutes()
    {
	    return min;
    }


    public int getSeconds()
    {
        return sec;
    }


    public void printTime()
    {
 	    if (hr < 10)
	        System.out.print("0");
        System.out.print(hr + ":");

	    if (min < 10)
	        System.out.print("0");
	    System.out.print(min + ":");

	    if (sec < 10)
	        System.out.print("0");
        System.out.print(sec);
    }

    public void incrementSeconds()
    {
        sec++;

	     if (sec > 59)
	     {
	         sec = 0;
	         incrementMinutes(); //increment minutes
	     }
    }


    public void incrementMinutes()
    {
	    min++;

	    if (min > 59)
	    {
	        min = 0;
	        incrementHours(); //increment hours
	    }
    }


    public void incrementHours()
    {
	    hr++;

	    if (hr > 23)
 	       hr = 0;

    }

    public void decrementHours()
    {
		hr--;

		if(hr< 23)

		hr = 23;

}

	public void decrementMinutes()
		{
			min--;
			if(min < 59)
			{
				min = 0;
				decrementHours();
			}
		}

   public void decrementSeconds()
		{
			sec--;

			if(sec < 59)
			{
			sec = 0;
			decrementMinutes();
			}
	}

    public boolean equals(Clock otherClock)
    {
        return (hr == otherClock.hr
 	            && min == otherClock.min
                && sec == otherClock.sec);
    }


    public void makeCopy(Clock otherClock)
    {
        hr = otherClock.hr;
        min = otherClock.min;
        sec = otherClock.sec;
    }


    public Clock getCopy()
    {
	    Clock temp = new Clock();

	    temp.hr = hr;
	    temp.min = min;
	    temp.sec = sec;

	    return temp;
 }
 public String toString() {
	  return String.format("%02d:%02d:%02d",hr, min, sec);
}
}
import java.util.*;

public class TestProgClock
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {
        Clock clock1 = new Clock(5, 4, 30); // 5:4:30 am
        Clock clock2 = new Clock(); // default time 0:0:0
        Clock clock3 = new Clock(9,59,00); // 9:59:00 am

        System.out.println( "clock1 should be 05:04:30, and it is " + clock1.toString());
        System.out.println( "clock2 should be 00:00:00, and it is " + clock2.toString());
        System.out.println( "clock3 should be 09:59:00, and it is " + clock3.toString());
        
        System.out.println( "clock2 is changed to 05:45:16");
        clock2.setTime(5, 45, 16); // change time to 05:45:16

        System.out.println( "clock2 should be 05:45:16, and it is " + clock2.toString());

        if (clock1.equals(clock2))
            System.out.println("clock1 is equal to clock2 (wrong)");
        else
            System.out.println("clock1 is not equal to clock2 (correct)");

        System.out.println( "clock1 is incremented by one second");
        clock1.incrementSeconds();
        System.out.println( "clock1 should be 05:04:31, and it is " + clock1.toString());

        System.out.println( "clock1 is incremented by 2 minutes");
        clock1.incrementMinutes();
        clock1.incrementMinutes();
        System.out.println( "clock1 should be 05:06:31, and it is " + clock1.toString());

        System.out.println( "clock1 is incremented by one hour");
        clock1.incrementHours();
        System.out.println( "clock1 should be 06:06:31, and it is " + clock1.toString());


        clock3.makeCopy(clock1);
        System.out.println("After copying "
                       + "clock1 into clock3, clock3 is "
                       + clock3);
                       
      	Clock clock4 = new Clock(); // set to 00:00:00
        System.out.println( "clock4 is decremented by one hour");
        clock4.decrementHours();
        System.out.println( "clock4 should be 23:00:00, and it is " + clock4.toString());

        System.out.println( "clock4 is decremented by 2 minutes");
        clock4.decrementMinutes();
        clock4.decrementMinutes();
        System.out.println( "clock4 should be 22:58:00, and it is " + clock4.toString());

        System.out.println( "clock4 is decremented by one second");
        clock4.decrementSeconds();
        System.out.println( "clock4 should be 22:57:59, and it is " + clock4.toString());        
                       
        
    }//end main
}

Recommended Answers

All 3 Replies

public void decrementHours()
    {
		hr--;
 
		if(hr< 23)
 
		hr = 23;
 
}
 
	public void decrementMinutes()
		{
			min--;
			if(min < 59)
			{
				min = 0;
				decrementHours();
			}
		}
 
   public void decrementSeconds()
		{
			sec--;
 
			if(sec < 59)
			{
			sec = 0;
			decrementMinutes();
			}
	}

The logic of all 3 of these look incorrect to me.

Let the time be 11:11:11. Try decrementing each of them. See if you get 10:10:10. Why, for example, if I am decrementing 11 to 10, would I care if 10 was less than 59? I'm guessing you copied and pasted the "increment" code and forgot to change some things.

yes, I agree. If you decremented the seconds of 12:12:12, you would get 12:12:11. So I would try something like:

public void decrementSeconds()
{
   sec--;
   if(sec < 0)
   {
      sec = 59;
      decrementMinutes();
   }
}

Getting to 0 is the trigger to decrement, not 59. So when you get below zero, set the seconds then decrement minutes. The decrementMinutes function will look the same as above but the decrementHours() would look like this:

public void decrementHours()
{
  hours--;
  if(hours < 0)
   hours = 23;
}

Alright thanks guys for the input. I got the java working 100%. Much appreciated and thanks for the pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.