Skip to content

Checkpoint: Plan Summary

The Course
•
20 min study

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.

Plan the loop

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.

Count selected records in a different task Java
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
Text
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.

Preserve the supplied data Java
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
Text
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.

Plan your acceptance cases

Predict the returned total for each row before you write the method.

CaseExpected behaviorSkill it checks
{10, 25, 40}, minimum 2565Only selected values are added
{10, 25, 40}, minimum 4040A value equal to the minimum is included
{10, 25, 40}, minimum 410Nothing selected
empty array, minimum 250Zero loop iterations
{0, 5}, minimum 05Minimum 0 includes everything

The "Change it" section below breaks the method three ways on purpose; for each one, find the row that shows it.

Your turn

Exercise

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.

Your workspace Java
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
Java
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;
    }
}
Text
65

Only total += value sits inside if (value >= minimum), and the return comes after the loop. >= keeps a value equal to the minimum, which is what the [10, 25, 40], 40 → 40 case checks.

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.

Was this article helpful?

Your response is saved on this device.