A vote-counting class with candidates and percentages.
Parallel ArrayLists of names and vote counts, plus methods to record votes and report each candidate's percentage.
A vote counter holds candidate names and their tallies. Recording a vote adds a candidate (if new) or increments their tally (if existing). Percentage methods divide each tally by the total — with the usual cast and zero-guard discipline.
On the AP exam, vote-counting FRQs combine parallel ArrayLists, search-or-add logic, and ratio computation.
Search-or-add; then report percentages.
One method per behavior.
VoteCounter class
public class VoteCounter {
private ArrayList<String> candidates;
private ArrayList<Integer> votes;
public VoteCounter() {
candidates = new ArrayList<>();
votes = new ArrayList<>();
}
public void recordVote(String name) {
for (int i = 0; i < candidates.size(); i++) {
if (candidates.get(i).equals(name)) {
votes.set(i, votes.get(i) + 1);
return;
}
}
candidates.add(name);
votes.add(1);
}
public int totalVotes() {
int total = 0;
for (int v : votes) total += v;
return total;
}
public double percentFor(String name) {
int total = totalVotes();
if (total == 0) return 0.0;
for (int i = 0; i < candidates.size(); i++) {
if (candidates.get(i).equals(name)) {
return 100.0 * votes.get(i) / total;
}
}
return 0.0;
}
}
Three patterns combined
- Parallel ArrayLists for records.
- Search-or-add for vote registration.
- Cast + zero-guard for percentage.
Vote-counting vocabulary.
Used in tally and election problems.
- Tally — running count per candidate.
- Search-or-add — find existing or append new.
- Parallel ArrayLists — aligned candidates and votes.
- Percentage — share of total votes.
- Total votes — sum of all tallies.
How vote-counting is tested.
Search-or-add and percentage math dominate.
- Search-or-add structure. Found: increment. Not found: append both.
- Use
.equals()for candidate names. - Cast for percentage. Avoid truncation.
- Zero-guard. Total of 0 returns 0.0.
Trace votes coming in.
Search-or-add at work.
- recordVote("Alex"): not found; append. candidates=[Alex], votes=[1].
- recordVote("Ben"): not found; append. candidates=[Alex, Ben], votes=[1, 1].
- recordVote("Alex"): found at 0; increment. votes=[2, 1].
- recordVote("Cara"): not found; append. votes=[2, 1, 1].
- totalVotes() → 4.
- percentFor("Alex") → 100.0 * 2 / 4 = 50.0.
- percentFor("Zoe") → not found; returns 0.0.
Vote-counting pitfalls.
Sync and cast errors.
- Append to only one list. Breaks alignment.
- Forgetting to return after increment. Falls through to append, adding a duplicate.
- No cast for percentage. Always 0 except at 100%.
- No zero guard. Crash on empty election.
Vote counting structure.
Three integrated patterns.
Vote Counter Reference
AP Quick Reference| Method | Pattern | Key step |
|---|---|---|
| recordVote | Search-or-add | Increment or append both. |
| totalVotes | Accumulator | Sum all tallies. |
| percentFor | Cast + guard | 100.0 * votes / total. |
How to master vote counting.
Search-or-add; then cast; then guard.
- MCQ Practice — verify search-or-add uses an early return after increment.
- Java Lab — build VoteCounter. Print both lists after each vote.
- FRQ Practice — combine the three patterns; treat percentFor like any percentage method.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 14 Vote Counting and Percentage FRQ Practice
AP-style topic practice assessment