|
/* |
|
* Tony Silvestri |
|
* Clock class emulates a normal clock |
|
* 2/2/16 |
|
* silvestri@stcc.edu |
|
* CSC-112 Intermediate Java |
|
*/ |
|
|
|
public class Clock extends Object implements Comparable<Clock> { |
|
private int hr; |
|
private int min; |
|
private int sec; |
|
private static int count; |
|
|
|
public static Clock midNight = new Clock(0, 0, 0); |
|
public static Clock noon = new Clock(12, 0, 0); |
|
|
|
// ************************************************** |
|
|
|
public Clock(int hr, int min, int sec) { |
|
this.setHr(hr); |
|
this.setMin(min); |
|
this.setSec(sec); |
|
Clock.count++; |
|
} |
|
|
|
public Clock(int hr, int min) { |
|
this(hr, min, 0); |
|
} |
|
|
|
public Clock(int hr) { |
|
this(hr, 0, 0); |
|
} |
|
|
|
public Clock() { |
|
this(0, 0, 0); |
|
} |
|
|
|
// ************************************************** |
|
|
|
public static Clock factory(int hr, int min, int sec) { |
|
return new Clock(hr, min, sec); |
|
} |
|
|
|
public static int getCount() { |
|
return count; |
|
} |
|
|
|
// ************************************************** |
|
|
|
public int getHr() { |
|
return this.hr; |
|
} |
|
|
|
public void setHr(int hr) { |
|
if (hr >= 0 && hr <= 23) |
|
this.hr = hr; |
|
else |
|
System.err.println("ERROR: Bad hour assignment attempt = " + hr); |
|
} |
|
|
|
public int getMin() { |
|
return this.min; |
|
} |
|
|
|
public void setMin(int min) { |
|
if (min >= 0 && min <= 59) |
|
this.min = min; |
|
else |
|
System.err.println("ERROR: Bad minute assignment attempt = " + min); |
|
} |
|
|
|
public int getSec() { |
|
return this.sec; |
|
} |
|
|
|
public void setSec(int sec) { |
|
if (sec >= 0 && sec <= 59) |
|
this.sec = sec; |
|
else |
|
System.err |
|
.println("ERROR: Bad seconds assignment attempt = " + sec); |
|
} |
|
|
|
// ************************************************** |
|
|
|
void tick() { |
|
this.incrementSeconds(); |
|
} |
|
|
|
private void incrementSeconds() { |
|
this.sec++; |
|
if (this.sec > 59) { |
|
this.sec = 0; |
|
incrementMinutes(); |
|
} |
|
} |
|
|
|
private void incrementMinutes() { |
|
this.min++; |
|
if (this.min > 59) { |
|
this.min = 0; |
|
incrementHours(); |
|
} |
|
} |
|
|
|
private void incrementHours() { |
|
this.hr++; |
|
if (this.hr > 23) { |
|
this.hr = 0; |
|
} |
|
} |
|
|
|
// *********************************************** |
|
|
|
// public String toString() { |
|
// return this.hr + ":" + this.min + ":" + this.sec; |
|
// } |
|
// |
|
// public String toString() { |
|
// String retval = ""; |
|
// if (this.hr < 10) |
|
// retval += "0"; |
|
// retval += this.hr + ":"; |
|
// if (this.min < 10) |
|
// retval += "0"; |
|
// retval += this.min + ":"; |
|
// if (this.sec < 10) |
|
// retval += "0"; |
|
// retval += this.sec; |
|
// return retval; |
|
// } |
|
// |
|
// public String toString() { |
|
// StringBuilder retval = new StringBuilder(); |
|
// if (this.hr < 10) |
|
// retval.append("0"); |
|
// retval.append(this.hr); |
|
// retval.append(":"); |
|
// if (this.min < 10) |
|
// retval.append("0"); |
|
// retval.append(this.min); |
|
// retval.append(":"); |
|
// if (this.sec < 10) |
|
// retval.append("0"); |
|
// retval.append(this.sec); |
|
// return retval.toString(); |
|
// } |
|
|
|
public String toString() { |
|
return String.format("%02d:%02d:%02d", this.hr, this.min, this.sec); |
|
} |
|
|
|
public void finalize() { |
|
Clock.count--; |
|
} |
|
|
|
public boolean equals(Clock c) { |
|
if (this.hr != c.hr) |
|
return false; |
|
if (this.min != c.min) |
|
return false; |
|
if (this.sec != c.sec) |
|
return false; |
|
return true; |
|
} |
|
|
|
@Override |
|
public boolean equals(Object obj) { |
|
if (this == obj) |
|
return true; |
|
if (obj == null) |
|
return false; |
|
if (getClass() != obj.getClass()) |
|
return false; |
|
Clock other = (Clock) obj; |
|
if (hr != other.hr) |
|
return false; |
|
if (min != other.min) |
|
return false; |
|
if (sec != other.sec) |
|
return false; |
|
return true; |
|
} |
|
|
|
@Override |
|
public int compareTo(Clock c) { |
|
int temp = this.hr - c.hr; |
|
if (temp != 0) |
|
return temp; |
|
temp = this.min - c.min; |
|
if (temp != 0) |
|
return temp; |
|
temp = this.sec - c.sec; |
|
return temp; |
|
} |
|
|
|
} |
This comment has been minimized.
bytecodeman commentedFeb 3, 2018
Updated the Clock class with the complete code.
Also added 2 more tester programs.