2015 AP Comp Sci Free-Response Solutions

2015 AP CS FRQ Solutions

Today the 2015 APCS FRQs were released. Here are my solutions. I did create all the extra code to test all these classes and interfaces out as well. But these are just the answers to the questions. If you want a BlueJ project with classes for testing the code, click here. If you just want to browse the classes, click here.

Question 1

//part A
 public static int arraySum(int[] arr)
 {
    int sum = 0;

    for(int n : arr)
      sum += n;

    return sum;
 }

//part B
 public static int[] rowSums(int[][] arr2D)
 {
    int[] sums = new int[arr2D.length];

    for(int i = 0; i < sums.length; i++)
    {
       sums[i] = arraySum(arr2D[i]);
    }

    return sums;
 }

//part C
 public static boolean isDiverse(int[][] arr2D)
 {
    int[] sums = rowSums(arr2D);

    for(int i = 0; i < sums.length; i++)
       for(int j = i+1; j < sums.length; j++)
          if(sums[i] == sums[j])
             return false;

    return true;
 }

Question 2

public class HiddenWord 
{
   private String hidden;

   public HiddenWord(String h) 
   {
     hidden = h;
   }

   public String getHint(String hint) 
   {
     String r = "";

     for(int i = 0; i < hint.length(); i++) 
     {
       if(hint.charAt(i) == hidden.charAt(i))
         r += ""+hint.charAt(i);
       else if(hidden.indexOf(hint.charAt(i)) > -1)
         r += "+";
       else
         r += "*";
     }

     return r;
   }
}

Question 3

// part A
 public int getValueAt(int row, int col)
 {
    for(SparseArrayEntry e : entries)
    {
       if(e.getRow() == row && e.getCol() == col)
         return e.getValue(); 
    }

    return 0;
 }

 // part B
 public void removeColumn(int col)
 {
    numCols--;

    for(int i = entries.size()-1; i >= 0; i--)
       if(entries.get(i).getCol() == col)
          entries.remove(i);

    for(int i = 0; i < entries.size(); i++)
       if(entries.get(i).getCol() >= col) 
       {
          SparseArrayEntry h = entries.get(i);
          SparseArrayEntry e = 
             new SparseArrayEntry(h.getRow(),(h.getCol()-1),h.getValue());
          entries.set(i, e);
       }
 }

Question 4

// part A
public interface NumberGroup 
{
     public boolean contains(int num);
}

// part B
public class Range implements NumberGroup 
{
   private int[] list;

   public Range(int min, int max) 
   {
      list = new int[Math.abs(max-min+1)];
      for(int i = 0; i < list.length; i++)
          list[i] = min + i;
   }

   public boolean contains(int num)
   {
      for(int n: list) 
         if(num == n)
            return true;
      return false;
   }
}

// part C
public boolean contains(int num)
{
   for(NumberGroup n : groupList)
      if(n.contains(num))
         return true;
   return false;
}