Last active
November 27, 2018 18:00
-
-
Save CompSciRocks/d83bf0f9e8f9b24ddce9851e5d3a0cdb to your computer and use it in GitHub Desktop.
Solution for GrayImage free response question on 2012 AP Computer Science exam - https://compsci.rocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int countWhitePixels() { | |
int cnt = 0; | |
for (int r=0; r<pixelValues.length; r++) { | |
for (int c=0; c<pixelValues[r].length; c++) { | |
if (pixelValues[r][c] == WHITE) | |
cnt++; | |
} | |
} | |
return cnt; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int countWhitePixels() { | |
int cnt = 0; | |
for (int[] row: pixelValues) { | |
for (int val: row) { | |
if (val == WHITE) | |
cnt++; | |
} | |
} | |
return cnt; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GrayImage { | |
public static final int BLACK = 0; | |
public static final int WHITE = 255; | |
private int[][] pixelValues; | |
public int countWhitePixels() { | |
// Part A | |
} | |
public void processImage() { | |
// Part B | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void processImage() { | |
for (int r=0; r<pixelValues.length-2; r++) { | |
for (int c=0; c<pixelValues[r].length-2; c++) { | |
pixelValues[r][c] -= pixelValues[r+2][c+2]; | |
if (pixelValues[r][c] < BLACK) | |
pixelValues[r][c] = BLACK; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment