Question: Implement a class Clock with getHours and getMinutes methods to return the current time. (Use new java.util.Date.toString() and extract the time from that string.) Also

Implement a class Clock with getHours and getMinutes methods to return the current time. (Use new java.util.Date.toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling your getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, from California, a new WorldClock(3) will show the time in New York, three time zones ahead. Your WorldClock subclass should override some methods from Clock, but should not override getTime.

Hint: How to extract integer hour and minute from Date() object:

public class Clock { private int hour,minute; public Clock(){ String s=new java.util.Date().toString(); hour=Integer.parseInt(s.substring(11,13)); minute=Integer.parseInt(s.substring(14,16)); } public int getMinute(){ return minute; } public int getHour(){ return hour; } public static void main(String[] args){ Clock c=new Clock(); System.out.printf("%02d:%02d ",c.getHour(),c.getMinute()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!