JAVA PROGRAMMING. Using below code, incorporate new code to answer: Answer c) an
ID: 3821396 • Letter: J
Question
JAVA PROGRAMMING.
Using below code, incorporate new code to answer:
Answer c) and e) with your conclusions expressed as literal text output from your program to the IDE console.
Example output:
Turn # Number of wins on that turn
1 222768
2 77103
3 54910
4 39173
5 28275
6 20174
7 14454
8 10285
9 7364
10 5257
11 3862
12 2715
13 1951
14 1437
15 1037
16 765
17 550
18 356
19 276
20+ 801
Turn # Number of losses on that turn
1 111052
2 111027
3 79389
4 57512
5 41107
6 29404
7 21442
8 15393
9 11179
10 8042
11 5600
12 4258
13 3145
14 2175
15 1579
16 1142
17 794
18 568
19 467
20+ 1212
Total Number of wins: 493513 Total number of losses: 506487
Probability of winning: 0.493513
c) The probability of winning is about (your estimate%), which is very fair. You are just as likely to win as lose.
d) Average number of turns per game: 3.378512
e) You answer as literal output.
// Fig. 6.8: e_18.java
// Craps class simulates the dice game craps.
import java.security.SecureRandom;
public class e_18
{
// create secure random number generator for use in method rollDice
private static final SecureRandom randomNumbers = new SecureRandom();
// 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;
private static final int SEVEN = 7;
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
int sumOfDice = rollDice(); // first roll of the dice
// determine game status and point based on first roll
switch (sumOfDice)
{
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 not win or lose, so remember point
gameStatus = Status.CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
System.out.printf("Point is %d%n", myPoint);
break;
}
// while game is not complete
while (gameStatus == Status.CONTINUE) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
// determine game status
if (sumOfDice == myPoint) // win by making point
gameStatus = Status.WON;
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");
else
System.out.println("Player loses");
}
// roll dice, calculate sum and display results
public static int rollDice()
{
// pick random die values
int die1 = 1 + randomNumbers.nextInt(6); // first die roll
int die2 = 1 + randomNumbers.nextInt(6); // second die roll
int sum = die1 + die2; // sum of die values
// display results of this roll
System.out.printf("Player rolled %d + %d = %d%n",
die1, die2, sum);
return sum;
}
}
Explanation / Answer
while (gameStatus == Status.CONTINUE) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
// determine game status
if (sumOfDice == myPoint) // win by making point
gameStatus = Status.WON;
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");
else
System.out.println("Player loses");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.