Assignement #10 Part 1: PatternOfSix class Write a third game program that promp
ID: 3761103 • Letter: A
Question
Assignement #10
Part 1: PatternOfSix class
Write a third game program that prompts for a pattern of 6 dice and then counts the number of throw necessary to get that pattern to appear.
Use methods to simplify the code.
Guarantee inputs are in an acceptable range.
Proper comments and formating are expected, include pre and post conditions.
Allow the client to repeat the game as many times as she wishes.
A sample output is shown below:
This game requests six dice numbers from the player.The game then throws the dice until that pattern appears. The game then tells the player how many throws it took
Please enter the six number pattern you would like the computer to throw separated by spaces
1 2 3 4 5 6
it took 7,314 throws to get the pattern 1 2 3 4 5 6
Would you like to play the game again? Please enter (yes/no)
Part 2: RandomDistribution class
Write a fourth game program that evaluates the random distribution of a die. The game should first prompt the client for a number of die throws that he would like. The game then throws the die that many times. The game records the number of times each die number appears on top and calculates the average for the six values. The program then computes the percent difference between the average and the number for each die face value.
Use methods to simplify the code.
Guarantee the number entered is not less than zero .
Proper comments and formating are expected, include pre and post conditions.
Allow the client to repeat the game as many times as she wishes.
A sample output is shown below:
This game requests a number from the player.The game then throws the dice that number of times.The game then displays the percentage difference between the number for each dice face value and the average.
Please enter the number of times that you would like the computer to throw the dice.
600
The number of throws is 600 with the average of 100.
The percentage from the average for each dice number is:
Dice number 1 landed 90 times and is 10% below the average.
Dice number 2 landed 106 times and is 6% above the average.
Dice number 3 landed 104 times and is 4% above the average.
Dice number 4 landed 95 times and is 5% below the average.
Dice number 5 landed 112 times and is 12% above the average.
Dice number 6 landed 93 times and is 7% below the average.
Would you like to play the game again? Please enter (yes/no)
Explanation / Answer
Part 1: Pattern of Six Class
public class PatternOfSix
{
public static final int NTRIALS = 6000;
private final int NFACES = 12;
int counter[] = {0,0,0,0,0,0,0,0,0,0,0,0};
public void testDie (int nTrials)
{
int die;
for (int k = 0; k < nTrials; k++)
{
die = 1 + (int)(Math.random() * NFACES);
++counter[die - 1];
}
} // testDie()
public void printResults(int nTrials)
{
System.out.println("Out of " + nTrials + " die rolls, there were: ");
for (int k = 1; k <= NFACES; k++)
System.out.println(" " + counter [k - 1] + " " + k + "s");
} // printResults()
public static void main(String args[])
{
PatternOfSix tester = new PatternOfSix();
tester.testDie(NTRIALS);
tester.printResults(NTRIALS);
} // main()
} // PatternOfSix
Part 2: RandomDistribution Class
public class RandomDistribution{
public static void main(String[] args)
{
double r = Math.random();
int roll;
if (r < 1.0/8.0) roll = 1;
else if (r < 2.0/8.0) roll = 2;
else if (r < 3.0/8.0) roll = 3;
else if (r < 4.0/8.0) roll = 4;
else if (r < 5.0/8.0) roll = 5;
else
roll = 6;
System.out.println(roll);
}
}
OR
public class RandomDistribution {
public static void main(String[] args) {
int SIDES = 6; // how many sides on the die?
// roll should be 1 through SIDES
int roll = (int) (Math.random() * SIDES) + 1;
System.out.println(roll);
}
}