01
Overview

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.

02
Core Concept

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

  1. Parallel ArrayLists for records.
  2. Search-or-add for vote registration.
  3. Cast + zero-guard for percentage.
03
Key Vocabulary

Vote-counting vocabulary.

Used in tally and election problems.

Vocabulary
  • 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.
04
AP Exam Focus

How vote-counting is tested.

Search-or-add and percentage math dominate.

Exam Focus
  • 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.
05
Worked Example

Trace votes coming in.

Search-or-add at work.

Worked Example AP-style reasoning
  • 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.
06
Common Mistakes

Vote-counting pitfalls.

Sync and cast errors.

Watch Out
  • 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.
07
Reference Table

Vote counting structure.

Three integrated patterns.

Vote Counter Reference

AP Quick Reference
MethodPatternKey step
recordVoteSearch-or-addIncrement or append both.
totalVotesAccumulatorSum all tallies.
percentForCast + guard100.0 * votes / total.
08
Practice Tip

How to master vote counting.

Search-or-add; then cast; then guard.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 19 marks Pending

AP CSA Unit 13 Topic 14 Vote Counting and Percentage FRQ Practice

AP-style topic practice assessment

Start