-4

I'm trying to add minutes and hours to a clock, but i'm not sure how to do that. Here's what i have:

    class Clock{
        private int minutes;
        private int hours;

        public Clock(){
           minutes = 00;
           hours = 0;
        }

        public Clock(int minutes, int hours){
            this.minutes = minutes;
            this.hours = hours;
        }

        public double minutes(){
            return minutes;    
        }    

        public double hours(){
            return hours;    
        }

        public void addMinutes(){

        }
        public void addFiveMinutes(){

        }
        public void addHours(){

        }
        public void addTwelveHours(){

        }

    }

I'm not sure on what variables to put

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
AznMan
  • 1
  • 3
  • 1
    Think of time more like the number of minutes (or seconds or milliseconds) since midnight. This way, you would simply add to a single value. As required, you would then calculate the number of hours and minutes the single `int` value represented. Or just use Java 8's Time API – MadProgrammer Feb 11 '15 at 04:35
  • Kinda like this -> `Date date = new Date(97, 1, 23);` ---> `long diff = date.getTime();` – Tushar Gogna Feb 11 '15 at 04:36
  • Would minutes++ or hours++ work? – AznMan Feb 11 '15 at 04:38
  • How do you add minutes and hours without Java? Also, perhaps you shouldn't widen `minutes` and `hours` to `double` (from `int`). – Elliott Frisch Feb 11 '15 at 04:39
  • I would keep the clock internal value only in minutes. When displaying the minutes will be divided by 60 to get hours and reminder would be the minutes left. The clock will be reset when the minuts value is 1440 (24 * 60) – MaxZoom Feb 11 '15 at 04:40
  • @AznMan Yes, `minutes++` and `hours++`, but now you need to ensure that both values are within their allowable range. It would be much simpler to do with a "base" value (in minutes for example) then having two values you need to keep in range. What happens if you add 100 minutes? – MadProgrammer Feb 11 '15 at 04:44

1 Answers1

0

This has the smell of a homework assignment, so I'll offer a hint but not a complete answer:

   public void addMinutes(double minutes){
       this.minutes = this.minutes + minutes;
   }

   public void addFiveMinutes(){
       addMinutes(5.0)
   }

You will need to work out how to handle the hours for yourself - it's not hard.

In a real-world problem, you'd just use the Calendar class that is built into the core java library.

pojo-guy
  • 966
  • 1
  • 12
  • 39