Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

ClockTime

Related Links:
Author: Marty Stepp

Suppose that a class ClockTime has been defined for storing information about times of day. The class code is linked under the problem title in "Related Links". Each ClockTime object keeps track of hours, minutes, and a String to indicate "am" or "pm". The class includes the following members:

NameDescription
private int hours hours component
private int minutes minutes component
private String amPm "am" for mornings or "pm" for afternoons
public ClockTime(int hours, int minutes, String amPm) constructs a clock time with given hours, mins, am/pm
public int getHours() returns the hours
public int getMinutes() returns the minutes
public String getAmPm() returns the am/pm setting
public String toString() returns a String representation of the time

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. The earliest time is 12:00 am and the latest time is 11:59 pm. In between the time increases as it would in a standard clock. Keep in mind that 12:59 am is followed by 1:00 am, that 11:59 am is followed by 12:00 pm, and that 12:59 pm is followed by 1:00 pm.

Assume that the values passed to your constructor are legal. In particular, hours will be between 1 and 12 inclusive, minutes will be between 0 and 59 inclusive, and the am/pm parameter will be either the string "am" or the string "pm". These values should be returned by the various get methods.

The toString method returns a string composed of the hours followed by a colon followed by the minutes (2 digits) followed by a space followed by the am/pm String. For example, given these declarations:

ClockTime time1 = new ClockTime(8, 31, "am");
ClockTime time2 = new ClockTime(12, 7, "pm");

time1.toString() would return "8:31 am" and time2.toString() should return "12:07 pm".

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.