Question: //Clock.java public class Clock { private int hour; private int minute; private int second; private static final int hour_value = 24; private static final int
//Clock.java
public class Clock { private int hour; private int minute; private int second;
private static final int hour_value = 24; private static final int minute_value = 60; private static final int second_value = 60; public void setHour(int h) { if(!(0 <= h && h < hour_value)) { h %= hour_value; if(h < 0) { h += hour_value; } } hour = h; } public void setMinute(int m) { if(!(0 <= m && m < minute_value)) { m %= minute_value; if(m < 0) { m += minute_value; } } minute = m; } public void setSecond(int s) { if(!(0 <= s && s < second_value)) { s %= second_value; if(s < 0) { s += second_value; } } second = s; } public int getHour() { return hour; }
public int getMinute() { return minute; }
public int getSecond() { return second; }
public String toString() { return String.format("%02d:%02d:%02d", hour, minute, second); }
public void addSeconds(int s) {
int secondRaw = second + s; int minuteRaw = minute + secondRaw / second_value; int hourRaw = hour + minuteRaw / minute_value;
setSecond(secondRaw); setMinute(minuteRaw); setHour(hourRaw); }
public void addMinutes(int m) {
int minuteRaw = minute + m; int hourRaw = hour + minuteRaw / minute_value;
setMinute(minuteRaw); setHour(hourRaw); }
public void addHours(int h) {
int hourRaw = hour + h;
setHour(hourRaw); }
public static void main(String[] args) { Clock clock = new Clock();
clock.setHour(-1); clock.setMinute(120); clock.setSecond(3);
System.out.println(clock); clock.addHours(1); clock.addMinutes(-100); clock.addSeconds(3000);
System.out.println(clock); } } we need to extend class Clock to a CalendarClock by implementing the information about year, month and day. For month and day, limitation should be set for the value range, and the value of year must be dropped into range [1, 3000], toString method will return a string represent the date and time in format dd/MM/yyyy hh:mm:ss, for example, 08/03/2020 18:30:00 for 6:30 pm of the 8th of March in year 2020. The corresponding get and set methods are required, including also: void addDays(int d) //add d days to the current date, and adjust month and year // where d can only be a positive value. void addMonths(int m) //add m months to the current date, and adjust year // where m can only be a positive value. void addYears(int y) //add y years to the current date. We need to consider the influence of a leaf year. A year is defined as: if year % 100 ==0 and year%400 == 0 year is a leaf year else if year % 4 == 0 year is a leaf year else year is not a leaf year A leaf year means that there are 29 days for February while there are only 28 days in a normal year.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
