Question: ( Name this program StuPreE 3 Select ) Image processing relies on multi - dimensional arrays. Write a method selectRect that simulates selecting and returning

(Name this program StuPreE3Select) Image processing relies on multi-dimensional arrays. Write
a method selectRect that simulates selecting and returning a rectangular area of a 2D image.
// Returns a full 2d int array with data from the rectangular area
// specified by startRow, startCol, endRow, and endCol (assume
// startRow<=endRow and startCol<=endCol) if those four indexes are
// valid.
// Return null if image is null or has zero rows or zero cols.
// Also return null if any row/col index is invalid.
public static int[][] selectRect(int[][] image,
int startRow, int startCol,
int endRow, int endCol){
// ADD code
}
Your method should return null on invalid or bad parameters as explained in the given method
prolog comment. It should also test for invalid starting/ending row/col values (outside of valid
index ranges of the 2D array parameter) and return null. Other than those, assume
startRow<=endRow and startCol <= endCol, that is, your method only needs to handle a start
position of a top left corner to an end position of a bottom righ corner, if they are within the
ranges.
For example, given this 2d image array (5 rows and 9 cols):
012345678
101112131415161718
202122232425262728
303132333435363738
404142434445464748
A start position (1,2) to an end position (3,7) would be a valid selection:
// from top left corner to bottom right corner
012345678
101112131415161718
202122232425262728
303132333435363738
404142434445464748
For the example 2D array, a start/end position of (-1,2) or (3,9) would be invalid.
CS252 Object-Oriented Programming
Page 3 of 5
Be aware that an area formed by the same start position and end position, such as (3,5) ~ (3,5)
covers one single pixel and is still valid. That would lead to a 2d array with one single row and
one single column.
Your main() should test the method following the example below. Include at least two additional
valid cases (besides the one shown below) and one invalid case in your main().
int[][] image = new int[][]{
{0,1,2,3,4,5,6,7,8},
{10,11,12,13,14,15,16,17,18},
{20,21,22,23,24,25,26,27,28},
{30,31,32,33,34,35,36,37,38},
{40,41,42,43,44,45,46,47,48}
};
System.out.println(Arrays.deepToString(image)); // original
int startRow =1, startCol =2, endRow =3, endCol =7;
int[][] select; // store selected area
//(valid 1)(1,2) ~ (3,7)
select = selectRect(image, startRow, startCol, endRow, endCol);
System.out.printf("(%d,%d)-(%d,%d): %s
", startRow, startCol,
endRow, endCol, Arrays.deepToString(select));
// reuse variables to add additional testing cases
...
The above segment of code will produce this output:
[[0,1,2,3,4,5,6,7,8],[10,11,12,13,14,15,16,17,18],
[20,21,22,23,24,25,26,27,28],[30,31,32,33,34,35,36,37,
38],[40,41,42,43,44,45,46,47,48]]
(1,2)-(3,7): [[12,13,14,15,16,17],[22,23,24,25,26,27],
[32,33,34,35,36,37]]
Arrays.deepToString() is similar to Arrays.toString() but works with 2D arrays (and
higher dimension arrays). You need: import java.util.Arrays;
A downside of Arrays.deepToString() is that it always returns the content in one long string. Feel
free to replace it with a method of your own to print a 2D array in a nicer format

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!