logo Practice-It logo

advance

Language/Type: Java classes instance methods
Related Links:
Author: Marty Stepp

Suppose that you are provided with a pre-written class ClockTime as described below. (The headings are shown, but not the method bodies, to save space.) Assume that the fields, constructor, and methods shown are already implemented. You may refer to them or use them in solving this problem if necessary.

// A ClockTime object represents an hour:minute time during
// the day or night, such as 10:45 AM or 6:27 PM.
public class ClockTime {
    private int hour;
    private int minute;
    private String amPm;

    // Constructs a new time for the given hour/minute
    public ClockTime(int h, int m, String ap)
    
    // returns the field values
    public int getHour()
    public int getMinute()
    public String getAmPm()

    // returns String for time; for example, "6:27 PM"
    public String toString()

    // your method would go here

}

Write an instance method named advance that will be placed inside the ClockTime class to become a part of each ClockTime object's behavior. The advance method accepts a number of minutes as its parameter and moves your object forward in time by that amount of minutes. The minutes passed could be any non-negative number, even a large number such as 500 or 1000000. If necessary, your object might wrap into the next hour or day, or it might wrap from the morning ("AM") to the evening ("PM") or vice versa. A ClockTime object doesn't care about what day it is; if you advance by 1 minute from 11:59 PM, it becomes 12:00 AM.

For example, if the following object is declared in client code:

ClockTime time = new ClockTime(6, 27, "PM");

The following calls to your method would modify the object's state as indicated in the comments:

time.advance(1);       //  6:28 PM
time.advance(30);      //  6:58 PM
time.advance(5);       //  7:03 PM
time.advance(60);      //  8:03 PM
time.advance(128);     // 10:11 PM
time.advance(180);     //  1:11 AM
time.advance(1440);    //  1:11 AM  (1 day later)
time.advance(21075);   //  4:26 PM  (2 weeks later)

Assume that the state of the object is valid at the start of the call and that the amPm field stores either "AM" or "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.