You now have the tools to summarize a collection without manually naming every entry. This checkpoint asks for a method, not a method that reads input itself. Keeping the data boundary separate lets the same calculation work in a program and in automated practice cases.
The brief
Implement minutesAtLeast(int[] minutes, int minimum). Return the total of values that are at least the supplied minimum. The array is non-null, durations and the minimum are nonnegative, and examples are small enough for int totals. Do not change the input. Do not confuse the number of selected values with their sum.
Acceptance cases
For {10,25,40} and 25, return 65. With minimum 40, return 40; with 41, return 0. An empty array returns 0. A value exactly at the threshold is included. With minimum 0, all supplied values are included. Use these cases to detect plausible mistakes rather than writing a special branch for each example.
Make a table with columns current value, included?, total. The include decision is inside the loop, but the accumulated total is initialized outside it and returned after it. This is the same control structure you have used for days, now packaged as a reusable method over an array.
public class Main {
public static void main(String[] args) {
int[] pages = {5, 15, 20};
int matches = 0;
for (int value : pages) {
if (value >= 15) {
matches++;
}
}
System.out.println(matches);
}
}Check your prediction
2 This example counts matching values. The checkpoint totals their contents instead. Identify the exact line that must change when the question changes from how many to how much.
public class Main {
public static void main(String[] args) {
int[] values = {10, 25};
int sum = total(values);
System.out.println(sum);
System.out.println(values[0]);
}
static int total(int[] values) {
int sum = 0;
for (int value : values) {
sum += value;
}
return sum;
}
}Check your prediction
35
10 Reading and totaling does not require replacing elements. A caller can reuse the same data after this method finishes. A destructive summation would violate the checkpoint even if its first returned number looked correct.
Your turn
Total matching durations
Implement minutesAtLeast and use the practice cases to check boundaries and empty input. The ordinary main should print 65 for {10,25,40} with minimum 25. Write the rule once, not a series of checks for particular arrays.
public class Main {
public static void main(String[] args) {
System.out.println(minutesAtLeast(new int[] {10, 25, 40}, 25));
}
static int minutesAtLeast(int[] minutes, int minimum) {
return 0; // Replace the placeholder.
}
}Hint 1: the idea
Only the update belongs inside the selection branch. The loop must keep looking after a match.
Hint 2: a first step
Use an int accumulator starting at zero. Add the selected value rather than incrementing the accumulator by one.
Show one solution
public class Main {
public static void main(String[] args) {
System.out.println(minutesAtLeast(new int[] {10, 25, 40}, 25));
}
static int minutesAtLeast(int[] minutes, int minimum) {
int total = 0;
for (int value : minutes) {
if (value >= minimum) {
total += value;
}
}
return total;
}
}65 Change it and explain
Change the solution deliberately: use > instead of >=, return inside the loop, then increment by one instead of adding the value. Restore the correct version after each experiment. Say which acceptance case catches each defect and why.
Write a second version accepting ArrayList<Integer> if you want more practice. Do not introduce streams just to shorten the loop. Clear accumulation, a precise contract and good boundary cases matter more than fitting the program on fewer lines.
Completion is saved in this browser only. It does not measure mastery.