Question 3041
A grayscale image is represented as a rectangular 2D array of pixel values, where each entry is an integer in [0, 255]. The class ImageFilter provides operations on such images.
public class ImageFilter
{
/** The pixel grid. pixels[r][c] is the pixel at row r, column c.
* Each value is in [0, 255]. The grid has at least one row and
* one column, and every row has the same length. */
private int[][] pixels;
/** Constructs an ImageFilter around the given grid. */
public ImageFilter(int[][] p)
{
pixels = p;
}
/** Returns the maximum pixel value in the row range
* [startRow, endRow] (inclusive on both ends). Part (a) */
public int brightestIn(int startRow, int endRow)
{
/* part (a) */;
}
/** Applies an "invert then clip" filter to every pixel of pixels,
* modifying pixels in place. Part (b) */
public void invertAndClip(int floor)
{
/* part (b) */;
}
// Other methods are not shown.
}Part A
Write the method brightestIn. It returns the largest pixel value found anywhere in rows startRow through endRow of pixels, inclusive on both ends.
For example, suppose pixels is the following \(4 \times 5\) grid.
{ { 40, 12, 7, 30, 50 },
{ 15, 80, 20, 10, 5 },
{ 33, 33, 33, 33, 33 },
{ 99, 0, 60, 5, 42 } }
Then brightestIn(0, 1) returns 80, brightestIn(2, 2) returns 33, and brightestIn(0, 3) returns 99.
Assume 0 <= startRow <= endRow < pixels.length.
Part B
Write the method invertAndClip. It modifies pixels in place by applying the following to every pixel p:
- Compute the inverted value
255 − p. - If that inverted value is strictly less than
floor, replace the pixel withfloor(clip upward). - Otherwise, store the inverted value.
For example, if floor is 50 and a pixel has value 210, then \(255 - 210 = 45\), which is below 50, so the pixel becomes 50. If a pixel has value 100, then \(255 - 100 = 155\), which is not below 50, so the pixel becomes 155.
Using the \(4 \times 5\) example from Part A and calling invertAndClip(50), the resulting grid is:
{ { 215, 243, 248, 225, 205 },
{ 240, 175, 235, 245, 250 },
{ 222, 222, 222, 222, 222 },
{ 156, 255, 195, 250, 213 } }
(Every inverted value was already at least 50.) If instead the row { 240, 250, 252, 200, 220 } were present, calling invertAndClip(50) would produce { 50, 50, 50, 55, 50 } — four pixels clip up to the floor. Note that a pixel whose inverted value equals floor is not clipped. Assume 0 <= floor <= 255.
Main Solution
DETAILED SOLUTION — Question 46
Sample Solution
// Part (a)
public int brightestIn(int startRow, int endRow)
{
int best = pixels[startRow][0];
for (int r = startRow; r <= endRow; r++)
{
for (int c = 0; c < pixels[r].length; c++)
{
if (pixels[r][c] > best)
{
best = pixels[r][c];
}
}
}
return best;
}
// Part (b)
public void invertAndClip(int floor)
{
for (int r = 0; r < pixels.length; r++)
{
for (int c = 0; c < pixels[r].length; c++)
{
int inv = 255 - pixels[r][c];
if (inv < floor)
{
pixels[r][c] = floor;
}
else
{
pixels[r][c] = inv;
}
}
}
}
Rubric (9 pts): (a) 4 pts — iterates rows startRow..endRow inclusive, iterates all columns of each row, tracks a running max, returns the max. (b) 5 pts — visits every pixel in pixels, computes 255 − p, clips only when inverted is strictly less than floor, writes the result back in place to pixels[r][c], overall behavior matches spec (including the "equal to floor does NOT clip" edge case).
Teacher Explanation
255 − p and applying a strict less-than clip condition, storing results back in place.Education Coach Advice
Track the maximum across the inclusive row range. Initializing from an actual pixel is general and clear; for the stated [0,255] domain, zero or Integer.MIN_VALUE also works. Invert every pixel and store the larger of its inverted value and floor.
Traps
Visit all columns in every required row and update pixels in place for Part B. At equality, using <= rather than < assigns the same value and does not change the observable result. Do not reject a correct maximum initialized to zero for the nonnegative pixel domain.
Answer images

