A class with an array field plus stat methods.
When the data is fixed-size, an array field works; the class adds methods for total, max, count-above, and other statistics.
An array field (as opposed to ArrayList) suits classes where the data size is known and fixed: a fixed number of test scores, sensor readings, or grades. Stat methods loop over the array and compute totals, extremes, and counts.
On the AP exam, array-field classes appear in test-score, athletic-stat, and sensor FRQs.
Fixed array; methods loop over it.
One stat per method.
Scores class
public class Scores {
private int[] scores;
public Scores(int[] data) {
this.scores = data;
}
public int total() {
int sum = 0;
for (int s : scores) sum += s;
return sum;
}
public int max() {
if (scores.length == 0) return 0;
int m = scores[0];
for (int s : scores) {
if (s > m) m = s;
}
return m;
}
public int countAbove(int threshold) {
int count = 0;
for (int s : scores) {
if (s > threshold) count++;
}
return count;
}
public double average() {
if (scores.length == 0) return 0.0;
return (double) total() / scores.length;
}
}
One stat per method
Each method computes one statistic. Calls can be chained from the outside but each method stays focused.
Array-field vocabulary.
Used in stat-tracking classes.
- Array field — fixed-size collection as instance variable.
- Total — sum of all elements.
- Max — largest element.
- Conditional count — number of elements meeting a condition.
- Encapsulation — array is private; methods do the work.
How array-field classes are tested.
Stat-method correctness dominates.
- Loop the field. Use enhanced-for for read-only stats.
- Cast for average. Avoid integer truncation.
- Empty-array safety. Guard max and average.
- Init max from scores[0]. Not 0.
Compute multiple stats.
Same field; different aggregations.
Setup. Scores s = new Scores(new int[]{55, 78, 92, 60, 45});
- s.total() → 330.
- s.max() → 92.
- s.countAbove(70) → 2 (78 and 92).
- s.average() → 330.0 / 5 = 66.0.
Stat-method pitfalls.
Cast and init errors.
- No cast in average. Truncated result.
- Init max = 0. Wrong for all-negative arrays.
- Empty-array crash. Division by zero or get(0) on empty.
- Wrong threshold direction. "Above" vs "at least".
Stat methods at a glance.
Common aggregations.
Stats Reference
AP Quick Reference| Method | Returns | Initial |
|---|---|---|
| total | int | 0 |
| max | int | scores[0] |
| countAbove | int | 0 |
| average | double | 0.0 on empty |
How to master stat methods.
One method per stat; guard the edges.
- MCQ Practice — verify each method's initial value and edge-case handling.
- Java Lab — write Scores class with multiple stat methods. Test on empty input.
- FRQ Practice — each stat gets its own short method; chain them in the test code.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 13 Statistics over an Array Field FRQ Practice
AP-style topic practice assessment