Question

In: Computer Science

Prepare a class and tester that simulates and tabulates the result of repeatedly throwing two dice.  Recall...

Prepare a class and tester that simulates and tabulates the result of repeatedly throwing two dice.  Recall a dice, a six-sided cube with 1 through 6 dots on each side.

Using a now familiar looping user experience, the user enters the number of throws. Then, the system simulates the throws and displays the results using a frequency distribution chart. For instance:

Please, use this statement to get the dice action started!

String[] labels = {"Snake Eyes", "Ace Deuce", "Hard Four", "Fever Five", "Easy Six", "Natural Seven", "Easy Eight", "Nina", "Easy Ten", " Yo-leven", "Boxcars"};

Solutions

Expert Solution

Dice.java

import java.util.Random;

public class Dice {
        
        private String[] labels = {"", "Snake Eyes", "Ace Deuce", "Hard Four", 
                        "Fever Five", "Easy Six", "Natural Seven", "Easy Eight", 
                        "Nina", "Easy Ten", "Yo-leven", "Boxcars"};
        
        int[] diceThrows;
        private int numberOfThrows;
        
        // initializing number of throws
        public Dice(int numberOfThrows) {
                this.numberOfThrows = numberOfThrows;
                diceThrows = new int[numberOfThrows];
        }
        
        // simulation started
        public void simulate() {
                
                // creating random instance 
                Random rand = new Random();
                int min = 1, max = labels.length;
                
                // throwing dice 
                for(int i=0 ; i<numberOfThrows ; i++) {
                        int randNum = rand.nextInt(max - min) + min;
                        diceThrows[i] = randNum;                        
                }
                
                distributionChart();
        }
        
        // printing the distribution chart
        public void distributionChart() {
                
                System.out.println();
                System.out.println("-------- Frequency  Distribution ----------");
                System.out.println();
                
                // iterating through all the keys and printing each
                for(int i=1 ; i<labels.length; i++) {
                        int count = 0;
                        String s = "";
                        for(int j=0 ; j<numberOfThrows ; j++) {
                                if(i==diceThrows[j]) {
                                        s += "*";
                                        count++;
                                }
                        }
                        System.out.println(justify(labels[i])+"\t"+s);
                        //System.out.println(justify(labels[i])+"\t"+count);
                }
                System.out.println();
                System.out.println();
        }
        
        // justify the spaces for string
        public String justify(String s){
                int size = 20;
                
                for(int i=s.length() ; i<size ; i++)
                        s += " ";
                
                return s;
        }
}

Tester.java

// Tester.java

import java.util.Scanner;

public class Tester {
        
        public static void main(String[] args) {
                
                Scanner sc= new Scanner(System.in);
                
                String play="y";
                do {
                        
                        // reading number of throws
                        System.out.print("Enter number of throws : ");
                        int numberOfThows = sc.nextInt();
                        
                        // initializing dice with number of dice
                        Dice dice = new Dice(numberOfThows);
                        
                        // starting simulation 
                        dice.simulate();
                        
                        // asking user to continue the game or not
                        System.out.print("Do you want to continue (y/n) : ");
                        play = sc.next();
                        
                }while(play.charAt(0)=='y');
                
                sc.close();
                
        }
        
}

Code   

Dice.java

Tester.java

Output   


Related Solutions

In Python write a function that simulates the throwing of any even numbered dice which will...
In Python write a function that simulates the throwing of any even numbered dice which will be passed on as a parameter: customDice(sides). The function will return a random number rolled for that particular type of dice. Write the program that will utilize the customDice function with the following specification: Ask the user to pick which even-sided dice to use. The program will then roll the dice twice. If the total of both rolls is greater than the number of...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should...
2. (a) Let (a, b, c) denote the result of throwing three dice of colours, amber,...
2. (a) Let (a, b, c) denote the result of throwing three dice of colours, amber, blue and crimson, respectively., e.g., (1, 5, 3) represents throwing amber dice =1, blue dice = 5, crimson dice = 3. What is the probability of throwing these three dice such that the (a, b, c) satisfy the equation b2 − 4ac ≥ 0? [7 marks] (b) From a survey to assess the attitude of students in their study, 80% of them are highly...
The probability of throwing “box cars” (two sixes) with a pair of dice is 1/62. Show...
The probability of throwing “box cars” (two sixes) with a pair of dice is 1/62. Show this using Monte Carlo method and determine the probability of throwing box cars twice in a row.
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Using Eclipse (RollTwoDice) Write a program RollTwoDice that repeatedly rolls two 6-sided dice until they arrive...
Using Eclipse (RollTwoDice) Write a program RollTwoDice that repeatedly rolls two 6-sided dice until they arrive at a given desired sum that you read in from user. 1) the source code (.java file), and 2) the screenshot of running results of each question
Two dice are rolled. Let X be the random variable representing the result of the first...
Two dice are rolled. Let X be the random variable representing the result of the first die, and Y be the random variable representing the largest value rolled on either die. Describe the joint probability density function for X and Y .
Recall the Monty Hall problem as presented in class. There are three caves, two of which...
Recall the Monty Hall problem as presented in class. There are three caves, two of which contain dragons and one of which is a hiding princess. The prince chooses a cave and a wizard (truthfully) reveals to the prince that one of the other caves has a dragon. 2 The prince now has the option to switch to the other cave (the one he didn’t choose and wasn’t revealed to have a dragon), and try his luck finding the princess...
Recall the Monty Hall problem as presented in class. There are three caves, two of which...
Recall the Monty Hall problem as presented in class. There are three caves, two of which contain dragons and one of which is a hiding princess. The prince chooses a cave and a wizard (truthfully) reveals to the prince that one of the other caves has a dragon. 2 The prince now has the option to switch to the other cave (the one he didn’t choose and wasn’t revealed to have a dragon), and try his luck finding the princess...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT