Mike S
Mike S

Reputation: 23

Java Clock Assignment

For my assignment we are suppose to make a clock. We need variables of hours, minutes, and seconds and methods like setHours/getHours, setMinutes/getMinutes, setSeconds/getSeconds. I also need a addClock() method to make the sum of two clock objects and a tickDown() method which decrements the clock. Also I need a tick() method that increments a Clock object by one second.

Here is what I have so far...

public class Clock {

   private int hr; //store hours
   private int min; //store minutes
   private int sec; //store seconds

   // Default constructor

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

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

   public void setClock (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 addClock( Clock secondClock ) {
      this.sec += secondClock.getSeconds();
      this.min += secondClock.getMinutes();
      //add overflow to minutes from seconds
      this.min +=(int)(this.sec/60);
      //update seconds 
      this.sec = this.sec % 60;
      this.hr += secondClock.getHours();
      //add overflow to minutes from seconds
      this.hr +=(int)(this.min/60);
      //update minutes
      this.min = this.min % 60;
      //adjust hours
      this.hr = this.hr % 24;
   }

   public void tick(){
      this.sec += 1;
      //add overflow to minutes from seconds
      this.min +=(int)(this.sec/60);
      //update seconds 
      this.sec = this.sec % 60;
      //add overflow to minutes from seconds
      this.hr +=(int)(this.min/60);
      //update minutes
      this.min = this.min % 60;
      //adjust hours
      this.hr = this.hr %24;
   }

   public void tickDown(){
      this.sec -= 1;
      if(this.sec <0){
         this.sec+=60;
         this.min-=1;
      }
      if(this.min<0){
         this.min+=60;
         this.hr-=1;
      }
      if(this.hr<0){
         this.hr+=24;
      }
   }
}

Upvotes: 1

Views: 30100

Answers (4)

Sanjar Djuraev
Sanjar Djuraev

Reputation: 1

public Clock() {
    this.hours = 12;
    this.minutes = 0;
    this.seconds = 0;
}

public Clock(int seconds) {
    this(seconds/3600, seconds%3600/60, seconds%3600%60);

}

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

public void setClock(int seconds) {
    this.seconds += seconds;
    this.minutes += minutes;
    this.minutes +=(int)(this.seconds/60);
    this.seconds = this.seconds % 60;
    this.hours += hours;
    this.hours +=(int)(this.minutes/60);
    this.minutes = this.minutes % 60;
    this.hours = this.hours % 24;
}


public int getHours() {
    return hours;
}

public void setHours(int hours) {
    this.hours = hours;
}

public int getMinutes() {
    return minutes;
}

public void setMinutes(int minutes) {
    this.minutes = minutes;
}

public int getSeconds() {
    return seconds;
}

public void setSeconds(int seconds) {
    this.seconds = seconds;
}

public void tick() {
    this.seconds += 1;
    this.minutes +=(int)(this.seconds/60);
    this.seconds = this.seconds % 60;
    this.hours +=(int)(this.minutes/60);
    this.minutes = this.minutes % 60;
    this.hours = this.hours %24;
}

public void tickDown() {
    this.seconds -= 1;
    if(this.seconds <0){
        this.seconds+=60;
        this.minutes-=1;
    }
    if(this.minutes<0){
        this.minutes+=60;
        this.hours-=1;
    }
    if(this.hours<0){
        this.hours+=24;
    }
}

public void addClock(Clock clock) {
    this.seconds += clock.getSeconds();
    this.minutes += clock.getMinutes();
    this.minutes +=(int)(this.seconds/60);
    this.seconds = this.seconds % 60;
    this.hours += clock.getHours();
    this.hours +=(int)(this.minutes/60);
    this.minutes = this.minutes % 60;
    this.hours = this.hours % 24;
}

public Clock subtractClock(Clock clock) {

    if(this.seconds < clock.getSeconds()){
        --this.minutes;
        this.seconds += 60;
    }
    if(this.minutes < clock.getMinutes()){
        --this.hours;
        this.minutes += 60;
    }
    this.seconds -= clock.getSeconds();
     this.minutes -= clock.getMinutes();
     this.hours -= clock.getHours();
    return new Clock(hours,minutes,seconds);
}

public String toString() {
    String result = "(";
    String hh = "";
    String mm = "" + ":";
    String ss = "" + ")";
    if (hours < 10) {
        hh = "0" + String.valueOf(hours) + ":";
    }else {
        hh = String.valueOf(hours) +  ":";
    }
    if (minutes < 10) {
        mm = "0" + String.valueOf(minutes) + ":";
    }else {
        mm = String.valueOf(minutes) + ":";
    }
    if (seconds < 10) {
        ss = "0" + String.valueOf(seconds) +")";
    }else {
        ss = String.valueOf(seconds) +")";
    }
    result += (hh+mm+ss);
    return result;
}

Upvotes: 0

Aubin
Aubin

Reputation: 14853

You have to learn more about the Java language. ;-)

if( condition ) { /* no ';' here */
   <block when true>
}
else {
   <block when false>
}

The main is missing, I propose

public static void main( String args[] ) {
   Clock c1 = new Clock( 9, 59, 59 );
   c1.tickSeconds();
   System.err.println( c1 ); // You have to overload toString()
}

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34367

You can write addClock() as below:

    public void addClock(Clock secondClock){
       this.second += secondClock.getSecond();
       this.minute+= secondClock.getMinute();
       //add overflow to minutes from seconds
       this.minute+=(int)(this.second/60);
       //update seconds 
        this.second = this.second % 60;
       this.hour += secondClock.getHour();
       //add overflow to minutes from seconds

        this.hour+=(int)(this.minute/60);
       //update minutes
        this.minute= this.minute% 60;

        //adjust hours
        this.hour = this.hour%24;
    }

Similarly you can write tick and tickDown methods, with only difference that you will not have any argument in the method and you need to readjust minutes and hours after adding subtracting 1 from seconds

  public void tick(){
       this.second += 1;
       //add overflow to minutes from seconds
       this.minute+=(int)(this.second/60);
       //update seconds 
       this.second = this.second % 60;
       //add overflow to minutes from seconds
        this.hour+=(int)(this.minute/60);
       //update minutes
        this.minute= this.minute% 60;
        //adjust hours
        this.hour = this.hour%24;
    }

  public void tickDown(){
       this.second -= 1;
       if(this.second <0){
          this.second+=60;
          this.minute-=1;
        }
       if(this.minute<0){
          this.minute+=60;
          this.hour-=1;
        }
       if(this.hour<0){
          this.hour+=24;
       }
    }

Add main method as :

   public static void main(String[]  args){
      Clock clock1 = new Clock(2,4,7);
      Clock clock2 = new Clock(8,26,57);
      clock1.tick();
      int newSeconds = clock1.getSeconds();
      //validate it should be 8 now

      clock2.tickDown();
      newSeconds = clock1.getSeconds();
      //validate it should be 56 now

      clock1.addClock(clock2);
      //get Hour, minute and second from clock1 and validate
      int newHour = clock1.getHours();
      int nuwMinute = clock1.getMinutes();
      newSeconds = clock1.getSeconds();
      //validate if they are with the right expected value or not
  }

Upvotes: 3

Htaras
Htaras

Reputation: 879

You can simplify the tick logic by just maintaining the ticks. Extract hour, minutes and seconds from the tick values.

public Clock
{
 long ticks = 0;
 public void tick()
 {
   ticks++;
 }

 public void tickDown()
 {
  ticks--;
 }

 public int getSeconds()
 {
   return ticks % 60;
 }

 public int getMinutes()
 {
   return (ticks / 60) % 60;
 }

 public int getHours()
 {
      return (ticks / (60 * 60)) % 24;
 }
 }

Upvotes: 1

Related Questions