Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a class named Clock to represent a clock that stores time in 12-hour format. Use appropriate visibility modifiers and appropriate data types. The class
Write a class named Clock to represent a clock that stores time in 12-hour format. Use appropriate visibility modifiers and appropriate data types. The class shall contain only the following items:
- A data field named hours to hold the hour part of the current time.
- A data field named minutes to hold the minute part of the current time.
- Two constant fields named AM and PM with the values 1 and 2, respectively. The constants should be accessible through the class, rather than requiring an object to access the values. These constants (rather than the literal values 1 and 2) should be used whenever you need to indicate that a time is AM or PM.
- A field named am_pm that indicates whether the current time stored is for AM or PM.
- A method named getTime() which returns the current time stored as a string in hh:mm format followed by a.m. or p.m. (e.g., 06:40 p.m.).
- The accessor and mutator (getter and setter) methods for all data fields. The accessor for am_pm should return the string representation ("a.m." or "p.m.") of the field's value.
- A constructor that creates a default clock, set to 6:00 a.m.
Write a test program that creates three Clock objects.
- The first should be a default Clock.
- Assign 11:57 p.m. to the second object.
- Assign 8:15 a.m. to the third object.
Display the time for each of the objects by invoking their getTime() method.
MUST BE IN JAVA!
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