Question: Help with this program Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also
|
Help with this program Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include 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 NewYork, three time zones ahead. Include an Alarm feature in the Clock class. When setAlarm(hours, minutes), the clock stores |
| the alarm. When you call getTime and the alarm time has been reached or exceeded, return the time followed by the string Alarm and clear the alarm. I have provided a ClockDemo class which can be used to test the program. Do not change it when you submit it. I provided a Clock class which has shells for all the methods which you should fill in. The same applies to the WorldClock class. ==================================================== Clock.java 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. * * * Add an alarm feature to the Clock class. * When setAlarm(hours, minutes) is called, the clock * stores the alarm. When you call getTime, and the alarm * time has been reached or exceeded, return the time followed * by the string "Alarm" and clear the alarm. */ public class Clock { . . . /** * Sets the alarm. * @param hours hours for alarm * @param minutes minutes for alarm */ public void setAlarm(int hours, int minutes) { // Complete this method . . . } /** * gets current time composed of hours and minutes * @return time in string; */ public String getTime() { // Complete this method . . .
} /** * gets hours * @return hours of current time in string */ public String getHours() { // Complete this method . . . } /** * gets minutes * @return hours of current time in string */ public String getMinutes() { // Complete this method . . . } /** Returns the current Date and Time as a String. */ private String currentTime() { return LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()).toString(); } }
ClockDemo.java /** * Tests the alarm feature of the Clock class. */ public class ClockDemo { public static void main(String[] args) { //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());
//test the AlarmClock System.out.println(""); System.out.println("Testing AlarmClock"); Clock clock = new Clock(); System.out.println("Hours:" + clock.getHours()); System.out.println("Minutes: " + clock.getMinutes()); System.out.println("Time: " + clock.getTime()); //test AlarmClock with alarm int h = Integer.parseInt(clock.getHours()); int m = Integer.parseInt(clock.getMinutes()); clock.setAlarm(h, m - 1); System.out.println("Time:" + clock.getTime()); //test to see if the Alarm is cleared. System.out.println("Time: " + clock.getTime()); } }
WorldClock.java /** * 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 . . .
}
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
