Question
Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java
Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, 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 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? (You should not override getTime.)
import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; /** Part I: Implement a class Clock whose getHours, getMinutes and getTime methods return the current time at your location. Return the time as a string.
There are two ways to retrieve the current time as a String:
1) Before Java 8 use new Date().toString() 2) With Java 8, you can use 3 classes in the java.time package: Instant, LocalDateTime and ZoneId. Here's how you do it: String timeString = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()).toString()
With either method, you'll need to extract the hours and minutes as a substring. */ public class Clock { // private int hour, minute; public int getMinute() { String s = new java.util.Date().toString(); return Integer.parseInt(s.substring(14, 16)); } public int getHour() { String s = new java.util.Date().toString(); return Integer.parseInt(s.substring(11, 13)); } public String getTime() { return getHour() + ":" + getMinute(); }
}
------------------------------
/** * PART II. * Provide a subclass of Clock namded 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. You should not override getTime. */ public class WorldClock extends Clock { // Your work goes here . . .
}
-----------------------------
/** * Tests the Clock and WorldClock Classes. */ public class ClockDemo { public static void main(String[] args) { //test Clock System.out.println("Testing Clock class"); Clock clock = new Clock(); System.out.println("Hours: " + clock.getHours()); System.out.println("Minutes: " + clock.getMinutes()); System.out.println("Time: " + clock.getTime());
//test WorldClock System.out.println(""); System.out.println("Testing WorldClock class"); int offset = 3; System.out.println("Offset: " + offset); WorldClock wclock = new WorldClock(offset); System.out.println("Hours: " + wclock.getHours()); System.out.println("Minutes: " + wclock.getMinutes()); System.out.println("Time: " + wclock.getTime()); } }
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