9.7.1. Free Response - Gray Image A¶
The following is part a of a free response question from 2012. It was question 4 on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.
Question 4. A grayscale image is represented by a 2-dimensional rectangular array of pixels (picture elements). A pixel is an integer value that represents a shade of gray. In this question, pixel values can be in the range from 0 through 255, inclusive. A black pixel is represented by 0, and a white pixel is represented by 255. The declaration of the GrayImage
class is shown below.
public class GrayImage
{
public static final int BLACK = 0;
public static final int WHITE = 255;
/**
* The 2-dimensional representation of this image. Guaranteed not to be null.
* All values in the array are within the range [BLACK, WHITE], inclusive.
*/
private int[][] pixelValues;
/**
* @return the total number of white pixels in this image. Postcondition: this
* image has not been changed.
*/
public int countWhitePixels()
{
/* to be implemented in part (a) */
}
}
Part a. Write the method countWhitePixels
that returns the number of pixels in the image that contain the value WHITE
. For example, assume that pixelValues
contains the following image.
![../_images/grayImageA.png](../_images/grayImageA.png)
Figure 1: Example 2D array¶
A call to countWhitePixels
method would return 5 because there are 5 entries (shown in boldface)
that have the value WHITE
.
public class GrayImage
{
public static final int BLACK = 0;
public static final int WHITE = 255;
/**
* The 2-dimensional representation of this image. Guaranteed not to be null.
* All values in the array are within the range [BLACK, WHITE], inclusive.
*/
private int[][] pixelValues;
/**
* @return the total number of white pixels in this image. Postcondition: this
* image has not been changed.
*/
public int countWhitePixels()
{
/* to be implemented in part (a) */
}
}
9.7.1.1. How to solve this problem¶
To solve this problem, we will need to loop through the entire 2D array, looking for instances of a WHITE
pixel, keeping track of our count during the loop.
- single for each loop
- This is a two-dimensional array so you would need a nested for-each loop.
- nested for loop
- Correct!
- nested while loop
- You could use a nested while loop, but since you know the numbers of rows and columns a nested for loop is usually better since with a while loop you could forget to increment the row or column index.
9-10-1: What kind of loop could you use to solve this problem?
- nested for each loop
- Correct!
- single for loop
- For a two-dimensional array you would need to use a nested for loop.
- nested switch statement
- Nested switch statements would not work in this situation and are generally convoluted and difficult to read.
9-10-2: What is another kind of loop you could use to solve this problem?
Looping through a 2D array is more complicated than the simple arrays we usually see, requiring nested for loops. Check out the questions and code below, which displays how nested for loops work to display a block of numbers.
9.7.1.2. Algorithm¶
When approaching this problem, it can be helpful to look for keywords or hints that maybe be in the problem statement. This section contains a plain English explanation of one way to solve this problem as well as problems that test your understanding of how to write the code to do those things. Click on the buttons to reveal the questions.
9-10-5: Explain in plain English what your code will have to do to answer this question. Use the variable names given above.
- pixelValues
- Pixel values is a private member variable of the overall class, it is not the class that contains countWhitePixels
- greyimage
- Capitalization and spelling are important! Check the class name again carefully.
- countWhitePixels
- This is the name of the method you are writing! Since it is not a constructor, the overall Class name cannot be countWhitePixels
- GrayImage
- Correct!
9-10-6: Which class is countWhitePixels
a method in?
- pixelValues
- Correct!
- You need to initialize your own
- This method iterates through an already existing image, which can be found in the GreyImage class initialization.
- countWhitePixels
- This is the name of the method you are writing, not an array.
- This method is called using the dot operation so you can just write "this"
- Although this method is called with the dot operator, you still need to specify the name of the array and cannot only write "this"
9-10-7: What array will you be modifying in this method?
There are many ways to solve this question, but we will only cover two in this section. Although it is a good exercise to be able to write the solution in multiple ways, you do not need to write both for the AP exam and you can just look at the problems below which relate to the method that is more intuitive to you.
If you want to solve this problem using nested for loops, complete the three questions below.
- (int row = 0; row < pixelValues.length - 1; row++)
- This does not iterate through all the rows of the array
- (int row = 0; row < pixelValues.length; row++)
- Correct
- (int row = 0; row < pixelValues.height; row++)
- The .height method does not exist
- (int row = 0; row < pixelValues.height - 1; row++)
- The .height method does not exist
9-10-8: What could you write for the outer for loop so that it iterates through the rows of the array?
- (int col = 0; col <= row; col++)
- This does not correctly iterate through all the columns in the array
- (int col = 0; col < pixelValues[0].length - 1; col++)
- This stops one short of iterating through the whole array
- (int col = 0; col < row; col++)
- This does not correctly iterate through all the columns in the array
- (int col = 0; col < pixelValues[0].length; col++)
- Correct!
9-10-9: What could you write for the inner for loop so that it iterates through the columns of the array?
- if (pixelValues[row][col] == 0)
- 0 is actually equal to black
- if (pixelValues[col][row] == 255)
- the column and row variables are switched, so the array could possibly go out of bounds.
- if (pixelValues[row][col] == WHITE)
- Correct!
- if (pixelValue == WHITE)
- pixelValue does not access the integers stored in the array
9-10-10: how could you check if the current value is white?
Alternatively, if you want to solve this problem using nested for each loops, complete the three questions below.
- (int[] row; row < pixelValues; row++)
- This is not the correct way to initialize a for each loop
- (int row : this.pixelValues)
- When looping through an array with a for each loop you need to include the '[]'
- (int[] row : this.pixelValues)
- Correct!
- (int[] row ; pixelValues)
- A for each loop has a colon not a semicolon.
9-10-11: What could you write for the outer for loop so that it iterates through the rows of the array?
- (int col = 0; col < pixelValues.length; col++)
- This is not the correct way to initialize a for each loop
- (int pv : row)
- Correct!
- (int pv[] : row)
- For the inner bound, you are no longer iterating through an array so you do not need to include the '[]'
- (int col : pixelValues)
- Your outer bound is already iterating through pixelValues, your inner bound needs to loop through something else
9-10-12: What could you write for the inner for loop so that it iterates through the columns of the array?
- if (pv == this.WHITE)
- Correct!
- if (pv == {255,255,255})
- Our image is in greyscale and is not a three value color
- if (pv == 0)
- 0 is the value of black
- if (pv == white)
- When using private variables, you need to make sure the spelling is the same.
9-10-13: How will you check if pv is white?
9.7.1.3. Try and Solve It¶
FRQ Gray Image A: write the code for the method countWhitePixels
. When you are ready click “Run” to test your solution.
9.7.1.4. Video - One way to code the solution¶
The following video is also on YouTube at https://youtu.be/Rx4bPs0wkxU. It walks through coding a solution.