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

Write a fourth game program that evaluates the random distribution of a die. The

ID: 3762843 • Letter: W

Question

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)

I have this so far but its giving me an error:

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();
        }
    }
}

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();
        }
    }
}

OUTPUT----

--------------------Configuration: <Default>--------------------
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.
600

The number of throws is 600 with the average of 100.
The percentage from the average for each dicenumber is:
Dice number 1 landed 91 times and is 9.0% below the average.
Dice number 2 landed 108 times and is 8.0% above the average.
Dice number 3 landed 96 times and is 4.0% below the average.
Dice number 4 landed 111 times and is 11.0% above the average.
Dice number 5 landed 100 times and is equal to the average
Dice number 6 landed 94 times and is 6.0% below the average.

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

Please enter the number of times that you would like the computer to throw the dice.
1000

The number of throws is 1000 with the average of 166.
The percentage from the average for each dicenumber is:
Dice number 1 landed 158 times and is 4.8% below the average.
Dice number 2 landed 170 times and is 2.4% above the average.
Dice number 3 landed 158 times and is 4.8% below the average.
Dice number 4 landed 166 times and is equal to the average
Dice number 5 landed 169 times and is 1.8% above the average.
Dice number 6 landed 179 times and is 7.8% above the average.

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

Please enter the number of times that you would like the computer to throw the dice.
1200

The number of throws is 1200 with the average of 200.
The percentage from the average for each dicenumber is:
Dice number 1 landed 188 times and is 6.0% below the average.
Dice number 2 landed 202 times and is 1.0% above the average.
Dice number 3 landed 202 times and is 1.0% above the average.
Dice number 4 landed 194 times and is 3.0% below the average.
Dice number 5 landed 219 times and is 9.5% above the average.
Dice number 6 landed 195 times and is 2.5% below the average.

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


Process completed.

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