Question 171
A regional airport uses a computerized scheduling system to manage daily flight departures. Each flight has a scheduled departure time and an actual departure time, both stored as integers in 24-hour format where the value represents hours and minutes combined. For example, 1430 represents 2:30 PM and 905 represents 9:05 AM. You will write two methods for the FlightScheduler class to help the operations team analyze the day's performance.
PART A
Write the method public int minutesBetween(int time1, int time2) that converts two times from HHMM format to total minutes since midnight and returns the absolute difference. For example, minutesBetween(1430, 900) should return 330 because \(14\times60+30 = 870\) minutes and \(9\times60+0 = 540\) minutes, and the absolute difference is 330.
public int minutesBetween(int time1, int time2) {
PART B
Write the method public int countDelays(int[] scheduled, int[] actual, int threshold) that returns the number of flights where the actual departure was more than threshold minutes after the scheduled time. You may call the minutesBetween method from Part A. A flight is only considered delayed if the actual time is later than the scheduled time and the difference exceeds the threshold.
For example, if scheduled is [800, 1200, 1500] and actual is [830, 1155, 1545] with threshold 20, the method returns 2 because the first flight (30 minutes late) and the third flight (45 minutes late) exceed the threshold.
public int countDelays(int[] scheduled, int[] actual, int threshold) {Main Solution
Part A Solution:
public int minutesBetween(int time1, int time2) {
int minutes1 = (time1 / 100) * 60 + (time1 % 100);
int minutes2 = (time2 / 100) * 60 + (time2 % 100);
return Math.abs(minutes1 - minutes2);
}
This method converts each HHMM time to total minutes since midnight by extracting hours (time/100) and minutes (time%100), then calculating total minutes and returning the absolute difference.
Part B Solution:
public int countDelays(int[] scheduled, int[] actual, int threshold) {
int count = 0;
for (int i = 0; i < scheduled.length; i++) {
if (actual[i] > scheduled[i]) {
int delay = minutesBetween(scheduled[i], actual[i]);
if (delay > threshold) {
count++;
}
}
}
return count;
}
This method iterates through the arrays, checks if actual time is later than scheduled time, calculates the delay using minutesBetween, and counts flights where the delay exceeds the threshold.

