Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. (10 points) Write a static method named enoughTimeForLunch that accepts four

ID: 3720704 • Letter: A

Question

a. (10 points) Write a static method named enoughTimeForLunch that accepts four integers hourl, minutel, hour2, and minute2 as parameters. Each pair of parameters represents a time on the 24-hour clock (for example, 1:36 PM would be represented as 13 and 36). The method should return true if the gap between the two times is long enough to eat lunch: that is, if the second time is at least 45 minutes after the first time. Otherwise the method should return false You may assume that all parameter valucs are valid: the hours are both betwcen 0 and 23, and the minute parameters are between 0 and 59. You may also assume that both times represent times in the same day, e.g. the first time won't represent a time today while the second time represents a time tomorrow. Note that the second time might be earlier than the first time; in such a case, your method should return false Here are some example calls to your method and their expected return results Call enoughTimeForLunch( 11, 00, 11, 59) enoughTimeForLunch( 12, 30, 13, 00) enoughTimeForLunch(12, 30, 13, 15) enoughTimeForLunch (14, 20, 17, 02) enoughTimeForLunch (12, 30, 9, 30) enoughTimeForLunch (12, 00, 11,55) Value Returned true false true true false false

Explanation / Answer

public class Lunch {
public static boolean enoughTimeForLunch(int hour1, int minute1, int hour2, int minute2) {
int time1 = 60*hour1 + minute1;
int time2 = 60*hour2 + minute2;
return (time2-time1 >= 45);
}

public static void main(String[] args) {
System.out.println(enoughTimeForLunch(11,00,11,59));
System.out.println(enoughTimeForLunch(12,30,13,00));
System.out.println(enoughTimeForLunch(12,30,13,15));
System.out.println(enoughTimeForLunch(14,20,17,02));
System.out.println(enoughTimeForLunch(12,30,9,30));
System.out.println(enoughTimeForLunch(12,00,11,55));
}
}

Sample run

true
false
true
true
false
false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote