GrayImage free response answer from the 2012 AP Computer Science Exam

The GrayImage problem from the 2012 AP Computer Science Exam is typical of free response problems that test 2 dimensional arrays.

Review the GrayImage free response solution with AP CS Tutor Brandon Horn.

GrayImage Part (a): countWhitePixels

  public int countWhitePixels()
  {
    int whitePixels = 0;
    
    for(int row = 0; row < this.pixelValues.length; row++)
      for(int col = 0; col < this.pixelValues[row].length; col++)
        if(this.pixelValues[row][col] == WHITE)
          whitePixels++;
    
    return whitePixels;
  }

GrayImage Part (b): processImage

  public void processImage()
  {
    for(int row = 0; row < this.pixelValues.length; row++)
    {
      for(int col = 0; col < this.pixelValues[row].length; col++)
      {
        int otherRow = row + 2;
        int otherCol = col + 2;
        
        if(otherRow < this.pixelValues.length &&
             otherCol < this.pixelValues[otherRow].length)
        {
          this.pixelValues[row][col] -= this.pixelValues[otherRow][otherCol];
          if(this.pixelValues[row][col] < BLACK)
            this.pixelValues[row][col] = BLACK;
        }
      }
    }
  }

Get AP CS Help

2012 AP CS Exam Free Response Solutions

Leave a Reply