Home > Blockchain >  How to create a simple clock on java? I am having a problem with if-statements
How to create a simple clock on java? I am having a problem with if-statements

Time:09-30

I have create a field, setters, getters for the variables hour, minute and second. Then I created a method called count. Here is the code

public void count(int seconds) {
  while (hours < 3) {
    seconds = this.seconds   seconds;
    seconds = seconds % 60;
    setSecond(seconds);
    if (seconds % 60 > 0) {
      minutes = this.minutes   1;
      minutes = minutes % 60;
      setMinutes(minutes);

      if (minutes == 59) {
        hours = this.hours   1;
        hours = hours % 60;
      }
      System.out.println(toString());
    }
  }
}

The problem I am having is with the if-statement, "if (seconds % 60 > 0)" because no matter how much the second increases, meaning even if it doesn't surpass 60, its modulo will always be greater than 0 so this doesn't work. I wanted to try "if( second > 59) but it doesn't work either because seconds will never be greater than 59. Also keep in mind that I am a new beginner, and thanks beforehand.

CodePudding user response:

public static void clockTranslationFromSeconds(int seconds){
    hour = seconds / 60 / 60;
    minute = seconds / 60 - hour * 60;
    second = seconds - minute * 60 - hour * 60 * 60;
}

i try to keep things as simple as possible. this code still needs some changes to work with your project but i'll leave it up to you.

CodePudding user response:

This definitely looks like a homework problem. One I did myself long time ago. What they are trying to get you to understand is how the modulo operator works and there's a nice [animation here that might help.][1] I would also recommend trying things in JShell just to test things out. For example:

jshell> 3930/3600
$17 ==> 1

If we have 3930 seconds and divide that by 3600 (total seconds in one hour) we get 1 hour. Modulo gives us what remains.

jshell> 3930600
$16 ==> 330

So in this instance we get 330 seconds remaining. Now we can build on that to find out how many minutes are in those 330 seconds.

jshell> 330/60
$26 ==> 5

Which give us 5 minutes and is the same as saying:

jshell> (3930600)/60
$27 ==> 5

Now we can start to put these ideas into a method

  void formatTime(int totalSeconds) {

       int hours = 0;
       int minutes = 0;
       int seconds = 0;

       if(totalSeconds >= 3600 ) {
           hours = totalSeconds/3600;
           minutes = (totalSeconds600)/60;
           seconds = (totalSeconds600)`;
       } else if(totalSeconds < 3600 && totalSeconds >= 60) {
           minutes = (totalSeconds/60);
           seconds = (totalSeconds`);
       } else {
           seconds = totalSeconds;
       }



       System.out.println(hours   (hours == 1 ? " hours " : " hour ") 
                                  minutes   " minutes and "   seconds   " seconds");
  }

This method takes into consideration what happens if we have less than an hour worth of seconds and so on. Then we can test a little to see how we did.

jshell> formatTime(3930)
1 hours 5 minutes and 30 seconds

jshell> formatTime(362)
0 hour 6 minutes and 2 seconds

jshell> formatTime(1)
0 hour 0 minutes and 1 seconds

I'll probably get down voted 1000x for this, but keep trying. Programming seems difficult at first but it's very rewarding when you figure things out. Keep at it, it'll get easier and you'll get better. [1]: https://www.mathsisfun.com/definitions/modulo-operation.html

  •  Tags:  
  • java
  • Related