Question

(Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1...

(Java Coding)

Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times)
Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1,000,000 times)

Given below that the class die, oddstester, and gameSimulator(partially complete and the bold missing parts need to be done), need to find the probability of winning game 1 and 2 (after the 1 million plays)

hence the getWins and Losses should sum to 1million
--------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Random;

/** This class models a die that, when cast,
lands on a random face.
*/
public class Die
{
private Random generator;
private int sides;

/** @param s the number of sides, e.g., 6 for a normal die.
Constructs a die with a given number of sides.
*/
public Die(int s)
{
sides = s;
generator = new Random();
}

/** @return the face of the die.
Simulates a throw of the die.
*/
public int cast()
{
return 1 + generator.nextInt(sides);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------

public class GameSimulator
{
private int tries;
private Die d1, d2;
private int wins;
private int losses;

public GameSimulator(int numSides, int numTries)
{
d1 = new Die(numSides);
d2 = new Die(numSides);
  
tries = numTries;
}


public void runSingleDieSimulation()
{
//missing
}


public void runDoubleDieSimulation()
{
//missing  

}


public double getWinPercent()
{
return (double)(wins) / (wins + losses);
}


public int getWins()
{
return wins;
}


public int getLosses()
{
return losses;
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------

public class DiceOddsTester
{
public static void main(String[] args)
{

GameSimulator simulator = new GameSimulator(6, 1000000);

simulator.runSingleDieSimulation();
System.out.println("Game #1 wins: " + simulator.getWinPercent());
System.out.println("Expected: .51");

simulator.runDoubleDieSimulation();
System.out.println("Game #2 wins: " + simulator.getWinPercent());
System.out.println("Expected: .49");
  
simulator.runSingleDieSimulation();
System.out.println(simulator.getWins());
System.out.println(simulator.getLosses());
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package abc1;

public class GameSimulator {
   private int tries;
   private Die d1, d2;
   private int wins;
   private int losses;

   public GameSimulator(int numSides, int numTries){
   d1 = new Die(numSides);
   d2 = new Die(numSides);  
   tries = numTries;
   this.wins = 0;
   this.losses = 0;
   }
   public void runSingleDieSimulation(){
       this.wins = 0;
       this.losses = 0;
       for(int i=0; i<this.tries; i++) {           //Number of tries
           boolean win = false;
           for(int j=0; j<4; j++) {               //Single trial
               int x = d1.cast();
               if(x==6) {
                   win = true;
                   break;
               }
           }
           if(win==true)
               this.wins++;
           else
               this.losses++;
       }
   }

   public void runDoubleDieSimulation()
   {
       this.wins = 0;
       this.losses = 0;
       for(int i=0; i<this.tries; i++) {           //Number of tries
           boolean win = false;
           for(int j=0; j<24; j++) {               //Single trial
               int x = d1.cast();
               int y = d2.cast();
               if(x==6 && y==6)                   //If both die gets 6
                   win = true;
           }
           if(win==true)
               this.wins++;
           else
               this.losses++;
       }
   }

   public double getWinPercent(){
   return (double)(wins) / (wins + losses);
   }

   public int getWins(){
   return wins;
   }

   public int getLosses(){
   return losses;
   }
}

Other classes are same as given in the question.

Output:

Add a comment
Know the answer?
Add Answer to:
(Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT