Jump to content

I'm having trouble formatting my clock to 0:00. Instead it compiles into this:

My Clock: 0:0
Your Clock: 0:0
My Clock (add 1 Minute): 0:1
My Clock (add 5 Minutes): 0:6
My Clock (add 1 Hour): 1:6
Your Clock (twelve hours later): 12:0
Your Clock (five minutes later): 12:5
 
Here's my code:
public class Clock {                private int minute;                private int hour;                                public Clock(){                minute = 0;                hour = 0;                }                public Clock(int minutes, int hours){                this.minute = minute;                this.hour = hour;                }                public int minutes(){                return minute;                    }                    public int hours(){                return hour;                    }                public void addMinute(){                if(this.minute<59){                this.minute++;                }else{                this.minute=0;                this.hour++;                }                                }                public void addFiveMinutes(){                this.minute = this.minute + 5;                }                public void addHour(){                if(this.hour<24){                this.hour++;                    }                }                public void addTwelveHours(){                this.hour = this.hour + 12;                 }                private void checkTime(){                if(this.minute<=59){                this.minute++;                    }                if(this.hour<=24){                this.hour++;                }                }                public void displayTime(){                                     System.out.println((hour + ":" + minute));                }                public String toString(){                return hour+ ":" +minute;                   }
Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/
Share on other sites

Link to post
Share on other sites

When you display the time, you're only printing out the minute. If the minutes is a single digit (0 - 9), it will just print out the number. I think you can fix this by creating an if statement checking the value of the minutes, if it's < 10, then System.out.println((hour + ":0" + minute));

 

I'm only a novice programmer, so sorry if I do not make any sense or I am way off.

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4206225
Share on other sites

Link to post
Share on other sites

When you display the time, you're only printing out the minute. If the minutes is a single digit (0 - 9), it will just print out the number. I think you can fix this by creating an if statement checking the value of the minutes, if it's < 10, then System.out.println((hour + ":0" + minute));

 

I'm only a novice programmer, so sorry if I do not make any sense or I am way off.

yeah me too. Where would i put that if statement?

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4206245
Share on other sites

Link to post
Share on other sites

The simplest way would be to do what pureGold has said. A better way would be to use System.out.printf. You can read more about printf here: http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Also, I've noticed a few issues with your program.

1) The method addFiveMinutes isn't going to work correctly when minutes >= 55.

For example if the time was 0:59 and you called addFiveMinutes() then the time would be updated to 0:64.

2) The method addHour() doesn't reset to zero.

For example if the time was 24:00 (which isn't even a valid time) and you called addHour() then the time would remain the same.

3) The method addTwelveHours has the same issue as issue 1).

4) What is checkTime meant to be doing? If it's meant to be incrementing the hours and minutes by one, then you are going to have the same issue as 1) and 2).

For example if the time is 23:59 and you called checkTime() then the updated time will be 24:60.

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4206797
Share on other sites

Link to post
Share on other sites

The simplest way would be to do what pureGold has said. A better way would be to use System.out.printf. You can read more about printf here: http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Also, I've noticed a few issues with your program.

1) The method addFiveMinutes isn't going to work correctly when minutes >= 55.

For example if the time was 0:59 and you called addFiveMinutes() then the time would be updated to 0:64.

2) The method addHour() doesn't reset to zero.

For example if the time was 24:00 (which isn't even a valid time) and you called addHour() then the time would remain the same.

3) The method addTwelveHours has the same issue as issue 1).

4) What is checkTime meant to be doing? If it's meant to be incrementing the hours and minutes by one, then you are going to have the same issue as 1) and 2).

For example if the time is 23:59 and you called checkTime() then the updated time will be 24:60.

yeah, i've been trying to figure out how to reset it to zero all day but i can't think of anything. Is it more if statements or..?

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4207554
Share on other sites

Link to post
Share on other sites

It's to make sure the minutes isn't greater than 59 and hour isn't greater than 24. 

This doesn't sound right. If you implement your add minutes and add hours methods correct it should never be the case that the time is incorrect. Is this for a school assignment? 

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4207791
Share on other sites

Link to post
Share on other sites

 

I'm having trouble formatting my clock to 0:00. Instead it compiles into this:

My Clock: 0:0
Your Clock: 0:0
My Clock (add 1 Minute): 0:1
My Clock (add 5 Minutes): 0:6
My Clock (add 1 Hour): 1:6
Your Clock (twelve hours later): 12:0
Your Clock (five minutes later): 12:5
 
Here's my code:
-snip-

 

urg.. just please if you want help with something like this don't type it like that -,-

 

This is much more understandable.(btw this is just your class object since there is not main)

 

public class Clock 
{
   private int minute;
   private int hour;
                
   public Clock()
   {
      minute = 0;
      hour = 0;
   }
   public Clock(int minutes, int hours)
   {
      this.minute = minute;
      this.hour = hour;
   }
   public int minutes()
   {
      return minute;    
   }    
   public int hours()
   {
      return hour;    
   }
   public void addMinute()
   {
      if(this.minute<59)
      {
         this.minute++;
      }
      else
      {
         this.minute=0;
         this.hour++;
      }                
   }
   public void addFiveMinutes()
   {
      this.minute = this.minute + 5;
   }
   public void addHour()
   {
      if(this.hour<24)
      {
         this.hour++;    
      }
   }
   public void addTwelveHours()
   {
      this.hour = this.hour + 12; 
   }
   private void checkTime()
   {
      if(this.minute<=59)
      {
         this.minute++;    
      }
      if(this.hour<=24)
      {
         this.hour++;
      }
   }
   public void displayTime()
   {                    
      System.out.println((hour + ":" + minute));
   }
   public String toString()
   {
      return hour+ ":" +minute;   
   }
}

 

 

as for your question would this help as i think your just wanting to change the format of the output ?

 

SimpleDateFormat sdf = new SimpleDateFormat("HH:MM a");

 

source : http://stackoverflow.com/questions/13811224/java-display-current-time

Needs Update

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4210521
Share on other sites

Link to post
Share on other sites

urg.. just please if you want help with something like this don't type it like that -,-

 

This is much more understandable.(btw this is just your class object since there is not main)

 

public class Clock 
{
   private int minute;
   private int hour;
                
   public Clock()
   {
      minute = 0;
      hour = 0;
   }
   public Clock(int minutes, int hours)
   {
      this.minute = minute;
      this.hour = hour;
   }
   public int minutes()
   {
      return minute;    
   }    
   public int hours()
   {
      return hour;    
   }
   public void addMinute()
   {
      if(this.minute<59)
      {
         this.minute++;
      }
      else
      {
         this.minute=0;
         this.hour++;
      }                
   }
   public void addFiveMinutes()
   {
      this.minute = this.minute + 5;
   }
   public void addHour()
   {
      if(this.hour<24)
      {
         this.hour++;    
      }
   }
   public void addTwelveHours()
   {
      this.hour = this.hour + 12; 
   }
   private void checkTime()
   {
      if(this.minute<=59)
      {
         this.minute++;    
      }
      if(this.hour<=24)
      {
         this.hour++;
      }
   }
   public void displayTime()
   {                    
      System.out.println((hour + ":" + minute));
   }
   public String toString()
   {
      return hour+ ":" +minute;   
   }
}

 

 

as for your question would this help as i think your just wanting to change the format of the output ?

 

SimpleDateFormat sdf = new SimpleDateFormat("HH:MM a");

 

source : http://stackoverflow.com/questions/13811224/java-display-current-time

I acutally figured it out

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4210559
Share on other sites

Link to post
Share on other sites

I acutally figured it out

okay just a hint if you really want to start with coding start now to write it neatly once you learn how to do it , trouble shooting is way faster. programs will get why more complected and you will need to know what it does at every point just because it works does not meen its fine make it easier to read and you will make less mistakes later :)

Needs Update

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4210589
Share on other sites

Link to post
Share on other sites

Didn't code for years, so sorry for any mistakes, but methods I would change:

	//There is no point of having two constructors, one with defined default values will work.        public Clock(int minutes = 0, int hours = 0){		this.minutes = minutes;		this.hours = hours;	}	//check if minutes are below 59 and call a function to add an hour if needed	public void addMinute(){		if(this.minutes < 59) {			this.minutes++;		} else {			this.minutes = 0;			addHour();		}	}	public void addHour(){		if(this.hours < 24) {			this.hours++;		} else {			this.hours = 0;		}	}        //probably not the smartest way to write it (regarding resources, but easiest), if you need add 5 minutes, why not to add a minute five times? 	public void addFiveMinutes(){		for(int i = 0, i < 5, i++) {			addMinute();		}	}        //same dirty trick with hours	public void addTwelveHours(){		for(int i = 0, i < 12, i++) {			addHour();		}	}        //check if minutes are below 10 to add missing 0	public void displayTime(){		if(this.minutes < 10) {			string min = "0" + this.minutes;		} else {			string min = this.minutes;		}		System.out.println(hour +":"+min);	}

I am sorry for my english.

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4210626
Share on other sites

Link to post
Share on other sites

 

Didn't code for years, so sorry for any mistakes, but methods I would change:

	//There is no point of having two constructors, one with defined default values will work.        public Clock(int minutes = 0, int hours = 0){		this.minutes = minutes;		this.hours = hours;	}	//check if minutes are below 59 and call a function to add an hour if needed	public void addMinute(){		if(this.minutes < 59) {			this.minutes++;		} else {			this.minutes = 0;			addHour();		}	}	public void addHour(){		if(this.hours < 24) {			this.hours++;		} else {			this.hours = 0;		}	}        //probably not the smartest way to write it (regarding resources, but easiest), if you need add 5 minutes, why not to add a minute five times? 	public void addFiveMinutes(){		for(int i = 0, i < 5, i++) {			addMinute();		}	}        //same dirty trick with hours	public void addTwelveHours(){		for(int i = 0, i < 12, i++) {			addHour();		}	}        //check if minutes are below 10 to add missing 0	public void displayTime(){		if(this.minutes < 10) {			string min = "0" + this.minutes;		} else {			string min = this.minutes;		}		System.out.println(hour +":"+min);	}

When i write private void checktime, it says checktime is not being used. why is that?

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4210763
Share on other sites

Link to post
Share on other sites

When i write private void checktime, it says checktime is not being used. why is that?

You wrote method (checktime) that's not being used anywhere. It's not a fatal error, code should compile, but in this case I don't see point of having this function, because there are enough checking done in addMinute() and addHour().

I am sorry for my english.

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4210797
Share on other sites

Link to post
Share on other sites

You wrote method (checktime) that's not being used anywhere. It's not a fatal error, code should compile, but in this case I don't see point of having this function, because there are enough checking done in addMinute() and addHour().

Oh okay. I'm having trouble figuring this out. I'm trying to output 12:55 but it will not show up only 0:00 would show up. Here's the code that's suppose to output it:

Clock yourClock = new Clock(12,55);System.out.print("Your Clock: \t\t\t");yourClock.displayTime();
Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4212220
Share on other sites

Link to post
Share on other sites

 

Oh okay. I'm having trouble figuring this out. I'm trying to output 12:55 but it will not show up only 0:00 would show up. Here's the code that's suppose to output it:

Clock yourClock = new Clock(12,55);System.out.print("Your Clock: \t\t\t");yourClock.displayTime();

 

What does your constructor and displayTime method look like?

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4212527
Share on other sites

Link to post
Share on other sites

What does your constructor and displayTime method look like?

oh now i got it to work, but now its printing 55:012 for some reason.

My constructor is:

public Clock(){                minute = 0;                hour = 0;                }
public void displayTime(){                                    if(this.minute>59){                System.out.println((hour%60)+ ":0" +(minute%60));                    }                if(this.hour>24){                System.out.println((hour%60)+ ":0" +(minute%60));                }else                if(this.minute<10){                System.out.println((hour+ ":0" +minute));                    }                }
Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4212571
Share on other sites

Link to post
Share on other sites

 

oh now i got it to work, but now its printing 55:012 for some reason.

My constructor is:

public Clock(){                minute = 0;                hour = 0;                }
public void displayTime(){                                    if(this.minute>59){                System.out.println((hour%60)+ ":0" +(minute%60));                    }                if(this.hour>24){                System.out.println((hour%60)+ ":0" +(minute%60));                }else                if(this.minute<10){                System.out.println((hour+ ":0" +minute));                    }                }

 

Wrong constructor. What is the content of the Clock(int, int) constructor?

Your displayMethod method is not correct. The first two if statements should never be allowed to be true, if one of them is true then you have made a mistake in your addMinute/addHour methods. But more importantly is the time will only be displayed when the minutes is less than 10. If the time is 12:30 your displayTime method will do nothing.

Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4212636
Share on other sites

Link to post
Share on other sites

Wrong constructor. What is the content of the Clock(int, int) constructor?

Your displayMethod method is not correct. The first two if statements should never be allowed to be true, if one of them is true then you have made a mistake in your addMinute/addHour methods. But more importantly is the time will only be displayed when the minutes is less than 10. If the time is 12:30 your displayTime method will do nothing.

So i should delete those two statements?

public Clock(int minute, int hour){                this.minute = minute;                this.hour = hour;                }
Link to comment
https://linustechtips.com/topic/309430-how-do-i-format-this/#findComment-4212645
Share on other sites

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

×