Cookie Program

advertisement

CSC 161 -- Computer Science II

PURPOSE:

Winter, 2016

Program 2

(100 points)

 To develop a well-crafted, modular, well documented program

 To become more familiar with the constructs, and features of Java

 To make appropriate use of public and private methods

 To pass parameters and use local variables appropriately

 To gain additional experience using 2 dimensional arrays

 To correctly use nested loops

 To work with a random number generator and an input seed

 To use a Boolean controlled loop

Program Specifications for Cookie.java

Version I

(due Tuesday, February 2)

ASSIGNMENT: A dog, Cookie, has buried three bones randomly in her 50 foot by 50 foot backyard. However she has forgotten where the bones are buried and so she randomly begins to dig holes. Her nose is so good, and her holes so big, that she will find a bone if she digs within one foot of it. That is, if a bone is buried at point (X,Y), Cookie will find it if she digs at (X,Y), (X-1,Y), (X+1,Y), (X,Y-1) or (X,Y+1). Assume that

X and Y are integers and Cookie only digs at points with integer coordinates. Assume also that Cookie is so dumb that she may dig the same hole more than one time.

Write a program consisting of a Cookie class with multiple methods that randomly buries three bones and then randomly digs holes (and counts them) until a bone is found.

The seed to the random number generator is to be input by the user. Note that the program should report the exact location where a bone was found. So, for example, if Cookie were to dig at (23, 48) and find a bone that had been buried at (24, 48), you should report that it was found at (24, 48). Have the program repeat the experiment fifteen times and average the results for the 15 trials. Direct the output to the screen using the format shown. Your output must include the properly aligned set of columns illustrated below.

Digging Up Bones -- Your name

Trial Bone Coordinates Bone Found # Holes

1 ( 2, 6) (24,48) (19, 3) (24,48)

2 ( 8,14) ( 9,20) ( 7,12) ( 7,12)

.

.

.

15 (50,16) (44,32) ( 6,12) (50,16)

171

414

.

331

Average number of holes dug to find a bone: 198.6

HAND IN:

General and detailed pseudocode

The source code listing

for each of the methods in your program.

for your completed program.

Output reports from running the program twice , first with a seed of 17 and then with a seed you choose. Both runs will generate a complete report of the 15 trials.

SAVE YOUR PROGRAM ON THE K: DRIVE:

Place your source code file, Cookie.java

, in a folder named Prog2_Cookie under your class K: drive Programs folder.

Sample Output for Version 1

Please enter an integer to use as a seed for simulation: 17

Digging Up Bones – Your name goes here

Trial Bone Coordinates Bone Found # Holes

---------------------------------------------------------------

1 (27, 21) (45, 17) (43, 44) (43, 44) 241

2 (25, 29) (28, 38) (17, 25) (17, 25) 216

3 ( 5, 36) (49, 23) (47, 23) ( 5, 36) 184

4 (33, 2) ( 7, 42) (11, 46) (11, 46) 205

5 (17, 37) (31, 30) (39, 40) (31, 30) 153

6 ( 6, 46) (40, 15) (40, 19) ( 6, 46) 97

7 (18, 37) (37, 43) (31, 1) (37, 43) 37

8 (34, 16) (49, 19) (15, 30) (15, 30) 72

9 ( 8, 50) (34, 50) (18, 32) (18, 32) 227

10 (13, 6) (25, 16) (33, 45) (13, 6) 18

11 (27, 46) (35, 17) (47, 31) (35, 17) 220

12 (32, 21) (22, 38) (19, 22) (32, 21) 130

13 (26, 13) (12, 9) (37, 38) (12, 9) 13

14 (42, 50) (38, 32) (37, 31) (37, 31) 234

15 (27, 35) (27, 11) ( 1, 36) ( 1, 36) 492

Average number of holes dug to find a bone: 169.3

Program Specifications for Version II of the Cookie Program

(due Tuesday, February 9)

In your class K: drive Prog2_Cookie folder, make a copy of your Cookie.java

file and call it BoneGame.java

. This is the file you will now begin working in. Rename the class in the new file BoneGame to match the file name. In this new file you will make changes and enhancements to your original Cookie program.

I will provide you with the client program ( Cookie2 ) that creates and uses BoneGame objects to play the game. That will be its only purpose. The yard array will now “belong” to the

BoneGame class. Other needed BoneGame data fields will be yardSize , numTrials , and numBones . It will also have a Random object to generate random numbers. These should all be private. The only variables in the client program will be ones that hold data it needs to pass to

BoneGame constructors when BoneGame objects are created.

The BoneGame class will have the following constructors.

1.

A default constructor that sets yardSize to 50 (for a 50X50 yard in a 52X52 array), numTrials to 15, and numBones to 3. It should use the default seed 13 for its random number generator.

2.

An overloaded constructor with two int parameters: the first receives the yard size and the second receives the random number generator seed. It uses a default value of 15 for numTrials and 3 for numBones .

3.

An overloaded constructor with four int parameters: yardSize , numTrials , numBones , and seed .

The class should have the additional public method: play() . This method will essentially do what the main method did in your original Part I program, except it will not be responsible for setting a random number seed because that was already handled by the constructor when the object was created. The play() method will control the "play" of the game. Any other class methods it calls to help it should be private.

My client program will simply create and then call play() for each of three BoneGame objects whose names are game1 , game2 , and game3 . To keep things simple, the seeds this program passes to the BoneGame constructors will be hard coded into the program.

 game1 will be created using constructor #3, with invalid arguments for yardSize , numTrials , and numBones .

 game2 will be created using constructor #2, and will have a yardSize of 30.

 game3 will be created using constructor #3, with a yardSize of 40, 20 for numTrials , and 5 for numBones .

Handling the yard size: The BoneGame class should use the yard size passed to the constructor to dynamically allocate the memory for the yard array. Remember to make it 2 rows and 2 columns larger than the user specified size. For example, if the user passes the constructor a yard size of 50, the yard should be created as a 52 X 52 array with rows and columns 1-50 representing the “yard” and rows and columns 0 and 51 being used as a dirt border around it.

DATA VALIDATION AND DEFAULT VALUES WITHIN THE BoneGame CLASS:

If the user specifies a yard size less than 5 or greater than 50, the constructor should report that the specified size is invalid and that it will create a 50 X 50 size yard instead.

If the user specifies fewer than 1 or more than 100 trials, report that a default value of 15 trials will be used.

If the user specifies fewer than 1 or more than 5 bones, report that a default value of 3 bones will be used.

HAND IN:

A structure chart (created with a drawing tool such as that provided by WORD, Excel, or PowerPoint) that has a labelled box for each method and that shows the relationship of modules between the client code and the BoneGame code and within the BoneGame class methods. You do not need to include boxes for constructors.

A source code listing of your BoneGame class that o Includes line numbers o Is well laid out with correct indentation and alignment o Uses blank lines in appropriate places o Has a comment box at the top containing the class name and your name o Has a comment box at the beginning of each module holding its name and a brief description of its function.

Outputs from 2 runs . Because each run plays a game for each of 3 objects, this will give you output from 6 games. Each game must now include a well formatted report heading that includes a statement of the yard size, the number of bones buried, the number of trials for that game, and the seed.

Analysis of results . There can be no "test plans" in the traditional sense because the program has no inputs other than the parameters of the game. Once a game starts, it runs to completion on its own. Therefore, in place of test plans, figure out approximately, on the average, how many digs it should take in each game to find a bone for the particular yard size and number of bones. Then compare that to the average your program produced. Is it close? It should be. If it is not, try increasing the number of trials to 30.

Now is it close? If it is still not close, try another seed. If even this doesn’t give you good results, you probably have a bug in your program.

GUI Version of the Cookie Program

(due Tuesday, February 16)

Once your program is working, and safely backed up somewhere, create a GUI interface for the program that displays an N x N grid where N is the array size. For each trial, the squares should initially all be green (i.e. grass) or dark brown (i.e. dirt). Each time a dig is made, the selected grid cell and the 4 surrounding it should change color to show where digs have occurred. When a bone is uncovered, a bone image should display in that location. You will need to add a

“delay” to the program to slow down its progress enough that a user can “see” what is occurring. In class I will cover how to do this before this version is due.

Last updated 1/25/16

Download