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% (2 reviews)
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...
-
Morrison Co. experienced the following events during 2016: 1. Acquired cash from the issue of common stock. 2. Borrowed cash. 3. Collected cash from providing services. 4. Purchased land with cash....
-
Describe learning theories that relate to motivation.(p. 93)
-
A foundation promises to donate $1 million to a local public broad casting station (a not-for-prot organization) in one year. When, and in what amount, should the station recognize revenue? The...
-
the equal total payents pattern for installment notes cocsists of changong amounts of interest. but constant amounts of principle over lifetime
-
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...
-
Find the weight of a 65.0-kg person on Jupiter (in newtons and pounds).
-
Link two articles from a trade journal in your field and discuss them in a short paper...
-
Evaluate vendors for supplying differentiated or specialized widgets. Review the "Supplier Scorecard Data" document and answer the questions below. 1. Discuss which supplier you will select,...
-
3. By keeping the leading term in the relativistic correction, the kinetic energy operator T of a relativistic electron in one dimension can be written as p 3p4 + 2m 8m3c2 where c is the speed of...
-
CHOOSE CORRECT OPTION How might inadequate training impact Thandiwe's ability to address classroom challenges? a. Develops effective teaching strategies b. Enhances problem solving skills and creates...
-
A 3.5-kg cannon on wheels is loaded with a 0.0527-kg ball. The cannon and ball are initially moving forward with a speed of 1.27 m/s. The cannon is ignited and launches a 0.0527-kg ball forward with...
-
Use mathematical induction to prove each of the following. a. The sum of n terms of a geometric sequence: b. x + y is a factor of x2n - y2n. a air a t ar ar+
-
How do the principles of (a) Physical controls and (b) Documentation controls apply to cash disbursements?
-
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...
-
Mass LLp developed software that helps farmers to plow their fiels in a mannyue sthat precvents erosion and maimizes the effoctiveness of irrigation. Suny dale paid a licesnsing fee of $23000 for a...
-
Average Rate of Return The following data are accumulated by Lone Peak Inc. in evaluating two competing capital investment proposals: 3D Printer Truck Amount of investment $40,000 $50,000 Useful life...
-
4. (10 points) Valuation using Income Approach An appraiser appraises a food court and lounge and provides the following assessment: o O The building consists of 2 floors with the following (6)...
Study smarter with the SolutionInn App