1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30, or 23:59
○ Time is measured in hours (00 – 23) and minutes (00 – 59)
○ Times are ordered from 00:00 (earliest) to 23:59 (latest)
Complete the first constructor of the class Clock
● It takes two arguments: h and m and creates a new clock object whose initial time is h hours and m minutes
● Test cases: Clock clock1 = new Clock(1, 0); System.out.println(clock1);
Complete the second constructor of the class Clock
● It takes one string argument: s
○ s is composed of two digits, followed by a colon, followed by two digits, so the format is HH:MM such as 02:30
○ it creates a new clock object whose initial time is HH hours and MM minutes
● Test cases: Clock clock2 = new Clock("02:30"); System.out.println(clock2); → 02:30
Complete the method toString of the class Clock
● It returns a string representation of this clock, using the format HH:MM
○ that is, the format is the hours (2 digits), followed by a colon, followed by the minutes (2 digits), for example, 00:00 and 23:59
● Test cases: Clock clock1 = new Clock(1, 0); Clock clock2 = new Clock("02:30"); System.out.println(clock1); → 01:00 System.out.println(clock2); → 02:30
Complete the method isEarlierThan of the class Clock
● It returns true if and only if the time on this clock (the current referenced object) is earlier than the time on that clock (in the argument)
● Test cases: Clock clock1 = new Clock(1, 0); Clock clock2 = new Clock("02:30"); System.out.println(clock1.isEarlierThan(clock2)); → true
Complete the method tick of the class Clock
● It adds 1 minute to the time on this clock
○ for example, one minute after 01:00 is 01:01; one minute after 23:59 is 00:00
● Test cases: Clock clock1 = new Clock(1, 0); System.out.println(clock1); → 01:00 clock1.tick(); System.out.println(clock1); → 01:01
Complete the method tock of the class Clock
● It adds delta minute(s) to the time on this clock, where delta is a positive integer
○ for example, 100 minutes after 02:30 is 04:10
● Note that must not use the method in CW1 6.5 tick().
● Test cases: Clock clock2 = new Clock("02:30"); System.out.println(clock2); → 02:30 clock2.tock(100); System.out.println(clock2); → 04:10
Here below, I attached the clock.java class
public class Clock {
private int hours;
private int minutes;
// CW1 6.1
// Creates a clock whose initial time is h hours and m
minutes.
public Clock(int h, int m) {
}
// CW1 6.2
// Creates a clock whose initial time is specified as a string,
using the format HH:MM.
public Clock(String s) {
}
// CW1 6.3
// Returns a string representation of this clock, using the format
HH:MM.
public String toString() {
}
// CW1 6.4
// Is the time on this clock earlier than the time on that
one?
public boolean isEarlierThan(Clock that) {
}
// CW1 6.5
// Adds 1 minute to the time on this clock.
public void tick() {
}
// CW1 6.6
// Adds delta minutes to the time on this clock.
public void tock(int delta) {
}
// Test client
public static void main(String[] args) {
Clock clock1 = new Clock(1, 0);
Clock clock2 = new Clock("02:30");
System.out.println(clock1);
System.out.println(clock2);
System.out.println(clock1.isEarlierThan(clock2));
clock1.tick();
clock2.tock(100);
System.out.println(clock1);
System.out.println(clock2);
}
}
public class Clock {
private int hours;
private int minutes;
public Clock(int h, int m) {
this.hours=h;
this.minutes=m;
}
public Clock(String s) {
// split the string by character ':'
String [] splt=s.split(":");
// set hours
this.hours=Integer.parseInt(splt[0]);
// set minutes
this.minutes=Integer.parseInt(splt[1]);
}
public String toString() {
// return string
return(String.format("%02d:%02d", this.hours,this.minutes));
}
public boolean isEarlierThan(Clock that) {
// get time to minute
int minute1=this.hours*60+this.minutes;
int minute2=that.hours*60+that.minutes;
// if small return true
if(minute1<minute2){
return true;
}
// otherwise false
else{
return false;
}
}
public void tick() {
// increase minute by 1
this.minutes++;
// if minute is 60 then reset to 0
// and increase hour
if(this.minutes==60){
this.hours++;
this.minutes=0;
if(this.hours==24){
this.hours=0;
}
}
}
public void tock(int delta) {
// increase minute by delta
this.minutes+=delta;
// if minute is more 60 then reset to valid
minutes
// and increase hour by minute/60
if(this.minutes>=60){
this.hours+=(this.minutes/60);
this.minutes%=60;
if(this.hours==24){
this.hours=0;
}
}
}
// Test client
public static void main(String[] args) {
Clock clock1 = new Clock(1, 0);
Clock clock2 = new Clock("02:30");
System.out.println(clock1);
System.out.println(clock2);
System.out.println(clock1.isEarlierThan(clock2));
clock1.tick();
clock2.tock(100);
System.out.println(clock1);
System.out.println(clock2);
}
}
Get Answers For Free
Most questions answered within 1 hours.