A class that tracks data and reports min and average.
Two common statistics — minimum and average — wrapped inside a clean class with an ArrayList field.
The class stores numeric data, lets clients add values, and offers methods that report the minimum and the average. Both methods need to handle the empty-data case carefully.
On the AP exam, this pattern appears in score-tracking and sensor-monitoring FRQs.
Loop over the data once per stat.
Min finds smallest; average sums and divides.
DataSet class
public class DataSet {
private ArrayList<Integer> data;
public DataSet() { data = new ArrayList<>(); }
public void add(int value) { data.add(value); }
public int getMin() {
if (data.size() == 0) return 0; // empty case
int min = data.get(0);
for (int v : data) {
if (v < min) min = v;
}
return min;
}
public double getAverage() {
if (data.size() == 0) return 0.0;
int sum = 0;
for (int v : data) sum += v;
return (double) sum / data.size();
}
}
Initialization for min
Start min at the first element — never 0 (fails for all-positive data) or Integer.MAX_VALUE (still works but less natural). The empty guard handles the edge case.
Min and average vocabulary.
Used in stat classes.
- Minimum — smallest element.
- Average / mean — sum ÷ count.
- Accumulator — running sum.
- Empty guard — early return for no data.
- Initialization from first element — safe for min.
How min/average classes are tested.
Initialization and cast dominate.
- Init min from first element. Not 0.
- Cast sum for average. Avoid integer division.
- Empty case handling. Return sensible default.
- Iterate the field. Use the class's own ArrayList.
Compute stats over a small dataset.
Min and average.
- ds.add(7); ds.add(3); ds.add(9); ds.add(5);
- getMin(): start min=7; compare 3 → min=3; 9 → no change; 5 → no change. Returns 3.
- getAverage(): sum=24; size=4; 24.0/4 = 6.0.
Stat-class pitfalls.
Init and cast errors.
- Init min = 0. Fails on all-positive data.
- No cast in average. Integer truncation.
- No empty guard. Division by zero or get(0) on empty list.
- Forgetting to update min. Returns initial value.
Min vs average.
Side-by-side patterns.
Min & Average Reference
AP Quick Reference| Stat | Initial value | Empty return |
|---|---|---|
| min | data.get(0) | 0 or per spec |
| sum | 0 | — |
| average | sum / size | 0.0 |
How to master min/average classes.
Init right; cast right; guard empty.
- MCQ Practice — for each option, verify min init and average cast.
- Java Lab — build DataSet with min and average. Test empty and all-negative inputs.
- FRQ Practice — empty-case guard at top; init min from first; cast sum for average.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 11 Complete Class with Min and Average FRQ Practice
AP-style topic practice assessment