Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective: Complete and test an instantiable Java class that keeps track of information for a clock time. Description: Chapter 9 of your textbook introduces you

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Objective: Complete and test an instantiable Java class that keeps track of information for a clock time. Description: Chapter 9 of your textbook introduces you to the idea writing your own instantiable classes. As you have learned, these are the types of classes from which you can create objects. 1. Start your work by copying the BlueJ project named Assign9Clock (available from Canvas as a zip file) 2. You will see a class already in your project named TestClockTime. This class holds a main () method which tests all the parts of the ClockTime class. 3. Add a new class to your project named clockTime. This is your instantiable class. You SHOULD NOT make any changes to the TestClockTime class. All work should be done in the ClockTime class, described here: ClockTime class Instance variables Each ClockTime object needs 3 private data fields, all integers (please use the exact names as specified below). In your comments, please note that this type of object keeps time in the 24-hour format. 1. hour holds the ClockTime hour setting 2. minute holds the ClockTime minute setting 3. second holds the ClockTime second setting Constructors Test each of these by right-clicking on the class to create the object and then inspecting the object created. This class needs 2 constructors: 1. A default (no parameter) version that sets all instance variables to zeroes. 2. An alternate version that takes 3 integer parameters, holding the hour, minute and second values (IN THAT ORDER) to which the ClockTime object should be initialized. However, the constructor needs to do a little error checking, too: - If the seconds parameter is over 59 , use mod 60 to make it 59 or lower, and add the extra minutes to the minutes parameter. If negative, make it zero. - If the minute parameter is over 59 , use mod 60 to make it 59 or lower, and add the extra hour value to the hours parameter. If negative, make it zero. - If the hour parameter is over 24 , use mod 24 to make it 23 or lower. If negative, make it zero. Instance methods Test each of these by first creating an object on Bluel's object bench and right-clicking on it to choose and run individual instance methods. 1. "Getter" (Accessor) methods The class should have public "getter" methods for each of the instance variables. The methods must be named: - int gethour () - int getMinute () - int getSecond () 2. "Setter" (Mutator) methods The class should have public "setter" methods for each of the instance variables. The methods must be named: - void setHour (int) - void setMinute (int) - void setSecond (int) Please make sure to do the same range checks as required in the constructor, so that the instance variable values are valid. 3.Methods that create Strings There are two ways a time in a 24 -hour clock can be displayed. For example, 3:25:0 in the afternoon can be written as: 15:25:0003:25:00P.M. (using 24-hour format), or (using 12-hour format) Your class will contain a method for each version. Inside each method, build a String by concatenating the pieces in the correct sequence. Note that if the hour, minute or second are only one digit in length, your code should include a leading zero in the String that you build and return. Here are the prototypes for each of these public methods: - String tostring () This method creates and returns a String that holds the clock's time in 24-hour format (15:25:00). It does NOT contain A.M. or P.M. in it. . String tostring12() This method creates and returns a String that holds the clock's time in 12-hour format (03:25:00 P.M.). It will, of course, include A.M. instead of P.M., when appropriate. 4. Methods to calculate and compare Here are 2 more public methods to add. Neither is complicated, but each lets us see, or do, more interesting things with a given ClockTime: - void advance (int) This method takes a positive integer as input and advances the ClockTime object by that many seconds. Note that if this makes the number of seconds go over 60 , the number of minutes should go up instead. Also, if the number of minutes goes over 60 , the number of hours should go up. If the hours variable goes over 24 , use mod 24 on it (as you did in the constructor). Simple if-statements might not be enough for this step; so you'll need either loops or some other trickery... - boolean equals (ClockTime) This method checks to see if the current ClockTime contains the same values as the one sent in the parameter. If so, it returns true, otherwise returns false. TestClockTime class This class contains a main () method with which you can test your code. You may wish to comment out most of the code here as you are testing. Uncomment the corresponding lines as soon as you are ready to test those methods. When all are finished, here is what you should see printed: Setting a clock with midnight clock1 has time: 00:00:00 clockl values reset to 9:15:33 new hour: 9 new minute: 15 new second: 33 clock1 time using 12-hour clock: 09:15:33 A.M. clock2 has time: 14:48:53 clock2 time using 12-hour clock: 02:48:53 P.M. The two clocks hold different times. Adding 20000 seconds to clock1 Now clock1 reads: 14:48:53 The two clocks read the same. Warning: Your ClockTime class will be tested against both the main () method in TestclockTime and against a different main (), so make certain you have confidence in your code

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

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

=+(0

Answered: 1 week ago