Accumulate a count; report a percentage.
Sum or count first; divide and scale at the end. Two passes — or one pass plus one calculation.
Many AP-style problems ask for a percentage: pass rate, attendance percentage, success ratio. The pattern: count the matches, divide by the total, multiply by 100 — with the right cast to avoid integer truncation.
On the AP exam, percentage math appears in classification FRQs and in tracing problems involving cast-vs-no-cast.
Count, cast, divide, scale.
Each step has its purpose.
The template
public double percentPassing(int[] scores, int threshold) {
if (scores.length == 0) return 0.0;
int passed = 0;
for (int s : scores) {
if (s >= threshold) passed++;
}
return 100.0 * passed / scores.length;
}
Why the literal 100.0?
Multiplying first by 100.0 forces the entire expression into double arithmetic. Without the decimal, 100 * passed / scores.length stays in integer land until the division — and truncates.
Cast alternative
return (double) passed / scores.length * 100;
Same result — the cast promotes the division to double.
Empty input
Guard against division by zero by returning a sensible value (often 0.0) when the array is empty.
Percentage vocabulary.
Used in rate / ratio problems.
- Accumulator — counter or sum that grows during traversal.
- Percentage — value out of 100.
- Cast — explicit type conversion.
- Integer truncation — losing the decimal when dividing ints.
- Scale — multiplying by 100 to shift the decimal.
- Empty-input guard — protecting against division by zero.
How percentage math is tested.
Cast placement and empty-input handling dominate.
- Cast before integer division. Otherwise result truncates.
- 100.0 literal trick. Forces double from the start.
- Empty input guard. Don't divide by zero.
- Return type double. Percentage typically has decimals.
Compute pass percentage.
Two scenarios — with and without cast.
Input: 7 passing students out of 10.
Wrong: 100 * 7 / 10 → 700 / 10 = 70 (correct here but only by luck).
Wrong: 7 / 10 * 100 → 0 * 100 = 0 (integer truncation kills the value).
Right: 100.0 * 7 / 10 → 700.0 / 10 = 70.0.
Right: (double) 7 / 10 * 100 → 0.7 * 100 = 70.0.
Lesson: Cast or use 100.0 to force double arithmetic.
Percentage pitfalls.
All are about types and order.
- Integer-only arithmetic.
7 / 10is 0. - Wrong cast position. Cast must be before the division to take effect.
- Forgetting the empty-input guard. Division by zero on empty array.
- Returning percentage as
int. Loses the decimal.
Percentage math at a glance.
Three forms; pick one.
Percentage Reference
AP Quick Reference| Expression | Behavior | Recommended? |
|---|---|---|
100 * passed / total | Risky overflow | No. |
100.0 * passed / total | Correct | Yes. |
(double) passed / total * 100 | Correct | Yes. |
passed / total * 100 | Truncated | No. |
How to master percentages.
Always cast; always guard empty.
- MCQ Practice — verify cast position and rule out integer-only options.
- Java Lab — implement passRate, attendancePercent. Test empty arrays.
- FRQ Practice — use
100.0 *at the start of percentage expressions.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 09 Topic 07 Accumulation and Percentage Math FRQ Practice
AP-style topic practice assessment