Question

JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

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


}

1 0
Add a comment Improve this question Transcribed image text
Answer #1

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
this.hours = hours;
this.minutes = minutes;


}

/**
* gets current time composed of hours and minutes
* @return time in string;
*/
public String getTime()
{
// Complete this method
return getHours() + ":" + getMinutes();

}

/**
* gets hours
* @return hours of current time in string
*/
public String getHours()
{
// Complete this method
final int Hour_START = 11;
return currentTime().substring(HOUR_START, HOUR_START + 2);  


}

/**
* gets minutes
* @return hours of current time in string
*/
public String getMinutes()
{
// Complete this method
final int MINUTE_START = 14;
return currentTime().substring(MINUTE_START, MINUTE_START + 2);


}

/**
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
   private int offset;
   public WorldClock(int offset)
   {
           this.offset = offset;
   }
   public String getHours()
   {
       final int Hour_START = 11;
       return currentTime().substring(HOUR_START, HOUR_START + 2);  
   }
   public String getMinutes()
   {
       final int MINUTE_START = 14;
       return currentTime().substring(MINUTE_START, MINUTE_START + 2);  
   }

}

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

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) 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 getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Use c++ to code: Problem 1 Implement a class Clock whose get hours and get_minutes member functions return the current...

    Use c++ to code: Problem 1 Implement a class Clock whose get hours and get_minutes member functions return the current time at your location. To get the current time, use the following code, which requires that you include the <ctime>header time_t current_time time (e); ta local time - localtime(current, tine)i int hours -local_time->tm hour int minutes-local time->tm_min; Also provide a get_time member function that returns a string with the hours and minutes by calling the get_hours and get_minutes functions. Provide...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now...

    In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now modify the Rental and RentalDemo classes as follows: Modify the Rental class to include an integer field that holds an equipment type. Add a final String array that holds names of the types of equipment that Sammy's rents—personal watercraft, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Include get and set methods for the integer equipment type field. If the argument passed...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT