Write an application that runs 1,000,000 games of craps (Fig. 5.8) and answers the following questions: a)
Question:
Write an application that runs 1,000,000 games of craps (Fig. 5.8) and answers the following questions:
a) How many games are won on the first roll, second roll, …, twentieth roll and after the twentieth roll?
b) How many games are lost on the first roll, second roll, …, twentieth roll and after the twentieth roll?
c) What are the chances of winning at craps?
d) What is the average length of a game of craps?
e) Do the chances of winning improve with the length of the game?
Fig. 5.8
Transcribed Image Text:
I 2 3 import java.security.SecureRandom; 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 // Fig. 5.8: Craps.java // Craps class simulates the dice game craps. 69 70 71 72 73 74 public class Craps { // create secure random number generator for use in method rollDice private static final SecureRandom randomNumbers = new SecureRandom(); 75 76 77 78 79 80 81 } // enum type with constants that represent the game status private enum Status {CONTINUE, WON, LOST}; // constants that represent common rolls of the dice private static final int SNAKE_EYES = 2; private static final int TREY = 3; final int SEVEN = 7; private static private static final int YO_LEVEN = 11; private static final int BOX_CARS = 12; //plays one game of craps public static void main (String [] args) { int myPoint = 0; // point if no win or loss on first roll Status gameStatus; // can contain CONTINUE, WON or LOST tus://pot int sumOfDice = rollDice (); // first roll of the dice // determine game status and point based on first roll switch (sumoOfDice) { case SEVEN: // win with 7 on first roll case YO LEVEN: // win with 11 on first roll gameStatus = Status. WON ; break; } } } case SNAKE_EYES: // lose with 2 on first roll case TREY: // lose with 3 on first roll. case BOX_CARS: // lose with 12 on first roll gameStatus Status . LOST; break; default: // did } // while game is not complete while (gameStatus == Status. CONTINUE) { // not WON or LOST sumOfDice = rollDice (); // roll dice again } gameStatus myPoint sumOfDice; // remember the point System.out.printf("Point is %d %n", myPoint); break; } // determine game status if (sumofDice = myPoint) { // win by making point gameStatus = Status. WON ; } else { } else { if (sumOfDice == SEVEN) { // lose by rolling 7 before point gameStatus Status. LOST; } // display won or lost message if (gameStatus == Status. WON) { System.out.println("Player wins"); not win or lose, so remember point Status. CONTINUE; // game is not over System.out.println("Player loses"); // roll dice, calculate sum and display results public static int rollDice () { //pick random die values int diel = 1 + randomNumbers.nextInt (6); // first die roll int die2= 1 + randomNumbers.nextInt (6); // second die roll int sum diel + die2; // sum of die values // display results of this roll System.out.printf("Player rolled %d + %d = %d %n", diel, die2, sum); return sum; Player rolled 5+ 6 = 11 Player wins Player rolled 5 + 4 = 9 Point is 9 Player rolled 4+ 2 = 6 Player rolled 3 + 6 = 9 Player wins Player rolled 1 + 2 = 3 Player loses Player rolled 2 + 6 = 8 Point is 8 Player rolled 5 + 1 = 6 Player rolled 2 + 1 = 3 Player rolled 1 + 6 = 7 Player loses
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (QA)
Based on the provided image which appears to be a fragment of Java code for simulating the game of c...View the full answer
Answered By
Branice Buyengo Ajevi
I have been teaching for the last 5 years which has strengthened my interaction with students of different level.
4.30+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
Which communication method is good for distributing information to large groups?
-
Solomon Company's cost and production data for two recent months included the following: March April Production (units) 400 600 $2,400 $ $2,400 $1,200 Rent Utilities 800 Required a. Separately...
-
(EPS: Simple Capital Structure) On January 1, 2010, Bailey Industries had stock outstanding as follows. 6% Cumulative preferred stock, $100 par value, issued and outstanding 10,000 shares $1,000,000...
-
What kind of bonding mechanism would you expect for (a) The HCl molecule (b) The O2 molecule, (c) Cu atoms in a solid?
-
What diameter orifice hole, \(d\), is needed if under ideal conditions the flowrate through the orifice meter of Fig. P3.129 is to be \(30 \mathrm{gal} / \mathrm{min}\) of seawater with...
-
In the spot market 7.8 pesos can be exchanged for 1 U.S. dollar. A compact disk costs $15 in the United States. If purchasing power parity holds, what should be the price of the same disk in Mexico?
-
The preceding section reveals that management accounting has evolved parallel with the development of technology and business environment. It is apparent that management accounting information has...
-
A small airline has just purchased a computer for its new automated reservations system. Youve been asked to develop the new system. Youre to write an application to assign seats on each flight of...
-
Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values...
-
Northwestern Ltd. (NW) acquires the only class of stock of Southeastern Ltd. (SE) from the latter's shareholders. Which of the following independent transactions qualify as a Type B reorganization?...
-
Below is a plot of the natural log of the fugacity coeffi cient, ln (i) , of pure NH3 as a function of pressure at a temperature of 100C. From this plot, as best you can, determine the molar volume...
-
Analyze your favorite brand at the hand of the reputational factors mentioned above. What do you most admire about it and are there areas that can be improved?
-
Several professional roles were identified in the chapter. Identify five and reflect on why you would like or not like to perform that role in strategic communication practice.
-
Identify an organization or institution you are familiar with and identify the kind or kinds of structure it uses. Describe the structural attributes you have identified.
-
What are the components of a print ad?
-
Consider the following zero-sum game. a. Does either player have a dominant strategy? Does either have a dominated strategy? Explain. b. Find the players equilibriumstrategies. Player C CI 13 14 C2...
-
In a nonmagnetic medium, E = 50 cos (10 9 t 8x) a y + 40 sin (10 9 t 8x) a z V/m find the dielectric constant r and the corresponding H.
-
Implement a method with signature transfer(S, T) that transfers all elements from stack S onto stack T, so that the element that starts at the top of S is the first to be inserted onto T, and the...
-
Suppose that instead of having the node-search function f (d) = 1 in an orderd B-tree T, we have f (d) = logd. What does the asymptotic running time of performing a search in T now become?
-
Consider the page caching strategy based on the least frequently used (LFU) rule, where the page in the cache that has been accessed the least often is the one that is evicted when a new page is...
-
Select a news article dated within the previous two months and analyze the issue using the economic concepts and theory learned in this class. Include at least one graph developed in our course....
-
Determine if the matrix is in echelon form, and if it is also in reduced echelon form. 1306-2 0019 3 0000 0
-
Write (27x6yz)(1/3) as a simplified radical expression.
Study smarter with the SolutionInn App