Question

[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:

  1. 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.
  2. Write a test program to test your new class.

Clock.Java:

public class Clock

{

private int hr;

private int min;

private int sec;

public Clock()

{

setTime(0, 0, 0);

}

public Clock(int hours, int minutes, int seconds)

{

setTime(hours, minutes, seconds);

}

public void setTime(int hours, int minutes, int seconds)

{

if (0 <= hours && hours < 24)

hr = hours;

else

hr = 0;

if (0 <= minutes && minutes < 60)

min = minutes;

else

min = 0;

if(0 <= seconds && seconds < 60)

sec = seconds;

else

sec = 0;

}

public int getHours()

{

return hr;

}

public int getMinutes()

{

return min;

}

public int getSeconds()

{

return sec;

}

public void printTime()

{

if (hr < 10)

System.out.print("0");

System.out.print(hr + ":");

if (min < 10)

System.out.print("0");

System.out.print(min + ":");

if (sec < 10)

System.out.print("0");

System.out.print(sec);

}

public void incrementSeconds()

{

sec++;

if (sec > 59)

{

sec = 0;

incrementMinutes();

}

}

public void incrementMinutes()

{

min++;

if (min > 59)

{

min = 0;

incrementHours();

}

}

public void incrementHours()

{

hr++;

if(hr > 23)

hr = 0;

}

public boolean equals(Clock otherClock)

{

return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);

}

public void makeCopy(Clock otherClock)

{

hr = otherClock.hr;

min = otherClock.min;

sec = otherClock.sec;

}

public Clock getCopy()

{

Clock temp = new Clock();

temp.hr = hr;

temp.min = min;

temp.sec = sec;

return temp;

}

}

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

CODE

class Clock

{

private int hr;

private int min;

private int sec;

public Clock()

{

setTime(0, 0, 0);

}

public Clock(int hours, int minutes, int seconds)

{

setTime(hours, minutes, seconds);

}

public void setTime(int hours, int minutes, int seconds)

{

if (0 <= hours && hours < 24)

hr = hours;

else

hr = 0;

if (0 <= minutes && minutes < 60)

min = minutes;

else

min = 0;

if(0 <= seconds && seconds < 60)

sec = seconds;

else

sec = 0;

}

public int getHours()

{

return hr;

}

public int getMinutes()

{

return min;

}

public int getSeconds()

{

return sec;

}

public void printTime()

{

if (hr < 10)

System.out.print("0");

System.out.print(hr + ":");

if (min < 10)

System.out.print("0");

System.out.print(min + ":");

if (sec < 10)

System.out.print("0");

System.out.print(sec);

}

public void incrementSeconds()

{

sec++;

if (sec > 59)

{

sec = 0;

incrementMinutes();

}

}

public void incrementMinutes()

{

min++;

if (min > 59)

{

min = 0;

incrementHours();

}

}

public void incrementHours()

{

hr++;

if(hr > 23)

hr = 0;

}

public boolean equals(Clock otherClock)

{

return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);

}

public void makeCopy(Clock otherClock)

{

hr = otherClock.hr;

min = otherClock.min;

sec = otherClock.sec;

}

public Clock getCopy()

{

Clock temp = new Clock();

temp.hr = hr;

temp.min = min;

temp.sec = sec;

return temp;

}

}

class ExtClock extends Clock {

private String timeZone;

public ExtClock(int hours, int minutes, int seconds, String timeZone) {

super(hours, minutes, seconds);

this.timeZone = timeZone;

}

/**

* @return the timeZone

*/

public String getTimeZone() {

return timeZone;

}

/**

* @param timeZone the timeZone to set

*/

public void setTimeZone(String timeZone) {

this.timeZone = timeZone;

}

public void printTimeWithZone() {

if (this.getHours() < 10)

System.out.print("0");

System.out.print(this.getHours() + ":");

if (this.getMinutes() < 10)

System.out.print("0");

System.out.print(this.getMinutes() + ":");

if (this.getSeconds() < 10)

System.out.print("0");

System.out.print(this.getSeconds());

System.out.println(" " + timeZone);

}

}

public class Main {

public static void main(String[] args) {

ExtClock clock = new ExtClock(7, 14, 45, "IST");

clock.printTimeWithZone();

}

}

Add a comment
Know the answer?
Add Answer to:
[Java] We have learned the class Clock, which was designed to implement the time of day...
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
  • 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...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

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

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

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

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

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

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

  • D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime...

    D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime (int, int, int): void get Time (inte, int&, int&) const: void printTime() const: void increment seconds(): int +incrementMinutes(): int +increment Hours(): int -equalTime (const clockType.) const: bool Consider the UML class diagram shown in the accompanying figure. According to the UML class diagram, how many private members are in the class? none zero two three D Question 4 4 pts Consider the following class...

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