CS Labs: Lab 11

CS Labs: Lab 11


An Array of Arrays - Part 2

There are 6 checkpoints , plus the clean-up checkpoint, in this lab. You and your partner should work together using just one of your accounts. CHANGE WHO IS CONTROLLING THE COMPUTER AFTER EACH CHECKPOINT! If you need help with any exercise, raise your hand.

Copy the lab materials to your account, using the -r option on the Unix cp command in order to copy not only files, but directories, by entering

             cp -r /home/Classes/Cs1/Labs/Lab11 .
     
After running this copy command, you will see a new directory in your account called Lab11. What you copied was actually a directory, plus all of its contents. Everything you need for the lab exercises today is contained in this new directory.

Change directories into Lab11 and start running BlueJ.

Self-Referencing Pointers

Open the project Star, and edit the implementation of the class Point.

Finish the implementation of the constructor Point(int, int). Notice that the names of the formal parameters conflict with the names of a Point object's private instance variables. Remember how we can deal with this without changing either set of variable names. Hint: see section 9.5 of your textbook and review how this is used. Use the self-referencing pointer to makes your program changes in both this checkpoint and the next.

1 Use BlueJ to create an instance of the Point class for us using this constructor.

Next, you will finish the implementation of the default constructor Point(). When the default constructor constructs a Point, it should initialize that point to (0, 0). For this implementation, do not initialize the private instance variables of the Point object directly. You have another means available to you for changing the values of instance variables; use it. Hint: review the two constructors shown on the lower half of page 444 of your textbook.

2 Create an instance of the Point class for us using this constructor.

Connect the Dots

Edit the implementation of the Demo class. What steps are happening here? As you can see, the main() method creates an array of points. Your job is to finish the for loop so that all the points next to each other are connected by a line. In other words, connect points 0 and 1, 1 and 2, etc., through 11 and back to 0. Note that points[11] must be connected to points[0] by a line. Do not implement this by adding a separate statement! Hint: The modulus operator is your friend. You should have the following line drawing when you run the program:



Note that the program will close the drawing window automatically after a few seconds.

3 Show us how you completed the for loop, and run the program so we can see the Star.

Close the Star project.

Two Dimensional Arrays

Open the GraphPaper project. The Demo class first creates a 2-dimensional array of Points. Conceptually, the result of this is as follows, but the points are not actually 'displayed':

You and your partner should complete the code in the main() method to draw lines connecting the points, in order to produce a grid as seen in the following diagram:

Note that you need to draw only 2 * (NUM_SQUARES + 1) lines, with each line connecting the two points on opposite sides of the grid. Use two sequential for loops, one to go through the rows and draw the horizontal lines, and one to go through the columns and draw the vertical lines. Remember that the method for drawing a line from point (x1,y1) to (x2,y2) is written: [drawing_board_reference_variable].drawLine(x1,y1,x2,y2).

4 Run the program and show us how you successfully drew the lines.

After your code for drawing the graph lines, add an additional for loop to print out circles in the center of the squares down the main diagonal (see the picture, below).

Hint: see the online documentation (or page 274 in the text) to refresh your memory about the drawCircle method found in the DrawingBoard class. The radius of each circle should be SQUARE_SIZE / 3. You can calculate the coordinates for the center of each circle by averaging the coordinates of opposite corners of the square in which you are drawing the circle.

5 Run the program for us and show us how you implemented the circle-drawing inside the squares.

Close project GraphPaper.

Vectors: the Bottomless Pit

Open project LotsOfPoints. Examine the code in the Demo class. This program creates a random number of Points all with random coordinates, and stores them in a Vector.

Review page 473 of your textbook to see how to use an Enumeration object with your Vector. We want you to use an Enumeration object to go through that vector and examine each Point object. Your goal is to find the average of the x-coordinates and the average of the y-coordinates for all the Points in the Vector using your Enumeration object. On the way to finding the averages, you can use the already declared int variables xSum and ySum to hold your cumulative sums. Notice that there is code that you will need to uncomment that will draw the average Point in a larger size, once you have calculated its center.

6 Run the program for us and show how you calculated the average of the coordinates.

Close project LotsOfPoints.

After the Lab

Be sure to exit Netscape before you log out.

7 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Author: Susan Haller, University of Wisconsin-Parkside
Acknowledgment: I would like to thank Erica Eddy and Lester I. McCann, my colleagues at the University of Wisconsin-Parkside, for their corrections, updates, and revisions to this series of labs.