Question: I have written the following code for clock. It is giving wrong output. I can't seem to fix it. The instructions are after the code.
I have written the following code for clock. It is giving wrong output. I can't seem to fix it. The instructions are after the code. Can you please help me fix it?
public class Clock {
//private int hours, minutes, seconds;
private int totalSeconds;
private static int numClocks = 0;
public final int ID;
public static boolean use24HourFormat = false;
public Clock(int hours, int minutes, int seconds) {
setHours(hours);
setMinutes(minutes);
setSeconds(seconds);
numClocks++;
ID = numClocks;
}
public Clock(int hours, int minutes) {
this(hours, minutes, 0);
}
public Clock(int hours) {
this(hours, 0);
}
public Clock() {
this(0);
}
int getHours() {
return totalSeconds/3600;
}
int getMinutes() {
return (totalSeconds/60)%60;
}
int getSeconds() {
return totalSeconds%60;
}
void setHours(int h) {
if (h > 23)
h = 23; // or hours %= 24;
if (h < 0)
h = 0;
totalSeconds = h*3600+getMinutes()*60+getSeconds();
}
void setMinutes(int m) {
if (m> 59)
m = 59;
if (m < 0)
m = 0;
totalSeconds =m*60+getSeconds() ;
}
void setSeconds(int s) {
if (s > 59)
s = 59;
if (s < 0)
s = 0;
//this.seconds =seconds;
totalSeconds = s;
}
void incrementHours() {
if (getHours() == 23)
setHours(0);
else setHours(getHours()+1);
}
void incrementMinutes() {
if (getMinutes() == 59) {
setMinutes(0);
incrementHours();
}
else setMinutes(getMinutes()+1);
}
void incrementSeconds() {
if (getSeconds() == 59) {
setSeconds(0);
incrementMinutes();
}
else setSeconds(getMinutes()+1);
}
public String toString() {
boolean am = false;
String minuteString = "" + getMinutes();
if (getMinutes() < 10)
minuteString = "0" + minuteString;
String secondString = "" + getSeconds();
if (getSeconds() < 10)
secondString = "0" + secondString;
String hoursString = null;
if (use24HourFormat) {
if (getHours() <= 11) {
hoursString = "" + getHours();
am = true;
}
else if (getHours() == 12) {
hoursString = "" + getHours();
}
else {
setHours(getHours()- 12);
hoursString = "" + getHours();
}
return getHours() + ":" +
minuteString + ":"
+ secondString +
(am ? " AM" : " PM");
}
return getHours() + ":" + minuteString + ":"+ secondString;
}
public void setToCurrentTime() {
totalSeconds = (int)System.currentTimeMillis()/(1000 % (24*3600) );
}
}
public static void main(String [] args) {
//System.out.println(System.currentTimeMillis()/1000);
Clock m = new Clock(19,59,57);
for (int i = 0; i < 5000; i++) {
try {
System.out.println(m);
Thread.sleep(1000);
m.incrementSeconds();
}
catch (Exception e) {
}
}
m.setHours(10);
System.out.println(m.getHours());
System.out.println(m.getMinutes());
System.out.println(m.getSeconds());
}
}
Following are the instructions to solve the problem:
use a single private variable int totalSeconds for the internal representation of the time. That means getHours, getMinutes and getSeconds will return calculated values rather than just returning the value of a variable. Other than that the class will function mostly like the previous version of Clock. Here are the methods I want you to provide:
Default constructor (Sets totalSeconds to zero.)
Constructor that takes just hours and sets totalSeconds appropriately so that getHours would return that hour while getMinutes and getSeconds would both return 0.
Constructor that takes just hours and minutes and sets totalSeconds appropriately so that getHours would return that hour and getMinutes would return that value of minutes and getSeconds would return 0.
Constructor that takes hours, minutes and seconds and sets totalSeconds appropriately so that getHours would return that hour, getMinutes would return that value of minutes and getSeconds would return that value of seconds.
If illegal values are passed into a constructor then change to the nearest legal value(s).
setters, getters and "incrementers" as in the code I posted Monday.
A toString method that returns the appropriate string containing the time in AM/PM format. We will not use 24 hour format in this version of Clock.
Besides totalSeconds, there will be a private static variables numClocks of type int that starts at 0 and gets incremented in the constructor. There will also be a public constant called ID that is assigned the value of numClocks in the constructor. That way every clock will have a unique id.
One more method: void setToCurrentTime() will change the time to the current time UTC. You must code this one by calling System.currentTimeMillis().
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
