Question: Using Java we implemented a class Clock and we need to extend the class Clock to a TimezoneClock by implementing the following components: Use an
Using Java we implemented a class Clock and we need to extend the class Clock to a TimezoneClock by implementing the following components: Use an integer to represent the time zone, for example, 10 to indicate UTC+10:00, which is PH time, and -5 to indicate UTC-5:00, which is US time. toString method will return a time with timezone information, for example, 14:40:00 UTC+10:00 representing 2:40 pm at UTC+10:00, which is equivalent to 12:20:00 UTC+8:00. (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); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
