Suppose that you are provided with a pre-written class ClockTime as described be
ID: 3690991 • Letter: S
Question
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.
Write an instance method named isWorkTime that will be placed inside the ClockTime class to become a part of each ClockTimeobject's behavior. The isWorkTime method returns true if the ClockTime 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:
The following call to your method would return true:
Here are some other objects. Their results when used with your method are shown at right in comments:
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".
Explanation / Answer
ClockTime.java
package org.students;
//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 hour, int minute, String amPm) {
super();
this.hour = hour;
this.minute = minute;
this.amPm = amPm;
}
// returns the field values
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public String getAmPm() {
return amPm;
}
// returns String for time; for example, "6:27 PM"
@Override
public String toString() {
return "ClockTime [hour=" + hour + ", minute=" + minute + ", amPm="
+ amPm + "]";
}
// advances this ClockTime by the given # of minutes
//public void advance(int m) {
//}
public boolean isWorkTime()
{
if(hour==9 && minute==00 && amPm=="AM" ||
hour>9 && hour<=11 && minute>00 && minute<=59 && amPm=="AM" ||
hour==12 && minute==00 && amPm=="PM" ||
hour==12 && minute >00 && minute <=59 && amPm=="PM"||
hour>=1 && hour<=4 && minute>00 && minute<=59 && amPm=="PM" ||
hour<=4 && minute<=59 && amPm=="PM" ||
hour==5 && minute==00 && amPm=="PM")
{return true;}
else
return false;
}
}
_____________________________________________________________________________________________
Test.java
package org.students;
public class Test {
public static void main(String[] args) {
ClockTime t1 = new ClockTime(3, 27, "PM");//true
ClockTime t2 = new ClockTime(12, 45, "AM"); //false
ClockTime t3 = new ClockTime( 6, 02, "AM"); //false
ClockTime t4 = new ClockTime( 8, 59, "AM"); //false
ClockTime t5 = new ClockTime( 9, 00, "AM"); //true
ClockTime t6 = new ClockTime(11, 38, "AM"); //true
ClockTime t7 = new ClockTime(12, 53, "PM"); //true
ClockTime t8 = new ClockTime( 3, 15, "PM"); //true
ClockTime t9 = new ClockTime( 4, 59, "PM"); //true
ClockTime ta = new ClockTime( 5, 00, "PM"); //true
ClockTime tb = new ClockTime( 5, 01, "PM"); //false
ClockTime tc = new ClockTime( 8, 30, "PM"); //false
ClockTime td = new ClockTime(11, 59, "PM"); //false
boolean x1=t1.isWorkTime();
System.out.println(t1.getHour()+","+t1.getMinute()+","+t1.getAmPm()+" : " +x1);
boolean x2=t2.isWorkTime();
System.out.println(t2.getHour()+","+t2.getMinute()+","+t2.getAmPm()+" : " +x2);
boolean x3=t3.isWorkTime();
System.out.println(t3.getHour()+","+t3.getMinute()+","+t3.getAmPm()+" : " +x3);
boolean x4=t4.isWorkTime();
System.out.println(t4.getHour()+","+t4.getMinute()+","+t4.getAmPm()+" : " +x4);
boolean x5=t5.isWorkTime();
System.out.println(t5.getHour()+","+t5.getMinute()+","+t5.getAmPm()+" : " +x5);
boolean x6=t6.isWorkTime();
System.out.println(t6.getHour()+","+t6.getMinute()+","+t6.getAmPm()+" : " +x6);
boolean x7=t7.isWorkTime();
System.out.println(t7.getHour()+","+t7.getMinute()+","+t7.getAmPm()+" : " +x7);
boolean x8=t8.isWorkTime();
System.out.println(t8.getHour()+","+t8.getMinute()+","+t8.getAmPm()+" : " +x8);
boolean x9=t9.isWorkTime();
System.out.println(t9.getHour()+","+t9.getMinute()+","+t9.getAmPm()+" : " +x9);
boolean xa=ta.isWorkTime();
System.out.println(ta.getHour()+","+ta.getMinute()+","+ta.getAmPm()+" : " +xa);
boolean xb=tb.isWorkTime();
System.out.println(tb.getHour()+","+tb.getMinute()+","+tb.getAmPm()+" : " +xb);
boolean xc=tc.isWorkTime();
System.out.println(tc.getHour()+","+tc.getMinute()+","+tc.getAmPm()+" : " +xc);
boolean xd=td.isWorkTime();
System.out.println(td.getHour()+","+td.getMinute()+","+td.getAmPm()+" : " +xd);
}
}
____________________________________________________________________________________________
output:
3,27,PM : true
12,45,AM : false
6,2,AM : false
8,59,AM : false
9,0,AM : true
11,38,AM : true
12,53,PM : true
3,15,PM : true
4,59,PM : true
5,0,PM : true
5,1,PM : false
8,30,PM : false
11,59,PM : false
____________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.