Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Write a class called Clock. Here are the first few lines: public class Clock { // instance variables private int hour; private int minute;

JAVA Write a class called Clock. Here are the first few lines: public class Clock { // instance variables private int hour; private int minute; private int second;

a) Write a default constructor for a 12-hour Clock that initializes hour, minute, and second to zero. b) Write accessors and mutators for all instance variables. c)Write a toString method that will return a String representation of a Clock object using the format hour : minute : second.

d)Each integer should occupy 2 digits. For example, if hour contains 12, minute contains 5, and second contains 9, the toString method should return the String "12:05:09". In this example, you need to insert a '0' character before the minutes and also before the seconds.

e)Write a method called tick that will increment the number of seconds, then check to see that second, minute, and hour contain valid data. Adjust the instance variables as needed. f)Write an equals method that will return true if the Clock parameter has the same hour, minute, and second as the calling object.

Testing:

Write a main method that will test the Clock class by performing the following tasks:

Test the default constructor by creating a Clock object. Test the toString method by printing the Clock object you just created. Test the mutator methods by changing the hour to 4, the minute to 8, and the second to 5. Print the object with the changes. Test the accessors by printing, on separate lines, the hour, then the minute, then the second. Set the hour to 11, the minute to 59, and the second to 59. Test the tick method. Call tick() then print the Clock object to make sure that the new time is correct. Create another Clock object. Call the mutators to set the hour to 12, the minute to 0, and the second to 0. Use an if statement to call the equals method and print out either "The times are the same." or "The times are not the same." when you compare the first Clock object to the second Clock object.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions