Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CSC 2 0 5 Lab 2 Part I The Clock Class Creating our Very Own Class with Instance Methods - Clock Change into your Labs
CSC Lab Part I The Clock Class
Creating our Very Own Class with Instance Methods Clock
Change into your Labs directory by typing cd Labs, and let us create and change into a Lab
directory by typing mkdir Lab and then cd Lab
Now, use your CalendarDate class as a guide to create a Clock class. Create a file named
Clock.java. Here, we want to create an instance class named Clock that contains the
following variables, methods, and constructors:
Private instance variables that store hours minutes and seconds
A constructor that initializes the hours, minutes, and seconds to values specified by the
parameters.
A default constructor with no parameters that sets the hours, minutes, and seconds to
A method named reset that resets the hours, minutes, and seconds to It will have no
parameters.
An overloaded reset method that resets the hours, minutes, and seconds to values
specified by the parameters. It will have three parameters.
A method named advance that advances the clock by one second. If seconds has now
reached reset seconds to and increment the minutes. If after incrementing minutes,
minutes has now reached reset minutes to and increment the hours. If after
incrementing hours, hours has now reached reset hours to
A toString method that returns hours, minutes, and seconds as a string of the form
h:m:s
You should end up with a total of public methods, and private attributes. Compile your
Clock.java file before moving on to test it
We now need to write a client program named TestClock that contains a main method that we
can use to test our class. In this client program, declare a Clock object named dvr It will be
constructed using the default constructor. Next, prompt the user to input a specified hours,
minutes, and seconds. Reset your dvr object to this time using the reset method, print it out,
advance it by one second, and then reprint. Compile javac TestClock.java and run java
TestClock. Heres a sample test case with the user input shown in boldface.
Note: if you have any errors in Clock.java, return to this file, make your changes and this file
will be automatically recompiled for you when you recompile TestClock.java.
What time would you like to set your DVR to
Hours
Minutes
Seconds
The time is now ::
Advancing the time!
The time is now ::
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