Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clock Project Demo Code by Silvestri
/*
* 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;
}
}
import java.util.Calendar;
/*Nathaniel Murray
* 01/25/17
* Interm JAVA
* Class 01/25/17
* Creates a real time clock
* nmurray3861@student.stcc.edu
*/
public class MurrayClockDriver {
// public static void keepTime(Clock x, long millis) {
// int secs = ((int) (millis / 1000) % 60);
// int mins = ((int) (millis / 60000) % 60);
// int hours = ((int) ((millis / 3600000) % 12) + 7);
// x.setHr(hours);
// x.setMin(mins);
// x.setSec(secs);
// }
public static void keepTime(Clock x, long millis) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
int secs = cal.get(Calendar.SECOND);
int mins = cal.get(Calendar.MINUTE);
int hours = cal.get(Calendar.HOUR);
x.setHr(hours);
x.setMin(mins);
x.setSec(secs);
}
public static void printTime(Clock x) {
System.out.println(x.getHr() + ":" + x.getMin() + ":" + x.getSec());
}
public static void main(String[] args) throws InterruptedException {
Clock clock1 = new Clock(9, 56, 45);
do {
clock1.tick();
//keepTime(clock1, millis);
printTime(clock1);
Thread.sleep(1000);
} while (true);
}
}
public class TestClock {
public static void main(String[] args) {
Clock kitchen = new Clock(23, 59, 50);
Clock livingRoom = new Clock(23, 59, 50);
for (int i = 0; i < 20; i++) {
kitchen.tick(); // Simulate Clock tick
System.out.println(kitchen);
}
System.out.println("Number of Clocks: " + Clock.getCount());
kitchen = livingRoom;
System.gc();
try {
Thread.sleep(100);
}
catch (Exception e) {
}
System.out.println("Number of Clocks: " + Clock.getCount());
Clock a = new Clock(23, 41, 2);
Clock b = new Clock(23, 41, 2);
if (a.compareTo(b) < 0)
System.out.println("a < b");
else if (a.compareTo(b) > 0)
System.out.println("a > b");
else
System.out.println("a == b");
}
}
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
public static void main(String args[]) {
Clock c = new Clock(23, 59, 50);
Timer timer = new Timer();
Task task = new Task(c);
timer.schedule(task, 0, 1000);
}
}
class Task extends TimerTask {
Clock c;
public Task (Clock c) {
this.c = c;
}
public void run() {
c.tick();
System.out.println(c);
}
}
@bytecodeman

This comment has been minimized.

Copy link
Owner Author

commented Feb 3, 2018

Updated the Clock class with the complete code.

Also added 2 more tester programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.