Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that evaluates the random distribution of a dice. The game shoul

ID: 3642022 • Letter: W

Question

Write a program that evaluates the random distribution of a dice. 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 at least three methods: One for input, one for processing and one for output. Allow the client to repeat the game as many times as she wishes. A sample run of the game would look like this:

This game requests a number from the player.
The game then throws the die that number of times.
The game then displays the percentage difference
between the number for each die face value and the average
Please enter the number of times that you would like the computer to throw the dice. 100

The number of throws is 1200 with the average of 200.
The percentage from the average for each diece number is:
Dice number 1 landed 206 times and is 3% above the average.
Dice number 2 landed 204 times and is 2% above the average.
Dice number 3 landed 194 times and is 3% below the average.
Dice number 4 landed 192 times and is 4% below the average.
Dice number 5 landed 193 times and is 3.5% below the average.
Dice number 6 landed 211 times and is 5.5% above the average.

Would you like to play the game again?
Please enter (yes/no)

Explanation / Answer

import java.util.Scanner;

public class DiceFrequencies {
  
    public static Scanner keyboard = new Scanner(System.in);
  
    public static int throwTimes() {
        System.out.println("Please enter the number of times that you would "
                + "like the computer to throw the dice.");
        int times = keyboard.nextInt();
        return times;
    }
  
    public static void processDiceFrequencies(int times, int[] diceCounts,
            double[] diceFrequencies) {
        int average = times / 6;
        for (int i = 0; i < times; i++) {
            int dieValue = (int)(Math.random() * 6 + 1);
            diceCounts[dieValue - 1]++;
        }
        for (int i = 0; i < 6; i++) {
            diceFrequencies[i] = (double)(diceCounts[i] - average) / average;
        }
      
    }
  
    public static void printDiceFrequencies(int times, int[] diceCounts,
            double[] diceFrequencies) {
        System.out.println(" The number of throws is " + times
                + " with the average of " + times / 6 + ".");
        System.out.println("The percentage from the average for each dice"
                + "number is:");
        for (int i = 0; i < 6; i++) {
            System.out.print("Dice number " + (i + 1) + " landed "
                    + diceCounts[i] + " times and is ");
            if (diceFrequencies[i] != 0.0) {
                System.out.printf("%.1f", Math.abs(diceFrequencies[i] * 100));
                System.out.println("% " + (diceFrequencies[i] > 0.0 ?
                        "above" : "below") + " the average.");
            }
            else {
                System.out.println("equal to the average");
            }
        }
    }
  
    public static void main(String[] args) {
        int times;
        int[] diceCounts;
        double[] diceFrequencies;
        String again = "yes";
      
        System.out.println("This game requests a number from the player.");
        System.out.println("The game then throws the die that number of "
                + "times.");
        System.out.println("The game then displays the percentage difference "
                + "between the number for each die face value and the average");
        while (again.equals("yes")) {
            diceCounts = new int[6];
            diceFrequencies = new double[6];
          
            times = throwTimes();
            processDiceFrequencies(times, diceCounts, diceFrequencies);
            printDiceFrequencies(times, diceCounts, diceFrequencies);
          
            System.out.println(" Would you like to play the game again?");
            System.out.print("Please enter (yes/no) ");
            again = keyboard.next();
            System.out.println();
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote