In: Computer Science
Using Java:
Create a class that will hold the information about a clock, called Clock. You need to keep minutes, hours and seconds in military time (24-hour) format. Include member functions that will setTime ( this will set the hours, minutes and seconds of the clock), getHours (return the hours), getMinutes (return the minutes) and getSeconds (return the seconds). Another method printTime which will print the time out like this 03:13:09 (hours:minutes:seconds). Overloaded constructors that will take in hours, minutes and seconds or the default constructor that will take in nothing and set hours, minutes and seconds all to 0.
Your class should also include the following methods
incrementSeconds() //A method to increment the time by one second. //The time is incremented by one second. // If the before-increment time is // 23:59:59, the time is reset to 00:00:00.
incrementMinutes() //A method to increment the time by one minute. // The time is incremented by one minute. // If the before-increment time is // 23:59:53, the time is reset to 00:00:53.
incrementHours() //A method to increment the time by one hour. // The time is incremented by one hour. // If the before-increment time is // 23:45:53, the time is reset to 00:45:53.
Show possible calls to do the following
Create a clock that is set to 9:0:0
Create another clock that is set to 9:30:20
Add one hour to the first clock
Add 30 minutes to the second clock
Using your clock class, create a child class, ClockChild. Create a constructor that calls the parent’s default constructor and an overloaded constructor that takes in hours, minutes and seconds and sets the member variables accordingly. Create a printTime method, for the child. Now it will take in a parameter called typeOfClock. This will be an integer that will be either 12 or 24. This tell the function how to print the time, either in military time (the parents type, call the parent method) or in normal type (12-hour) 11:30pm or 9:09am.
Show calls to instantiate two objects. One of the parent type, with the time set to 23:30:30 and one of the child type with the same time set. Show a call to printTime, using both objects.
Given below is the code for question. Please do rate the answer if it helped. Thank you.
Clock.java
----
public class Clock{
private int hours, minutes, seconds;
public Clock() {
setTime(0, 0, 0);
}
public Clock(int h, int m, int s) {
setTime(h, m, s);
}
public void setTime(int h, int m, int s) {
seconds = s;
minutes = m;
hours = h;
}
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
public void printTime() {
String s = "";
if(hours < 10)
s += "0";
s += hours + ":";
if(minutes < 10)
s += "0";
s += minutes + ":";
if(seconds < 10)
s += "0";
s += seconds;
System.out.println(s);
}
public void incrementSeconds() {
seconds++;
if(seconds == 60) {
seconds =
0;
incrementMinutes();
}
}
public void incrementMinutes() {
minutes++;
if(minutes == 60) {
minutes =
0;
incrementHours();
}
}
public void incrementHours() {
hours++;
if(hours == 24) {
hours = 0;
}
}
}
ChildClock.java
----
public class ChildClock extends Clock {
public ChildClock() {
super();
}
public ChildClock(int h, int m, int s) {
super(h, m, s);
}
public void printTime(int type) {
if(type == 12) {
String ampm =
"";
int h =
getHours();
int m =
getMinutes(), s = getSeconds();
if(h >= 12)
{
h -= 12;
ampm = "pm";
}
else{
ampm = "am";
}
String str =
"";
if(h <
10)
str += "0";
str += h +
":";
if(m <
10)
str += "0";
str += m +
":";
if(s <
10)
str+= "0";
str += s;
str += " " +
ampm;
System.out.println(str);
}
else {
super.printTime();
}
}
}
ClockTest.java
-----
public class ClockTest {
public static void main(String[] args) {
Clock c1 = new Clock(9, 0,
0);
Clock c2 = new Clock(9, 30,
20);
System.out.print("c1 = ");
c1.printTime();
System.out.print("c2 = ");
c2.printTime();
System.out.println("Adding 1 hour
to first c1 and 30 mins to c2");
c1.incrementHours();
for(int i = 1; i<= 30;
i++)
c2.incrementMinutes();
System.out.print("c1 = ");
c1.printTime();
System.out.print("c2 = ");
c2.printTime();
Clock c3 = new Clock(23, 30,
30);
ChildClock c4 = new ChildClock(23,
30, 30);
System.out.print("parent clock c3 =
");
c3.printTime();
System.out.print("child clock c4 in
12-hour format = ");
c4.printTime(12);
System.out.print("child clock c4 in
24-hour format = ");
c4.printTime(24);
}
}