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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started