Source of Clock.java


  1: public class Clock
  2: {
  3:         private int hour;
  4:         private int minute;
  5:         private int second;
  6: 
  7:         //Constructors
  8:         public Clock()
  9:         {
 10:                 //initialize to default values
 11:                 hour = 0;
 12:                 minute = 0;
 13:                 second = 0;
 14:         }
 15: 
 16:         public Clock(int h, int m, int s)
 17:         {
 18:                 setHour(h);
 19:                 setMinute(m);
 20:                 setSecond(s);
 21:         }
 22: 
 23:         /**
 24:         Valid integers for hour is in the range of 0 - 24
 25:         */
 26:         public void setHour(int h)
 27:         {
 28:                 if((h >= 0) && (h <= 24))
 29:                         hour = h;
 30:                 else
 31:                         System.out.println("Fatal error: invalid hour");
 32:         }
 33: 
 34:         /**
 35:         Valid integers for minute is in the range 0 - 59
 36:         */
 37:         public void setMinute(int m)
 38:         {
 39:                 if((m >= 0) && (m <= 59))
 40:                         minute = m;
 41:                 else
 42:                         System.out.println("Fatal error: invalid minute");
 43:         }
 44: 
 45:         /**
 46:         Valid integers for second is in the range 0 - 59
 47:         */
 48:         public void setSecond(int s)
 49:         {
 50:                 if((s >= 0) && (s <= 59))
 51:                         second = s;
 52:                 else
 53:                         System.out.println("Fatal error: invalid second");
 54:         }
 55: 
 56:         //Mutator methods
 57:         public int getHour()
 58:         {
 59:                 return hour;
 60:         }
 61: 
 62:         public int getMinute()
 63:         {
 64:                 return minute;
 65:         }
 66: 
 67:         public int getSecond()
 68:         {
 69:                 return second;
 70:         }
 71: 
 72:         public String getCurrentTime()
 73:         {
 74:                 return toString();
 75:         }
 76: 
 77:         //Facilitator methods
 78:         public String toString()
 79:         {
 80:                 return "The current time is: " + hour + ":" + minute + ":" + second;
 81:         }
 82: 
 83:         public boolean equals(Object o)
 84:         {
 85:                 if(o == null)
 86:                         return false;
 87:                 else if(getClass() != o.getClass())
 88:                         return false;
 89:                 else
 90:                 {
 91:                         Clock otherClock = (Clock) o;
 92:                         return((hour == otherClock.hour) && (minute == otherClock.minute)
 93:                                         && (second == otherClock.second));
 94:                 }
 95: 
 96:         }
 97: }