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

Design and implement a Java program for programming exercise 6.30, page 241, fro

ID: 3674399 • 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.

(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, respectively. 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 win You rolled 1 + 2 = 3 You lose You rolled 4 + 4 = 8 point is 8 You rolled 6 + 2 = 8 You win You rolled 3 + 2 = 5 point is 5 You rolled 2 + 5 = 7 You lose

Please NO arrays. Thank you!!!

Explanation / Answer

import java.util.Random;

public class Craps

{

        // create random number generator for use in method rollDice

         private static final Random randomNumbers = new Random();

       // enumeration 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;

public static void main( String[] args )

{

         int myPoint = 0;

      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 ", myPoint );

      break; // optional at end of switch

} // end switch

// 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" );

}//end main

// roll dice, calculate sum and display results

public static int rollDice()

{

    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

System.out.printf( "Player rolled %d + %d = %d ", die1, die2, sum );

return sum; // return sum of dice

    }// end method rollDice

} // end class Craps

output

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

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