logo CodeStepByStep logo

Clock-isWorkTime

Language/Type: Java classes instance methods
Related Links:

Suppose that you are provided with a pre-written class Clock 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 Clock object represents an hour:minute time during
// the day or night, such as 10:45 AM or 6:27 PM.
public class Clock {
    private int hour;
    private int minute;
    private String amPm;

    // Constructs a new time for the given hour/minute
    public Clock(int hour, int minute, String amPm)
    
    // 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()

    // advances this Clock by the given # of minutes
    public void advance(int m)

    // your method would go here

}

Write an instance method named isWorkTime that will be placed inside the Clock class to become a part of each Clock object's behavior. The isWorkTime method returns true if the Clock object represents a time during the normal "work day" from 9:00 AM to 5:00 PM, inclusive. Any times outside that range would cause the method to return a result of false.

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

Clock t1 = new Clock(3, 27, "PM");

The following call to your method would return true:

if (t1.isWorkTime()) {       // true

Here are some other objects. Their results when used with your method are shown at right in comments:

Clock t2 = new Clock(12, 45, "AM"); //false
Clock t3 = new Clock( 6, 02, "AM"); //false
Clock t4 = new Clock( 8, 59, "AM"); //false
Clock t5 = new Clock( 9, 00, "AM"); //true
Clock t6 = new Clock(11, 38, "AM"); //true
Clock t7 = new Clock(12, 53, "PM"); //true
Clock t8 = new Clock( 3, 15, "PM"); //true
Clock t9 = new Clock( 4, 59, "PM"); //true
Clock ta = new Clock( 5, 00, "PM"); //true
Clock tb = new Clock( 5, 01, "PM"); //false
Clock tc = new Clock( 8, 30, "PM"); //false
Clock td = new Clock(11, 59, "PM"); //false

Your method should not modify the state of the object. 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".

Partial class: Write code that will become part of an existing class as described. You do not need to write the complete class, just the portion described in the exercise.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.