[ Exercises | Chapter Index | Main Index ]

Solution for Programmming Exercise 4.5


This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.


Exercise 4.5:

The sample program RandomMosaicWalk.java from Section 4.6 shows a "disturbance" that wanders around a grid of colored squares. When the disturbance visits a square, the color of that square is changed. The applet at the bottom of Section 4.7 shows a variation on this idea. In this applet, all the squares start out with the default color, black. Every time the disturbance visits a square, a small amount is added to the red component of the color of that square. Write a subroutine that will add 25 to the red component of one of the squares in the mosaic. The row and column numbers of the square should be passed as parameters to the subroutine. Recall that you can discover the current red component of the square in row r and column c with the function call Mosaic.getRed(r,c). Use your subroutine as a substitute for the changeToRandomColor() subroutine in the program RandomMosaicWalk2.java. (This is the improved version of the program from Section 4.7 that uses named constants for the number of rows, number of columns, and square size.) Set the number of rows and the number of columns to 80. Set the square size to 5.


Discussion

This is an exercise in making a rather small modification to a relatively complicated existing program.

The only real problem is to write a new subroutine, which I will call brightenSquare. Much of the program comes directly from RandomMosaicWalk.java. The randomMove() routine is unchanged. The only change in the main() routine is to substitute a call to brightenSquare for the call to changeToRandomColor. The subroutines fillWithRandomColors and changeToRandomColor in the RandomMosaicWalk2 program are not needed in the new program and should be removed. In the three lines that define the constants, the values are changed according the instructions in the exercise:

final static int ROWS = 80;        // Number of rows in the mosaic.
final static int COLUMNS = 80;     // Number of columns in the mosaic.
final static int SQUARE_SIZE = 5;  // Size of each square in the mosaic.

With these values, the program is interesting to watch for a while. In the end, I did make one other small change to the main() routine to make the program run better: I change the delay in the call to Mosaic.delay(20) from 20 to 5 to make the animation run faster. You might want to try using shades of green, blue, or gray, instead of red. Or even use three disturbances, one incrementing the red component of the color, one incrementing the green component, and one incrementing the blue.

An outline for the brightenSquare routine is clear:

Let r be the current red component of the square
Add 25 to r
Set the color components of the square to  r, 0, 0

The green and blue components of the color will always be zero. However, they must be specified in the Mosaic.setColor() routine. Written in Java, the body of the routine is just three lines long:

static void brightenSquare(int row, int col) {
    int r = Mosaic.getRed(row,col);
    r += 25;
    Mosaic.setColor(row,col,r,0,0);
}

In fact, you could even write the body of the routine using just one line:

Mosaic.setColor(row, col, Mosaic.getColor(row,col) + 25, 0, 0);

One thing here might bother you: It looks like the value of the red component of a given square might get bigger than 255 if the disturbance visits it often enough. But the largest legal value for a color component is 255. What I haven't told you is that when a value greater than 255 is used for a color component, Mosaic.setColor will silently change the value to 255. If this were not the case, it would be necessary to rewrite brightenSquare to avoid illegal values of r:

static void brightenSquare(int row, int col) {
    int r = Mosaic.getRed(row,col);
    r += 25;
    if ( r > 255 )
        r = 255;
    Mosaic.setColor(row,col,r,0,0);
}

The Solution

/**
 * This program opens a Mosaic window that is initially filled with black.
 * A "disturbance" moves randomly around in the window.  Each time it visits
 * a square, the red component of the color of that square is increased
 * until, after about ten visits, it has reached the maximum possible level.
 * The animation continues until the user closes the window.
 */

public class Brighten {
   
   final static int ROWS = 80;        // Number of rows in the mosaic.
   final static int COLUMNS = 80;     // Number of columns in the mosaic.
   final static int SQUARE_SIZE = 5;  // Size of each square in the mosaic.
   
   static int currentRow;    // Row currently containing the disturbance.
   static int currentColumn; // Column currently containing disturbance.
   
   /**
    * The main program creates the window, fills it with random colors,
    * and then moves the disturbakcs in a random wals around the window
    * as long as the window is open.
    */
   public static void main(String[] args) {
      Mosaic.open( ROWS, COLUMNS, SQUARE_SIZE, SQUARE_SIZE );
      currentRow = ROWS / 2;   // start at center of window
      currentColumn = COLUMNS / 2;
      while (Mosaic.isOpen()) {
         brightenSquare(currentRow, currentColumn);
         randomMove();
         Mosaic.delay(5);
      }
   }  // end main
   
   /**
    * Add a bit of red to the rectangle in a given row and column.
    * Precondition:   The specified rowNum and colNum are in the valid range
    *                 of row and column numbers.
    * Postcondition:  The red component of the color of the square has
    *                 been increased by 25, except that it does not go
    *                 over its maximum possible value, 255.
    */
   static void brightenSquare(int row, int col) {
      int r = Mosaic.getRed(row,col);
      r += 25;
      Mosaic.setColor(row,col,r,0,0);
   }
   
   /**
    * Move the disturbance.
    * Precondition:   The global variables currentRow and currentColumn
    *                 are within the legal range of row and column numbers.
    * Postcondition:  currentRow or currentColumn is changed to one of the
    *                 neighboring positions in the grid -- up, down, left, or
    *                 right from the current position.  If this moves the
    *                 position outside of the grid, then it is moved to the
    *                 opposite edge of the grid.
    */
   static void randomMove() {
      int directionNum; // Randomly set to 0, 1, 2, or 3 to choose direction.
      directionNum = (int)(4*Math.random());
      switch (directionNum) {
      case 0:  // move up 
         currentRow--;
         if (currentRow < 0)
            currentRow = ROWS - 1;
         break;
      case 1:  // move right
         currentColumn++;
         if (currentColumn >= COLUMNS)
            currentColumn = 0;
         break; 
      case 2:  // move down
         currentRow ++;
         if (currentRow >= ROWS)
            currentRow = 0;
         break;
      case 3:  // move left  
         currentColumn--;
         if (currentColumn < 0)
            currentColumn = COLUMNS - 1;
         break; 
      }
   }  // end randomMove
   
} // end class Brighten

[ Exercises | Chapter Index | Main Index ]