You can view the original question from the collegeboard here. (You may need to log into APCentral first).
A window is represented by an M-by-N matrix filled with integers representing colors. Operations on a window include the following,
Consider the following declaration for window.
class window {
private int myNumRows;
private int myNumCols;
private int myMat[][];
public window(){ /* Implementation not shown */}
public boolean isInBounds(int row, int col){ /* Part A */}
public void colorSquare(int ULrow, int ULcol, int N, int val) { /*Part B
*/ }
/**
* valAt - this method returns the integer color value stored in the window
* matrix at row, col.
**/
public int valAt(int row, int col){/* Implementation not shown */}
}
//Other public member methods not shown
a. Write the window member method isInBounds, as started below. isInBounds checks whether a single point is in the window.
For example, for any 5-by-4 window W, the following table shows the results of several calls to isInBounds.
| Call | Return Value |
| W.isInBounds(0,1) | true |
| W.isInBounds(2,1) | true |
| W.isInBounds(4,3) | true |
| W.isInBounds(5,3) | false |
| W.isInBounds(3, -1) | false |
| W.isInBounds(8,8) | false |
Complete method isInBounds below.
/**
* isInBounds - This method returns true if the point (row, col) is
* in this window; otherwise, returns false
**/
public boolean isInBounds(int row, int col) {
Part b.
Write the window member method colorSquare, as started below. colorSquare sets all the integers in a specified square to a particular color value. ULrow and ULcol specify the location of the upper left corner of the square, N is the number of rows and columns in the square, and val is the color value. Points that are in the specified square but do not lie in the window are ignored.
For example, consider the following 5-by-6 window W.
| 10 | 10 | 10 | 10 | 10 | 10 |
| 10 | 10 | 10 | 20 | 20 | 20 |
| 20 | 20 | 30 | 30 | 30 | 30 |
| 30 | 30 | 40 | 40 | 40 | 40 |
| 40 | 40 | 50 | 50 | 50 | 50 |
After the call W.colorSquare(2, 1, 3, 66), W is changed to
| 10 | 10 | 10 | 10 | 10 | 10 |
| 10 | 10 | 10 | 20 | 20 | 20 |
| 20 | 66 | 66 | 66 | 30 | 30 |
| 30 | 66 | 66 | 66 | 40 | 40 |
| 40 | 66 | 66 | 66 | 50 | 50 |
After an additional call W.colorSquare(2, 4, 3, 77), W is changed to
| 10 | 10 | 10 | 10 | 10 | 10 |
| 10 | 10 | 10 | 20 | 20 | 20 |
| 20 | 66 | 66 | 66 | 77 | 77 |
| 30 | 66 | 66 | 66 | 77 | 77 |
| 40 | 66 | 66 | 66 | 77 | 77 |
Note that the third column of the square added is not in W.
In writing method colorSquare, you may call method isInBounds specified in part (a). Assume that isInBounds works as specified, regardless of what you wrote in part a.
Complete method colorSquare below.
/**
* colorSquare - after execution all points in this window that are also in the
* N-by-N square with upper left corner (ULrow, ULcol) have been set to val;
* points in the square that are not in this window are ignored.
**/
public void colorSquare(int ULrow, int ULcol, int N, int val) {
Part c.
A rectangular area in a window can be specified using the rectangle object as declared below.
|
Method Summary |
|
| int | ULrow() - row position of upper left corner of rectangle |
|
int |
ULcol() - column position of upper left corner of rectangle |
|
|
numRows() - number of rows in rectangle( height) |
|
|
numCols() - number of columns in rectangle (width) |
|
|
Other public and private members not shown |
The following example shows a 5-by-4 window in which the 3-by-2 rectangle with upper left corner (2,1) is highlighted.
| 10 | 20 | 30 | 40 |
| 10 | 20 | 30 | 40 |
| 10 | 99 | 55 | 40 |
| 10 | 44 | 33 | 40 |
| 10 | 77 | 66 | 40 |
Write the method enlarge, as started below. enlarge magnifies a rectangle in the window by replacing each point with a factor-by-factor square of points of the same color. The upper left corner of the magnified rectangle is the same as the upper left corner of the original rectangle. Each square of color is placed in the window at the same relative position in the magnified rectangle as the original point in the rectangle. Conceptually, the enlarged rectangle may run off the window, but only points in the window are modified by enlarge.
For example, consider the 10-by-11 window W, and rectangle R, where R.ULrow() = 2, R.ULcol() = 1, R.numRows() = 2, and R.numCols() = 4. The following table shows the original version of W with R highlighted and the result of magnifying R in W by a factor of 3.
Window W with R highlighted
| 00 | 00 | 00 | 10 | 10 | 10 | 10 | 10 | 10 | 10 | 10 |
| 10 | 10 | 10 | 20 | 20 | 20 | 20 | 20 | 20 | 20 | 20 |
| 20 | 55 | 99 | 33 | 66 | 20 | 20 | 20 | 30 | 30 | 30 |
| 30 | 22 | 88 | 77 | 44 | 30 | 30 | 30 | 40 | 40 | 40 |
| 40 | 40 | 40 | 40 | 50 | 50 | 50 | 50 | 50 | 50 | 50 |
| 50 | 50 | 50 | 40 | 40 | 40 | 40 | 30 | 30 | 30 | 30 |
| 60 | 60 | 50 | 50 | 50 | 40 | 40 | 40 | 30 | 30 | 20 |
| 70 | 70 | 70 | 50 | 50 | 50 | 40 | 40 | 40 | 30 | 30 |
| 80 | 80 | 70 | 70 | 60 | 60 | 50 | 50 | 40 | 40 | 30 |
| 90 | 80 | 70 | 60 | 60 | 50 | 50 | 40 | 40 | 30 | 20 |
Result of the call enlarge(W, R, 3)
| 00 | 00 | 00 | 10 | 10 | 10 | 10 | 10 | 10 | 10 | 10 |
| 10 | 10 | 10 | 20 | 20 | 20 | 20 | 20 | 20 | 20 | 20 |
| 20 | 55 | 55 | 55 | 99 | 99 | 99 | 33 | 33 | 33 | 66 |
| 30 | 55 | 55 | 55 | 99 | 99 | 99 | 33 | 33 | 33 | 66 |
| 40 | 55 | 55 | 55 | 99 | 99 | 99 | 33 | 33 | 33 | 66 |
| 50 | 22 | 22 | 22 | 88 | 88 | 88 | 77 | 77 | 77 | 44 |
| 60 | 22 | 22 | 22 | 88 | 88 | 88 | 77 | 77 | 77 | 44 |
| 70 | 22 | 22 | 22 | 88 | 88 | 88 | 77 | 77 | 77 | 44 |
| 80 | 80 | 70 | 70 | 60 | 60 | 50 | 50 | 40 | 40 | 30 |
| 90 | 80 | 70 | 60 | 60 | 50 | 50 | 40 | 40 | 30 | 20 |
In writing enlarge, you may call any public window member method. Assume that isInBounds and colorSquare work as intended regardless of what you wrote in parts a and b.
Complete method enlarge below.
/**
* enlarge - Before entering this method factor has been set to a number
* greater than 0. The method magnifies a particular rectangle in a
* window under the conditions specified above.
**/
public void enlarge(window W, rectangle rect, int factor) {