You will be writing portions of a Java class that models a very simple clock. Yo
ID: 3574966 • Letter: Y
Question
You will be writing portions of a Java class that models a very simple clock. You will also be provided with two different "front ends" that use this same class as a plug-in - a simple program that loops through the time for a full hour of time, displaying each passing second on your screen, and a slightly more complex program that displays the time in a window and allows you to update it with a click of a button. If you have coded your clock class correctly, these two programs provided will just work when you run them.
https://gist.github.com/anonymous/3ab9f7cddebb753b8477a4bad4e70980
https://gist.github.com/anonymous/da1a20a48d6abca716779b6caa17fb46
You should see output like the following:
Clock starts at time: 12:00:00 AM
Clock has been set to time: 11:59:00 PM
TICK: 11:59:01 PM
TICK: 11:59:02 PM
TICK: 11:59:03 PM
TICK: 11:59:04 PM
TICK: 11:59:05 PM
TICK: 11:59:06 PM
TICK: 11:59:07 PM
... Many lines of output like the above ...
TICK: 12:58:58 AM
TICK: 12:58:59 AM
TICK: 12:59:00 AM
Clock finally reads: 12:59:00 AM
Explanation / Answer
Here is the code. I will also paste this as a comment in github gist.
import java.text.DecimalFormat;
class SimpleClock {
/* -------- Private member variables --------------------- */
private int hours;
private int minutes;
private int seconds;
private boolean morning;
/* -------- Constructor --------------------------------- */
/**
* The constructor should set the intial value of the clock to 12:00:00AM.
*/
public SimpleClock() {
hours = 12;
minutes = seconds = 0;
morning = true;
}
/* --------- Instance methods ------------------------- */
/**
* Sets the time showing on the clock.
*
* @param hh
* - the hours to display
* @param mm
* - the minutes to display
* @param ss
* - the seconds to display
* @param morning
* - true for AM, false for PM
*/
public void set(int hh, int mm, int ss, boolean morning) {
this.hours = hh;
this.minutes = mm;
this.seconds = ss;
this.morning = morning;
}
/**
* Advances the clock by 1 second. Make sure when implementing this method
* that the seconds "roll over" correctly - 11:59:59AM should become
* 12:00:00PM for example.
*/
public void tick() {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
hours++;
}
if (hours == 12 && minutes == 0 && seconds == 0) {
morning = !morning;
}
if (hours == 13) {
hours =1;
morning = !morning;
}
}
/**
* Returns a string containing the current time formatted as a digital clock
* format. For example, midnight should return the string "12:00:00AM" while
* one in the morning would return the string "1:00:00AM" and one in the
* afternoon the string "1:00:00PM".
*
* @return - the current time formatted in AM/PM format
*/
public String time() {
String meridian;
if (morning)
meridian = "AM";
else
meridian = "PM";
DecimalFormat formatter = new DecimalFormat("00");
return hours + ":" + formatter.format(minutes) + ":" + formatter.format(seconds) + meridian;
}
}
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.