CSCE 145 -- Fall 2008

Sections 4, 5, 6

Introduction to Algorithmic Design

Section 5 Lab Quiz 2

Lab Quiz 2


Friday, 28 March 2008.

You are to write a program to make a robot "plant" dots on a rectangular array and then to count the dots in a subset of that array.

Your main class should ask the user first for the density of dots to be put down. This should be a double value between 0.0 and 1.0 inclusive. Your program should check that the value given to the program is in fact between 0.0 and 1.0. You need not worry about errors if the value given to the program is not in fact in the format of a double.

Your program's main class should then ask for the sideLength of a square array to be planted with dots. As with the density, your main program should check that this is an integer between 1 and 20 inclusive but it need not worry that the input will be something other than an int value.

In either case, if the input is not correct, your program should terminate with a message explaining why it is terminating. You may take as a template the checking done with any of the bisection method, Newton method, or other numerical programs given as examples so far.

Your main class should then invoke a putDownDots(d,sideLength) method in a MyRobot extension of the RobotSE class. The d should be the density, and the sideLength should be the side length input to the main. The MyRobot constructor should have created a robot at location (0,0) facing EAST. This putDownDots() method should move the robot across a square array of sideLength intersections to the EAST and SOUTH of (0,0). At each intersection, the robot should invoke Math.random() to get a random number. If the random number is less than or equal to the density, then the robot should put a Thing on the intersection. (This means you should create a robot with 20*20=400 Things in the backpack to take into account the possibility of density 1.0 in a maximal 20 by 20 array.)

Your putDownDots() method should return the robot to the (0,0) location when it is finished putting down dots on the square array.

Your putDownDots() method should return to the main program a count of the number of dots put down in the square array, and the main program should print this out to the console window.

You main method should then invoke a countDots(int fromRow, int sideLength) method. The fromRow parameter should be an integer less than or equal to sideLength/2. This method should traverse sideLength/2 rows beginning with row fromRow and should count the number of dots that have been placed on that half of the square array. This number should be returned to the main method.

The main method should then test whether the number of dots counted in that half of the square array is within two of half the number of dots in total that were put down by the putDownDots() method. Whichever way the test comes out, the result should be printed to the console window.

Upload your finished code to the departmental drop box as usual.

Swearingen Engineering Center | Columbia, SC 29208 | 803.777.2880 | web@cse.sc.edu