Write a java program. Write a Clock class according to the following specificati
ID: 3778166 • Letter: W
Question
Write a java program.
Write a Clock class according to the following specifications:
Fields: private int seconds; private int minutes; private int hours;
Constructors: public Clock(int s, int m, int h) Initializes the instance variables seconds, minutes, and hours to the parameters s, m, and h, respectively.
public Clock(Clock r)
Copy constructor
Instance methods:
public void setTime(int s, int m, int h)
Sets the instance variables seconds, minutes, and hours to the parameters s, m, and h, respectively.
public void tick()
Adds 1 to seconds. seconds wraps around at 60 seconds to 0, in which case tick adds 1 to minutes. minutes similarly wraps around at 60 minutes to 0, in which case tick adds 1 to hours. hours similarly wraps around at 24 hours to 0.
public String toString()
returns a string containing the values of seconds, minutes, and hours in the follow- ing format: seconds = ___ minutes = ___ hours = ___
private void incrementMinutes()
Adds 1 to minutes. minutes and hours wrap around at 60 and 24, respectively.
private void incrementHours()
Add 1 to hours. hours wraps around at 24 hours to 0.
Use named constants to hold the wrap-around values for seconds, minutes, and hours (60, 60, and 24). Write a test program that constructs a Clock object, sets sec- onds, minutes, and hours to 58, 59, and 23, respectively. Then call tick four times. Display the time after each call of tick. Then create a copy with the copy constructor. Display the time in the new clock.
Can you send me a screenshot out the output.
Explanation / Answer
//Class Clock defined
class Clock
{
//Private data member
private int seconds;
private int minutes;
private int hours;
//Parameterized Constructor
public Clock(int s, int m, int h)
{
seconds = s;
minutes = m;
hours = h;
}
//Copy constructor
public Clock(Clock r)
{
seconds = r.seconds;
minutes = r.minutes;
hours = r.hours;
}
//Set Time method
public void setTime(int s, int m, int h)
{
seconds = s;
minutes = m;
hours = h;
}
//Tick method
public void tick()
{
//Checks for the second if 60 set the second to 0 and increment minute
if(seconds == 60)
{
seconds = 0;
incrementMinutes();
}
else
seconds++;
//After increment the minute if it is 60 then set the minute to 60 and increment the hour
if(minutes == 60)
{
minutes = 0;
incrementHours();
}
//if after increment of hour it is 24 then set it to 0
if(hours == 24)
{
hours = 0;
}
}
//Returns the String as per the format
public String toString()
{
String ss = "";
ss += "Seconds = " + String.valueOf(seconds) + " Minutes = " + String.valueOf(minutes) + " Hours = " + String.valueOf(hours);
return ss;
}
//Increment minute method
private void incrementMinutes()
{
//If minute is 60 set the minute to 0 and increment the hour
if(minutes == 60)
{
minutes = 0;
incrementHours();
}
else
minutes++;
//If after incrementing the hour it is 24 then set it to 0
if(hours == 24)
{
hours = 0;
}
}
//Increment the hour
private void incrementHours()
{
//If the hour is 24 then set it to 0
if(hours == 24)
{
hours = 0;
}
else
hours++;
}
}
//Driver class
public class ClockDemo
{
public static void main(String ss[])
{
//Creates an object of Clock class with parameterized constructor
Clock cc = new Clock(58, 59, 23);
//Displays the initial clock value
System.out.println("Initial Clock: " + cc.toString());
//Calls the Tick method 4 times and displays the clock value
for(int c = 1; c <= 4; c++)
{
cc.tick();
System.out.println(c + " : " + cc.toString());
}
//Creates an object of Clock class with copy constructor
Clock cc1 = new Clock(cc);
System.out.println("After Using Copy Constructor: " + cc1.toString());
}
}
Oputput:
Initial Clock: Seconds = 58 Minutes = 59 Hours = 23
1 : Seconds = 59 Minutes = 59 Hours = 23
2 : Seconds = 60 Minutes = 59 Hours = 23
3 : Seconds = 0 Minutes = 0 Hours = 0
4 : Seconds = 1 Minutes = 0 Hours = 0
After Using Copy Constructor: Seconds = 1 Minutes = 0 Hours = 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.