Introduction

I created solutions to these as an exercise and to compare against my mental record of my submissions on the exam. Advanced solutions with methods outside of the AP CSA subset are located here.

Questions

CollegeBoard® released the AP Computer Science A Free Response Questions on AP Central, accessible here.

Question 1 (DogWalker)

public int walkDogs(int hour) {
    int available = company.numAvailableDogs(hour);
    int dogs = available;
    if (dogs > maxDogs) {
        dogs = maxDogs;
    }
    company.updateDogs(hour, dogs);
    return dogs;
}

public int dogWalkShift(int startHour, int endHour) {
    int total = 0;
    for (int h = startHour; h <= endHour; h++) {
        int walked = walkDogs(h);
        int earnings = walked * 5;
        if (walked == maxDogs || (h >= 9 && h <= 17)) {
            earnings += 3;
        }
        total += earnings;
    }
    return total;
}

Question 2 (SignedText)

public class SignedText {
    private String first;
    private String last;

    public SignedText(String firstName, String lastName) {
        first = firstName;
        last = lastName;
    }

    public String getSignature() {
        if (first.equals("")) {
            return last;
        }
        return first.substring(0, 1) + "-" + last;
    }

    public String addSignature(String text) {
        String sig = getSignature();
        int sigLen = sig.length();

        if (text.length() >= sigLen && text.substring(0, sigLen).equals(sig)) {
            String remainder = text.substring(sigLen);
            return remainder + sig;
        }

        if (text.length() >= sigLen && text.substring(text.length() - sigLen).equals(sig)) {
            return text;
        }

        return text + sig;
    }
}

Question 3 (Round.java)

public Round(String[] names) {
    competitorList = new ArrayList<Competitor>();
    for (int i = 0; i < names.length; i++) {
        competitorList.add(new Competitor(names[i], i + 1));
    }
}

public ArrayList<Match> buildMatches() {
    ArrayList<Match> matches = new ArrayList<Match>();
    int size = competitorList.size();
    int left = (size % 2 == 1) ? 1 : 0; // skip best if odd
    int right = size - 1;
    while (left < right) {
        Competitor c1 = competitorList.get(left);
        Competitor c2 = competitorList.get(right);
        matches.add(new Match(c1, c2));
        left++;
        right--;
    }
    return matches;
}

Question 4 (SumOrSameGame.java)

public SumOrSameGame(int numRows, int numCols) {
    puzzle = new int[numRows][numCols];
    java.util.Random rand = new java.util.Random();
    for (int r = 0; r < numRows; r++) {
        for (int c = 0; c < numCols; c++) {
            puzzle[r][c] = rand.nextInt(9) + 1;
        }
    }
}

public boolean clearPair(int row, int col) {
    int val = puzzle[row][col];
    if (val == 0) {
        return false;
    }
    for (int r = row; r < puzzle.length; r++) {
        for (int c = 0; c < puzzle[0].length; c++) {
            if (r == row && c == col) {
                continue;
            }
            int other = puzzle[r][c];
            if (other != 0 && (other == val || other + val == 10)) {
                puzzle[row][col] = 0;
                puzzle[r][c] = 0;
                return true;
            }
        }
    }
    return false;
}