Question

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 the time in New York, three time zones ahead. Which methods did you override? (You should not override getTime.)

Skeleton Code Below

World Clock-

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


}

Clock-

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
{
/**
* gets hours
* @return hours of current time in string
*/
public String getHours()
{
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()
{
final int MINUTE_START = 14;
return currentTime().substring(MINUTE_START, MINUTE_START + 2);
}

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

/**
Returns the current Date and Time as a String.
*/
private String currentTime()
{
return LocalDateTime.ofInstant(Instant.now(),
ZoneId.systemDefault()).toString();
}
}

ClockDemo

/**
* Tests the Clock and WorldClock Classes.
*/
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());
}
}

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

Answer;

// Clock.java

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

                                */

                }

                /**

                * instead of assigning to a variable from constructor, it is much better

                * to return the current time whenever getMinute() or getHour() called,

                * which will always provide correct time

                */

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

                }

                /**

                * returns the current time in HH:MM format

                */

                public String getTime() {

                                return getHour() + ":" + getMinute();

                }

}

// WorldClock.java

public class WorldClock extends Clock {

                private int timeOffset;

                //constructor accepting an offset

                public WorldClock(int timeOffset) {

                                this.timeOffset = timeOffset;

                }

                @Override

                public int getHour() {

                                // adding offset value to hour

                                int hour = super.getHour() + timeOffset;

                                if (hour > 23) {

                                                /**

                                                * hour is now greater than 23 , so continuously subtracting from 23

                                                * to keep it under range

                                                */

                                                while (hour > 23) {

                                                                hour = 23 - hour;

                                                }

                                }

                                if (hour < 0) {

                                                /**

                                                * hour is now smaller than 0 [negative offset], so continuously

                                                * adding 23 to keep it under range

                                                */

                                                while (hour < 0) {

                                                                hour = 23 + hour;

                                                }

                                }

                                return hour;

                }

}

// Test.java (tester class with main method)

public class Test {

                public static void main(String[] args) {

                                Clock c = new Clock();

                                System.out.printf("%02d:%02d\n", c.getHour(), c.getMinute());

                                /**

                                * Testing the world clock class

                                */

                                WorldClock worldClock = new WorldClock(3);

                                /**

                                * displaying current time of worldClock object using getTime method

                                */

                                System.out.println(worldClock.getTime());

                }

}

/*OUTPUT*/

10:51

13:51

Add a comment
Know the answer?
Add Answer to:
Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...
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
  • 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....

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

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

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

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

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

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

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

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