Design and implement a Java program for programming exercise 6.30, page 241, fro
ID: 3598568 • Letter: D
Question
Design and implement a Java program for programming exercise 6.30, page 241, from the textbook (name it CrapsGame). Rolling a die is simply generating a random integer number between 1 and 6. Write a separate method for this function, call it rollDie(), to return the generated number (one number between 1 and 6). No parameters are passed to the method rollDie(). This program takes no input from the user and all output is handled by the main method. Do NOT print anything inside the method rollDie(). Follow the game rules stated in the problem statement. Display the results of every roll of the dice. Document your code and follow the model output shown in the sample runs in the textbook.
*6.30 Game: craps) Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1,2,.., and 6, respec- tively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player. Here are some sample runs. You rolled 5 + 6 11 You wirn You rolled 1+2=3 You lose You rolled 4 +4=8 point is 8 You rolled 6+2=8 You wirn You rolled 3 +2=5 point is 5 You rolled 2+5=7 You loseExplanation / Answer
Please find my implementation.
import java.util.Random;
public class CrapsGame {
// random object
static Random random = new Random();
public static int rollDie() {
return random.nextInt(6)+1; //1-6 random number
}
public static void main(String[] args) {
int r1 = rollDie();
int r2 = rollDie();
int sum = r1 + r2;
System.out.println("You rolled "+r1+"+"+r2+" = "+sum);
if(sum == 2 || sum ==3 || sum == 7)
System.out.println("You lose");
else if(sum == 7 || sum == 11) {
System.out.println("You win");
}else{
int point = sum;
System.out.println("point is "+sum);
while(true) {
r1 = rollDie();
r2 = rollDie();
int t = r1 + r2;
System.out.println("You rolled "+r1+"+"+r2+" = "+t);
if(t == point) {
System.out.println("You win");
break;
}else if(t == 7) {
System.out.println("You lose");
break;
}
}
}
}
}
/*
Sample run:
You rolled 5+1 = 6
point is 6
You rolled 3+2 = 5
You rolled 4+5 = 9
You rolled 3+3 = 6
You win
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.