Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!