Jump to content

Everytime i run this program i get this:

My Clock: 0:00
Your Clock:
My Clock (add 1 Minute): 0:01
My Clock (add 5 Minutes): 0:06
My Clock (add 1 Hour): 1:06
Your Clock (twelve hours later): 0:55
Your Clock (five minutes later): 1:00
 
For your clock it's suppose to have 12:55. How do i fix that?
 
public class Clock {                private int minute;                private int hour;                                public Clock(){                minute = 0;                hour = 0;                }                public Clock(int hour, int minute){                this.hour = hour;                this.minute = minute;                }                public int minute(){                return minute;                    }                    public int hour(){                return hour;                    }                public void addMinute(){                this.minute = this.minute + 1;                              }                public void addFiveMinutes(){                this.minute = this.minute +5;                }                public void addHour(){                this.hour = this.hour + 1;                }                public void addTwelveHours(){                this.hour = this.hour + 12;                 }                                    public void displayTime(){                                    if(this.minute>59){                System.out.println((hour%23)+ ":0" +(minute%60));                    }else  if(this.hour>23){                System.out.println((hour%24)+ ":" +(minute%60));                }else if(this.minute<10){                System.out.println((hour+ ":0" +minute));                    }                }                public String toString(){                return hour+ ":0" +minute;                   }
Link to comment
https://linustechtips.com/topic/309929-why-am-i-getting-this-output/
Share on other sites

Link to post
Share on other sites

Your problem is in the displayTime method, I'm posting a fix in a second. Another thing, why don't you use the default Java clock class? https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html

So its in here?

Clock myClock = new Clock();//call clock toString methodSystem.out.println("My Clock: \t\t\t"+myClock.toString()); //call constructor with two argumentsClock yourClock = new Clock(12,55);System.out.println("Your Clock: \t\t\t");yourClock.displayTime(); //call addMinutemyClock.addMinute();System.out.print("My Clock (add 1 Minute): \t");//call showTime methodmyClock.displayTime(); //call addFiveMinutesmyClock.addFiveMinutes();System.out.print("My Clock (add 5 Minutes): \t");myClock.displayTime(); //call addHourmyClock.addHour();System.out.print("My Clock (add 1 Hour): \t\t");myClock.displayTime(); //call addTwelveHoursyourClock.addTwelveHours();System.out.print("Your Clock (twelve hours later): ");yourClock.displayTime(); //call addTwelveHoursyourClock.addFiveMinutes();System.out.print("Your Clock (five minutes later): ");yourClock.displayTime();               }
Link to post
Share on other sites

You're doing some really crazy things

h/o

 

 

Instead of:

public void addMinute(){                this.minute = this.minute + 1;                              }                public void addFiveMinutes(){                this.minute = this.minute +5;                }

do

public void addminute(){    this.minute++;    if (this.minute >59)    {        this.minute = this.minute - 60;        this.hour++;    }}public void addFiveMinutes(){    this.minute = this.minute + 5;    if (this.minute >59)    {        this.minute = this.minute - 60;        this.hour++;    }}

Then fix the whole hour%23 problem and fix your displaytime.

public void displayTime(){    if (this.minute<10)    {        System.out.println(hour%24 + ":0"+ minute);    }    else    {        System.out.println(hour%24 + ":" + minute);    }}
Link to post
Share on other sites

 

Your problem is in the displayTime method, I'm posting a fix in a second. Another thing, why don't you use the default Java clock class? https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html

Try 

public void displayTime(){    	System.out.println((this.hour%24)+":"+(this.minute%60));}

when i do  that, it changes Your Clock (five minutes later): 1:00 to 0:00. I need this to be 1:00

Link to post
Share on other sites

You're doing some really crazy things

h/o

 

 

Instead of:

public void addMinute(){                this.minute = this.minute + 1;                              }                public void addFiveMinutes(){                this.minute = this.minute +5;                }

do

public void addMinute(){                this.minute = this.minute + 1;                 if  (this.minute >= 60)                {                     this.minute = this.minute - 60;                     this.hour = this.hour + 1;                    }                public void addFiveMinutes(){                this.minute = this.minute +5;                if  (this.minute >= 60)                {                     this.minute = this.minute - 60;                     this.hour = this.hour + 1;                    }                }

Then fix the whole hour%23 problem and fix your displaytime.

When i have hour%23, for some reason makes Your Clock (five minutes later): 1:00. If i change it to hour%24 it goes to 0:00

Link to post
Share on other sites

If you're goal is to use the %, you're going to hit 0:00 eventually. The 24th hour will become 0 instead of 24. I changed my post, but 0:00 is a perfectly fine and expected outcome. That is how it will display the 24th hour. If you're unhappy with that, then you're going to have to add in some other things.

Link to post
Share on other sites

If you're goal is to use the % command, you're going to hit 0:00 eventually. The 24th hour will become 0 instead of 24. I changed my post, but 0:00 is a perfectly fine and expected outcome. That is how it will display the 24th hour. If you're unhappy with that, then you're going to have to add in some other things.

well im trying to get the output:

My Clock: 0:00
Your Clock: 12:55
My Clock (add 1 Minute): 0:01
My Clock (add 5 Minutes): 0:06
My Clock (add 1 Hour): 1:06
Your Clock (twelve hours later): 0:55
Your Clock (five minutes later): 1:00
Link to post
Share on other sites

Yes, replace your displayTime method with mine version and it will work as you want:

 

public void displayTime(){    	System.out.println((this.hour%24)+":"+(this.minute%60));}

Check Lotus recommendations too, if you don't want a possible overflow in your code :D

thanks got it working 

Link to post
Share on other sites

Yes, replace your displayTime method with mine version and it will work as you want:

 

public void displayTime(){    	System.out.println((this.hour%24)+":"+(this.minute%60));}

Check Lotus recommendations too, if you don't want a possible overflow in your code :D

It's not just about overflow. With his current code, when he hits 60 minutes it'll just look like it went back to 0 without hitting the next hour. Like 1:45, 1:50, 1:55, 1:00, 1:05, etc.

Link to post
Share on other sites

It's not that big problem if he want to implement minutes separately from hours to do whatever he wants with it. What I don't get is why he is making a clock class if Java already offers a fully functional one =/

Well, if he needed help with it, I fully understand the desire to learn. It's only going to get harder.

Link to post
Share on other sites

Well, if he needed help with it, I fully understand the desire to learn. It's only going to get harder.

Totally agree, it is always know how things work. 

 

My advice (it won't be very optimized code, but it will be clean), create something like:

private void NormalizeTime(){this.hours += this.minutes / 60;this.minutes %= 60;this.hours %= 24;}

 

Then, in every method were you change the value of hours or minutes, at the end, call NormalizeTime(). For example:

                public void addHour(){                this.hour = this.hour + 1;                NormalizeTime();                }

 

That way, when you go to the DisplayTime function, you know minutes will always have a value in the range 0 - 59 and hours will have a value in the range 0 - 23

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×