Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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(); } }

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 . . .

}

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()); } }

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions