Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA CODE ONLY. NO OTHER CODES PER THE INSTRUCTION BELOW: Write a Java class Clock for dealing with the day time represented by hours, minutes,
JAVA CODE ONLY. NO OTHER CODES PER THE INSTRUCTION BELOW:
Write a Java classClockfor dealing with the day time represented by hours, minutes, and seconds. Your class must have the following features:
- Three instance variables for the hours (range 0 - 23), minutes (range 0 - 59), and seconds (range 0 - 59).
- Three constructors:
- default (with no parameters passed; is should initialize the represented time to 12:0:0)
- a constructor with three parameters: hours, minutes, and seconds.
- a constructor with one parameter: the value of time in seconds since midnight (it should be converted into the time value in hours, minutes, and seconds)
- Instance methods:
- aset-method methodsetClock()with one parametersecondssince midnight (to be converted into the time value in hours, minutes, and seconds as above).
- get-methodsgetHours(),getMinutes(),getSeconds()with no parameters that return the corresponding values.
- set-methods setHours(), setMinutes(), setSeconds() with one parameter each that set up the corresponding instance variables.
- methodtick()with no parameters that increments the time stored in a Clock object by one second.
- methodaddClock()accepting an object of type Clock as a parameter. The method should add the time represented by the parameter class to the time represented in the current class.
- Add an instance methodtoPrint()with no parameters to your class.toPrint()must return a String representation of the Clock object in the form "(hh:mm:ss)", for example "(03:02:34)".
- Add an instance methodtickDown()which decrements the time stored in a Clock object by one second.
- Add an instance methodsubtractClock()that takes one Clock parameter and returns the difference between the time represented in the current Clock object and the one represented by the Clock parameter. Difference of time should be returned as an clock object.
Write a separate classClockDemowith amain()method. The program should:
- Instantiate a Clock object firstClock using one integersecondssince midnight obtained from the keyboard.
- Tick the clock ten times by applying itstick()method and print out the time after each tick.
- Extend your code by appending to it instructions instantiating a Clock object secondClock by using three integers (hours, minutes, seconds) read from the keyboard.
- Then tick the clock ten times, printing the time after each tick.
- Add the secondClock time in firstClock by calling method addClock.
- Print both clock objects callingtoPrint()method
Create a reference thirdClock that should reference to object of difference of firstClock and secondClock by calling the methodsubtractClock().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started