Answered step by step
Verified Expert Solution
Link Copied!

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

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

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions