APCS 2015 FRQs Proposed Solutions

NOTES:
(1) There may be typos.
(2) While I am fairly certain that these solutions are correct, I may have made some errors.

1a
int c = 0;
for (int a : arr)
…c += a;
return c;

1b
int[] ret = new int[arr2d.length];
for (int k = 0; k < arr2d.length; k++)
…ret[k] = arraySum(arr2d[k]);
return ret;

1c
int[] sums = rowSums(arr2d);
for (int k = 0; k < sums.length – 1; k++)
…for (int j = k + 1; j < sums.length; j++)
…if (sums[k] == sums[j])
…return false;
return true;

2
public class HiddenWord {
…private String key;
…public HiddenWord(String k) { key = k; }
…public String getHint(String guess) {
…String hint = “”;

…for (int i = 0; i < key.length() – 1; i++) {
…String guessLetter = guess.substring(i, i + 1);
…if (guessLetter.equals(key.substring(i, i + 1)
…hint += guessLetter;
…else if (key.indexOf(guessLetter) < 0)
…hint += “*”;
…else
…hint += “+”;
…}
…return hint;
…}
}

3a
for (int i = 0; i < entries.size(); i++) {
…SparseArrayEntry e = entries.get(i);
…int tarRow = e.getRow();
…int tarCol = e.getCol();
…if (tarRow == row && tarCol == col)
…return e.getValue();
}
return 0;

3b
numCols -= 1;
int i = 0;
while (i < entries.size()) {
…SparseArrayEntry e = entries.get(i);
…if (e.getCol() == col)
…entries.remove(i);
…else if (e.getCol() > col) {
…int r = e.getRow();
…int c = e.getCol()- 1;
…int v = e.getValue();
…SparseArrayEntry replace = new SparseArrayEntry(r, c, v);
…entries.set(i, replace);
…i++;
…}
…else
…i++;
}

4a
public interface NumberGroup {
…boolean contains(int num);
}

4b
public class Range implements NumberGroup {
…private int min;
…private int max;
…public Range(int mi, int ma) {
…min = mi;
…max = ma;
…}

…public boolean contains(int num) {

…if (num >= min && num <= max)
…return true;
…return false;
…}
}

4c
for (int i = 0; i < groupList.size(); i++) {
…NumberGroup ng = groupList.get(i);
…if (ng.contains(num));
…return true;
}
return false;

Your loop bound in question 2 (getHint()) is wrong; you’ll always stop before the last character.

@jkhuggins Ya, that’s right. Line should read:
for (int i = 0; i < key.length(); i++) {

Another set, written by an APCS textbook author: http://www.skylit.com/beprepared/fr2015.html