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

0. Objective

Completing all tasks in this assignment will help you learn about: 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

Remember that instance variables should be private.

A Player has the following methods:

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 fi be the frequency of outcome i. We will use the notation favg[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 favg[1..4].

In an ASCII text file analysis.txt, answer the following questions.

  1. Run your simulator using a 6-sided dice for 10 rolls (each Player). What is the ratio f1:favg[2..5] for Player 1? For Player 2?
  2. Run your simulator using a 6-sided dice for 1000 rolls (each Player). What is the ratio f1:favg[2..5] for Player 1? For Player 2?
  3. Run your simulator using a 6-sided dice for 10000 rolls (each Player). What is the ratio f1:favg[2..5] for Player 1? For Player 2?
  4. Do you think your trickRoll method is correctly implemented according to our specification? Why?
  5. 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).