Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java: 1. The game of Craps is a dice game that involves rolli

ID: 3671971 • Letter: W

Question

Write a program in Java:

1. The game of Craps is a dice game that involves rolling a pair of dice. At the start of each turn if the players rolls a 7, or 11 they win.

2. Write a program that simulates how often a player would win if they rolled the dice 100 times.

3. The program will output a message such as "The player rolled 14 sevens and 5 elevens for a total of 19 wins out of 100."

4. The program will ask the user if they wish to run another simulation or quit.

5. Possible Program structure

     1. Create a random number for each dice using either the API Random class or the Math classes' random method.

     2. Create a loop that will loop 100 times.

    Within the loop:

         a. Store the random number output

         b. if it is a seven or eleven increment the value of the appropriate variable.

    3. Print out the message stating the number of sevens and elevens.

    4. Use the appropriate to type of loop to prompt the user if they like to try again or quit.

    5. Remember to provide javadoc comments and appropriate in code comments.

Explanation / Answer

// Craps.java

import java.util.Scanner;
import java.util.Random;

public class Craps {
//Instatiate the scanner
private static Scanner scannerObject = new Scanner(System.in);
  
public static void craps() {
//Instantiate the random number generator
Random randomGenerator = new Random();
  
//Set up die 1
int die1 = 2;
  
//Set up die 2
int die2 = 2;
  
//A variable to hold the dice total
int diceTotal = 0;
  
//A variable to track the number of sevens rolled
int numberOfSevens = 0;
  
//A variable to track the number of elevens rolled
int numberOfElevens = 0;
  
//Loop 100 times (100 rolls)
for(int count = 0; count < 100; count++) {
//Roll die number 1 (get a random number between 1 and 6)
die1 = randomGenerator.nextInt(6) + 1;
//Roll die number 2 (get a random number between 1 and 6)
die2 = randomGenerator.nextInt(6) + 1;
  
//Add up the two dice
diceTotal = die1 + die2;

if (diceTotal == 7) {
//Increment the number of sevens
numberOfSevens = numberOfSevens + 1;
} else if (diceTotal == 11) {
//Increment the number of elevens
numberOfElevens = numberOfElevens + 1;
}
  
}

//Get the total number of winds
int total = numberOfSevens + numberOfElevens;

//Output the results
System.out.printf("The player rolled " + numberOfSevens + " sevens and "
+ numberOfElevens + " elevens for a total of " + total
+ " wins out of 100 ");

//Ask the user if they want to play again
System.out.print("Do you want to play again? Y or N: ");
String play = scannerObject.next();
  
//Check if we want to play again
if (play.equals("Y") || play.equals("y")) {
craps();
} else {
System.out.println("Game Over ");
scannerObject.close();
}
}
  
public static void main(String[] args) {
//Initial game play
craps();
}

}

/*
output:

The player rolled 17 sevens and 2 elevens for a total of 19 wins out of 100
Do you want to play again? Y or N: Y
The player rolled 15 sevens and 4 elevens for a total of 19 wins out of 100
Do you want to play again? Y or N: Y
The player rolled 15 sevens and 2 elevens for a total of 17 wins out of 100
Do you want to play again? Y or N: N
Game Over


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote