92.5k views
4 votes
Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show the time in New York, three time zones ahead. Which methods did you override

User AlienDeg
by
3.5k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

The following code was written in Java. It uses the LocalTime import to detect the current time. Then it creates a getHours, getMinutes, and getTime method to correctly print out only the hours and minutes in a simple format. Then it does the same for the WorldClock subclass which takes in the time offset as an int parameter and adds that to the hours in your own timezone.

class Clock {

public String getHours() {

String hours = java.time.LocalTime.now().toString().substring(0,2);

return hours;

}

public String getMinutes() {

String min = java.time.LocalTime.now().toString().substring(3,5);

return min;

}

public String getTime() {

String time = getHours() + ":" + getMinutes();

return time;

}

}

class WorldClock extends Clock {

int timeZone = 0;

public WorldClock(int timeZone) {

super();

this.timeZone = timeZone;

}

public String getHours() {

String hours = String.valueOf(Integer.parseInt(super.getHours()) + 3);

return hours;

}

public String getTime() {

String time = getHours() + ":" + super.getMinutes();

return time;

}

}

class Test {

public static void main(final String[] args) {

Clock myClock = new Clock();

System.out.println("My Time: " + myClock.getTime());

WorldClock worldClock = new WorldClock(3);

System.out.println("My Time + 3: " + worldClock.getTime());

}

}

Implement a class Clock whose getHours and getMinutes methods return the current time-example-1
User Evernoob
by
3.9k points