Project 5

Due Thursday 4/15 at 3pm

- Array of objects
- Two-dimensional (2-d) array
- Simulation

Read carefully and *think about implementing the classes carefully*--developing a concise implementation is part of the work in this project. Whenever possible, reduce redundancy in your code by calling other (existing) methods. Program development is an iterative process--sometimes you will write code under a method and later decide that you should take part of that code out and set it up as a separate method. This iterative process is normal--you should not expect your first implementation to be perfect. Look for improvement as (after) you write code.

Let's first look at some example output so that you know what to expect. Notice that the histogram you will draw has horizontal "bars" (rows of asterisks).

Experiment One: Enter number of Dice you want to create: 2 Creating the 1th Die Enter the number of faces of for the Die: 6 Which side is weighted more, 1-6? Enter 0 if it's a fair die: 0 Creating the 2th Die Enter the number of faces of for the Die: 6 Which side is weighted more, 1-6? Enter 0 if it's a fair die: 0 Start simulation... Enter the number of trials: 100000 Calculating sum of two dice... Enter the first die: 1 Enter the second die: 2 The count distribution for the sum of Dice 1 and 2: 1 2 ** 3 *** 4 ***** 5 ******* 6 ******** 7 ********** 8 ******** 9 ******* 10 ***** 11 *** 12 **Another example run is shown at the end of the project statement.

`Dice`

`Dice`

has
`int top`

: Top face`int faces`

: Number of faces on the`Dice`

`int[] weight`

:`weight[i]`

is the likelihood of face`i`

being the top face. We say side`i`

has weight`weight[i]`

. A fair`Dice`

has`weight[i]=1`

for all`i`

. A trick dice (say 6-sided) where face 2 is three times more likely to show face-up than face 1, face 3, face 4, ..., etc., has the values {0,1,3,1,1,1,1} in array`weight`

. Note that we have chosen to keep an extra cell (at index 0). You may decide for yourself whether to use this extra cell. As always, write clear and*concise*comments to document your code. A`Dice`

has at most one "weighted side" (face 2 in the previous example).

`Dice`

has the following instance methods:
- Overloaded constructors:

/** A Dice has numFaces sides and the topFace is random */

public Dice(int numFaces)

/** An unfair Dice has numFaces sides. Face weightedSide has weight w. The

topFace is random */

public Dice(int numFaces, int weightedSide, int w) - A method
`roll`

to roll the`Dice`

based on its array`weight`

:

/** top gets a value in 1..faces */

public void roll() - Getter methods
`topFace()`

and`numberOfFaces()`

`DiceSet`

`DiceSet`

is a set of `Dice`

, of course! A `DiceSet`

has
`Dice[] set`

: references an array of`Dice`

`int[][] outcome`

: references a 2-d array of outcomes`outcome[d][j]`

is the top face value of the`d`

th`Dice`

after the`j`

th roll.`int[][] count`

: references a 2-d array of the counts of the outcomes`count[d][j]`

: is the number of times that face`j`

has shown up for`Dice`

`d`

. Note that`count`

may be a*ragged*array.- a
*class constant*`double MAXSTARS`

, which has the value 10.0. This is the maximum number of asterisks (stars) on any row in the histogram.

A `DiceSet`

has the following methods:

- static method
`createDiceSet`

creates an instance of`DiceSet`

by prompting the user for the necessary values to create a`DiceSet`

(see example runs). This method serves the purpose of the constructor:

`public static DiceSet createDiceSet()`

Here are some hints for the method:- This method has to call the default constructor for
`DiceSet`

- This method has to instantiate the
`set`

array - Remember that a 2-d array is just a 1-d array of 1-d arrays, so you can instantiate the individual 1-d arrays separately. You will create only the first dimension of
`outcome`

and`count`

in this method. - There're multiple
`Dice`

in the`DiceSet`

, so the creation of the a`Dice`

, i.e., the call of the`Dice`

constructor, should appear inside a loop. - Prompt the user for information as necessary. For example, ask the user for the weight of the weighted side only if the user says the
`Dice`

is weighted.

- This method has to call the default constructor for
- instance method
`runSimulation`

takes as parameter the number of rolls and fill the`outcome`

and`count`

arrays. Before you fill the arrays, of course you need to finish creating them. Recall that in the method`createDiceSet`

you have created only the first dimension. In this method you must instantiate the arrays for the second dimension.

`public void runSimulation(int trials)`

Note that in a simulation each`Dice`

is rolled`trials`

number of times. Note the terminology: a*trial*means rolling*each*`Dice`

in the`DiceSet`

once. - static method
`calculateCount`

computes the counts for an array of outcomes, i.e., it computes the data that can be used later for drawing a histogram. The computed counts are stored in a 1-d array and returned.

`public static int[] calculateCount(int[] outcomes)`

For example, if`outcomes`

references an array with the values {2,4,3,1,2,3,2,4,4,1}, then the returned array storing the counts should be {0,2,3,2,3} since value 1 occurs 2 times, value 2 occurs 3 times, value 3 occurs 2 times, and value 4 occurs 3 times. This method needs to determine the length of the array to return! Again, your array may not have the first cell with value zero. It is up to you to design your code. - static method
`plotHistogram`

draws a histogram with*horizontal*bars (rows of asterisks) given a 1-d array`data`

. The longest bar is always`MAXSTARS`

long--we call such a histogram a*normalized*histogram. In normalizing the bars, always*round*to the nearest number.

`public static void plotHistogram(int[] data)`

For example, if`data`

references an array with the values {0,2,3,2,3}, then the following histogram should be drawn:1 ******* 2 ********** 3 ******* 4 **********

Again, your`data`

array may not have the first cell with value zero. It is up to you to design your code. - instance method
`printCount`

display the count distribution (histogram) for each`Dice`

in the`DiceSet`

. Display a title above each histogram. See example output at the end of the project statement.

`public void printCount()`

- instance method
`getPairOutcomeSum`

calculates the sum of the faces shown by two specified`Dice`

for each trial. The resulting sums are stored in an array and returned.

/** = array of sums of outcomes for Dice d1 and d2 */

public int[] getPairOutcomeSum(int d1, int d2)

For example, this method (among others) needs to be called before you can produce the histogram shown in the example output above. - instance method
`countPairSameFace`

that counts the number of times that two specified`Dice`

show the same face in a trial.

/** = number of times Dice d1 and d2 both show face f */

public int countPairSameFace(int f, int d1, int d2) `instance method`

`probabilityAnyPairSameFace`

*estimates*the probability of*any*two`Dice`

in the`DiceSet`

showing the same face in a trial.

/** = probability of any pair of Dice showing face f in a trial */

public double probabilityAnyPairSameFace(int f)

You must*use simulation to*. The probability of any pair of**estimate**the probability`Dice`

showing face f is estimated by dividing the*number of times that*with the*at least*two`Dice`

show face f*number of trials*.

```
```#### 4. Class `P5`

for simulation

In the `main`

method, you will run several *experiments*.
**Experiment 1:** create a set of two six-sided fair dice. Simulate 10 trials and show the count distributions for the set.
**Experiment 2:** create a set of two six-sided dice: one fair, the other has side 3 weighted 5 times more than the other sides. Simulate 100000 trials and show the count distributions for the set. (This experiment gives the sample output shown at the end of the project statement).
**Experiment 3:** create a set of three six-sided dice: first two are fair, and the third has side 3 weighted 5 times more than the other sides. Simulate 100000 trials and show the count distributions for the set. Next show the histogram of the *pair outcome* for the *first two* dice. (This part gives output similar to that shown at the beginning of the project statement). Finally show the histogram of the *pair outcome* for the *last two* dice. I hope you see some interesting results! You can choose the value of constant `MAXSTARS`

in class `DiceSet`

to dramatize the results!
**Experiment 4:** create a set of four dice: first two are fair and have 6 sides each; the last two are 12-sided and unfair with face 4 weighted 5 times more than the other sides. Simulate 100000 trials and show the count distributions for the set. Next show the histogram of the *pair outcome* for the *first two* dice. Now show the histogram of the *pair outcome* for two dice where one is fair and the other is unfair. Finally, estimate (using existing methods) and display the probabilities of any two dice of the set having the same face:
- two dice showing face 1
- two dice showing face 4
- two dice showing face 10

**Experiment 5:** You will simulate 100000 trials using a set of dice with with properties of your choosing! Use some combination of properties that will result in a histogram of *pair outcome* that looks "different" from the histograms shown before. How different? Perhaps the "peak" shows up at a different location relative to the "tails", or perhaps one tail is thicker than another, or ... Have fun!

#### 6. Another example run

Experiment Two:
Enter number of Dice you want to create: 2
Creating the 1th Die
Enter the number of faces of for the Die: 6
Which side is weighted more, 1-6? Enter 0 if it's a fair die: 0
Creating the 2th Die
Enter the number of faces of for the Die: 6
Which side is weighted more, 1-6? Enter 0 if it's a fair die: 3
Enter the weight for side 3: 5
Start simulation...
Enter the number of runs: 100000
The count distribution of Dice 1:
1 **********
2 **********
3 **********
4 **********
5 **********
6 **********
The count distribution of Dice 2:
1 **
2 **
3 **********
4 **
5 **
6 **

#### 7. Submitting Your Work

Submit your files `Dice.java`

, `DiceSet.java`

, and `P5.java`

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).