CPSC 124, Winter 1998
Sample Answers to Lab 3
This page contains sample answers to some of the exercises from Lab #3 in CPSC 124: Introductory Programming, Winter 1998. See the information page for that course for more information.
Exercise 1: The problem was to modify an existing random walk program. It was only necessary to change a few lines of the program. However, the exercise also asked you to modify the comments so that they would be appropriate for the new program. (This is not just make-work. The idea is to encourage you to read the program carefully and understand how it works, why each variable was declared, etc.) In my solution, I have removed two variables, newRow and newCol, since they are not needed in the modified program. I didn't expect you to do this, but it does make the program cleaner.
/* In this program, a "disturbance" wanders randomly in a window. The window is actually made up of a lot of little squares. Initially, all the squares are black. But each time the disturbance visits a square, it becomes a slightly brighter shade of green (up to the maximum brightness). The action continues as long as the window remains open. Note that if the disturbance wanders off one edge of the window, it appears at the opposite edge. by David Eck, February 2, 1998 */ public class MosaicApplication { public static void main(String[] args) { MosaicFrame mosaic = new MosaicFrame(20,30,20,10); // Open a window with 30 rows and 30 columns of rectangles, // all initially black. int row = 15; // The row in which the disturbance is located. int col = 15; // The column in which the disturbance is located. // (Initial values of 15 put the disturbance in the middle // of the window.) while (mosaic.stillOpen()) { // repeat as long as the window stays open // Executing this while loop will move the disturbance // one space either left, right, up, or down, and increase // the level of green displayed in the square it visits. int rand = (int)(4*Math.random()); // a random number from 0 to 3, // used to decide which direction to move if (rand == 0) { // move left if (row > 0) row = row - 1; else // disturbance was already at left edge; move to right edge row = 29; } else if (rand == 1) { // move right if (row < 29) row = row + 1; else // disturbance was already at right edge; move to left edge row = 0; } else if (rand == 2) { // move up if (col > 0) col = col - 1; else // disturbance was already at top edge; move to bottom col = 29; } else { // move down if (col < 29) col = col + 1; else // disturbance was already at bottom edge; move to top col = 0; } int g = mosaic.getGreen(row,col); // Get current level of green in this square. mosaic.setColor(row,col,0,g+10,0); // Reset color with more green // (but still no red or blue). mosaic.delay(5); // insert a short delay between steps } // end of while loop } // end of main() } // end of class MosaicApplication
Exercise 2: The exercise was to write the program described in the comment:
/* This program displays a window containing a grid of little colored squares. Initially, the color of each square is set randomly. The program then selects one of the squares at random, selects one of its neighbors at random, and colors the selected square to match the color of its selected neighbor. This is repeated as long as the window is open. As the program runs, some colors disappear while others take over large patches of the window. (Note: For the purpose of determining the neighbor of a square, the bottom edge of the window is considered to be connected to the top edge, and the left edge is considered to be connected to the right edge.) David Eck, February 2, 1998 */ public class ConversionExperience { public static void main(String[] args) { MosaicFrame mosaic = new MosaicFrame(30,30); // Open a window with 30 rows and 30 columns of rectangles, // all initially black mosaic.fillRandomly(); // start by filling the mosaic with random colors while (mosaic.stillOpen()) { // repeat as long as the window stays open // Executing this while loop will select a random square (by selecting // a random row and a random column). It will then randomly select // a neighbor of that square (by randomly selecting one of the directions // up, down, left, or right). The color of the selected square is // changed to match the color of its selected neighbor. int row = (int)(30 * Math.random()); // randomly selected row int col = (int)(30 * Math.random()); // randomly selected column int neighborRow = row; // These will be the row and column of the int neighborCol = col; // randomly selected neighbor. They are // initialized to be the same as row and col, // but one of them will be changed by // the following if statement. int rand = (int)(4*Math.random()); // a random number from 0 to 3, // used to choose the direction in // which the neighbor lies if (rand == 0) { // choose neighbor to the left if (row > 0) neighborRow = row - 1; else // square is on the left edge; choose neighbor on right edge neighborRow = 29; } else if (rand == 1) { // choose neighbor to the right if (row < 29) neighborRow = row + 1; else // square is on the right edge; choose neighbor on left edge neighborRow = 0; } else if (rand == 2) { // choose neighbor above if (col > 0) neighborCol = col - 1; else // square is on the top edge; choose neighbor on bottom edge neighborCol = 29; } else { // choose neighbor below if (col < 29) neighborCol = col + 1; else // square is on the bottom edge; choose neighbor on top edge neighborCol = 0; } int r = mosaic.getRed(neighborRow,neighborCol); // Get color of neighbor int g = mosaic.getGreen(neighborRow,neighborCol); int b = mosaic.getBlue(neighborRow,neighborCol); mosaic.setColor(row,col,r,g,b); // set color of square to match neighbor mosaic.delay(5); // insert a short delay between steps } // end of while loop } // end of main() } // end of class ConversionExperience
Exercise 3: This exercise was postponed to Lab 5.
David Eck, 2 February 1998