### CS100J Spring 2004 Project 4 Due Thursday 4/1 at 3pm

#### 0. Objective

• One-dimensional arrays
• Simulation
• Generating random numbers
First skim, and then carefully read the entire assignment before starting any tasks! You must use the identifiers (variable names, method names, etc.) exactly as we specify, including capitalization. Use good programming style and observe the style guidelines that are given in the previous grading guides..

#### 1. A risky experiment

We're going to conduct a simulation experiment involving some dangerous material: a dice! We will roll a dice multiple times, record the outcomes, and calculate some statistics of our experiment. Later, we will learn an important new skill--how to do a "trick roll" such that we can get a specific outcome with higher probability! Our simulation will involve a `Dice`, some Players, and of course the `Simulator`.

#### 2. Class `Dice`

An incomplete class `Dice` is provided. Read it to make sure that you understand its functionality. You need to implement method `roll()` according to specification.

#### 3. Class Player

A Player rolls a `Dice` multiple times, record the outcomes, and calculate some statistics. You will implement the Player class.

A Player has

• `String name`
• `Dice d`: references a dice
• `int[] outcome`: references a 1-d array of the outcomes
• `int[] count`: references a 1-d array of the frequencies of the outcomes
Remember that instance variables should be `private`.

A `Player` has the following methods:

• A `Player` has a name and a dice. The constructor initializes the field `name` and instantiates a `Dice` object.
```public Player(String name, int numFaces) ```
• ```Method play rolls the dice numRolls times and records the outcomes. (Note that this method needs to instantiate the arrays outcome and count.) public void play(int numRolls) ```
• ```Calculate the average frequency for all outcomes. E.g., if the counts for outcomes 1..4 (a 4-sided dice) are 2,4,2,4, then the average frequency is 3. public double meanCount() ```
• ```Find the face that shows face-up with the highest frequency. If multiple faces has the highest frequency, then return any one of them. public int faceMaxCount() ```
• ```Report the statistics of the game: display the frequencies for all outcomes, the average frequency, and the face with the highest frequency. public void report() ```
``` Remember to test each method after you implement it! 4. Class Simulator In the main method, prompt the user for the number of sides of the Dice and the number of times to roll. Instantiate one Player (any name). Then the Player plays the dice game and reports the statistics. 5. Let's learn a new trick! We go back to class Dice to implement a trickRoll method. In a "trick roll," the probability of getting face 1 is x times as high as getting face 2, x times as high as getting face 3, x times as high as getting face 4, etc. The probabilities of getting face 2, face 3, etc, (other than face 1) are equal. public void trickRoll(int x) Now class Player needs a trickPlay method. trickPlay has the same functionality as play but uses the trickRoll method instead of roll. trickPlay has an extra parameter specifying the "weight" of the trick roll (corresponding to parameter x in method trickRoll). public void trickPlay(int numRolls, int weight) Finally, add more code in the main method of class Simulator. Instantiate another Player. Both Players have Dice with the same number of faces and they roll the same number of times. Player 1 uses the normal roll and Player 2 does trick rolls! Player 2's trick roll gives face 1 three times as often as face 2 (face 3, face 4, ...). Report each Player's game statistics. Something to look forward to: Have you noticed that we're adding a lot of code to accommodate the trick roll? First you add the method trickRoll, then you have to add trickPlay, and then you have the Player call the method trickPlay. In the process, we have duplicated a bit of code. Imagine how much work it would be if we have more tricks up our sleeves?! There is a more elegant way to deal with this situation... Think inheritance! We'll discuss this another time. 6. Analysis Let $f$i be the frequency of outcome $i$. We will use the notation $f$avg[i..j] to represent the average frequency of outcomes $i..j$. For example, the average frequency of all outcomes of a 4-sided dice is $f$avg[1..4]. In an ASCII text file analysis.txt, answer the following questions. Run your simulator using a 6-sided dice for 10 rolls (each Player). What is the ratio $f$1:$f$avg[2..5] for Player 1? For Player 2? Run your simulator using a 6-sided dice for 1000 rolls (each Player). What is the ratio $f$1:$f$avg[2..5] for Player 1? For Player 2? Run your simulator using a 6-sided dice for 10000 rolls (each Player). What is the ratio $f$1:$f$avg[2..5] for Player 1? For Player 2? Do you think your trickRoll method is correctly implemented according to our specification? Why? Do you think the effect of "trick rolls" is more noticeable for dice with more sides or with fewer sides? Why? (Note: Run some experiments for analysis!) 7. Submitting Your Work Submit your completed files Dice.java, Player.java, Simulator.java, and analysis.txt on-line using CMS (Course Management System) before the project deadline. Make sure you are submitting the correct, up to date, .java files (not .class or .java~). We will not accept any files after the deadline for any reason (except for documented medical reasons). See the CMS link on the web page for instructions on using CMS. If necessary, turn off the DrJava feature that saves the .java~ files (see course webpage announcement on 2/17 for instructions). ```