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

Obiectives: Java Methods and Arrays (chapters 6 and 7) Write an application that

ID: 3708221 • Letter: O

Question

Obiectives: Java Methods and Arrays (chapters 6 and 7) Write an application that runs 1,000,000 games of craps and answers the following questions: 1) How many games are won on the first roll, second roll, third roll,..., twentieth roll, and after the 2) How many games are lost on the first roll, second roll, third roll, . twentieth roll, and after the 3) 4) What is the average length of a game of craps? twentieth roll (all games that take more than 20 rolls are considered one group)? twentieth roll (all games that take more than 20 rolls are considered one group)? What are the chances of winning at craps? [Note: You should discover that craps is one of the fairest casino ga mes. What do you suppose this means?] Below is a reproduced copy of the Craps.java from chapter 6: // Craps.java // Craps class simulates the dice game craps. import java.uti1.Random public class Craps // create random number generator for use in method rollDice private static final Random randomNumbers new RandomO // 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 SEVEN7; 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 sumofoi ce rollDice(); // first roll of the dice // determine game status and nint hased on first roll 1/2

Explanation / Answer

CODING: COPY HERE

import java.util.Random;


public class GameOfCraps {
   private Random randomNumbers=new Random();
   private enum Status{Continue, Won, Lost};
   int[] GamesWon;
   int[] GamesLost;
   int winTotal;
   int loseTotal;
  
   public void play(){
       int totalOfDice=0;
       int myPoints=0;
       Status gameStatus;
       int roll;
       GamesWon=new int[22];
       GamesLost=new int[22];
       for(int x=1; x<=1000; x++){
           totalOfDice=rollDice();
           roll=1;
           switch(totalOfDice){
           case 7:
           case 11:
               gameStatus=Status.Won;
               break;
           case 2:
           case 3:
           case 12:
               gameStatus=Status.Lost;
               break;
           default:
               gameStatus=Status.Continue;
               myPoints=totalOfDice;
               break;
           }
       while(gameStatus==Status.Continue){
           totalOfDice=rollDice();
           roll++;
           if(totalOfDice==myPoints)
               gameStatus=Status.Won;
           else if(totalOfDice==7)
               gameStatus=Status.Lost;
       }
       if(roll>21)
           roll=21;
       if(gameStatus==Status.Won){
           GamesWon[roll]++;
           winTotal++;
       }
       else{
           GamesLost[roll]++;
           loseTotal++;
       }
       }
   printStats();  
   }
   public void printStats(){
       int totalGames=winTotal+loseTotal;
       int length=0;
       int RollsToWin;
       int RollsToLose;
      
       for(int x=1; x<=21; x++){
           if(x==21)
               System.out.printf(" %d games won and %d games lost on rolls after the 20th roll", GamesWon[21],GamesLost[21] );
           else
               if(x<=21)
                   System.out.printf(" %d games won and %d games lost on roll %d", GamesWon[x], GamesLost[x], x);
          
           RollsToWin=(1*GamesWon[1])+(2*GamesWon[2])+(3*GamesWon[3])+
           (4*GamesWon[4])+(5*GamesWon[5])+(6*GamesWon[6])+(7*GamesWon[7])+
           (8*GamesWon[8])+(9*GamesWon[9])+(10*GamesWon[10])+(11*GamesWon[11])+
           (12*GamesWon[12])+(13*GamesWon[13])+(14*GamesWon[14])+(15*GamesWon[15])+
           (16*GamesWon[16])+(17*GamesWon[17])+(18*GamesWon[18])+(19*GamesWon[19])+
           (20*GamesWon[20])+(21*GamesWon[21]);
          
           RollsToLose=(1*GamesLost[1])+(2*GamesLost[2])+(3*GamesLost[3])+
           (4*GamesLost[4])+(5*GamesLost[5])+(6*GamesLost[6])+(7*GamesLost[7])+
           (8*GamesLost[8])+(9*GamesLost[9])+(10*GamesLost[10])+(11*GamesLost[11])+
           (12*GamesLost[12])+(13*GamesLost[13])+(14*GamesLost[14])+(15*GamesLost[15])+
           (16*GamesLost[16])+(17*GamesLost[17])+(18*GamesLost[18])+(19*GamesLost[19])+
           (20*GamesLost[20])+(21*GamesLost[21]);
          
           length=(RollsToLose+loseTotal)+(RollsToWin+winTotal);
          
           }
       if((GamesWon[1]/GamesWon[1]+GamesLost[1])>(GamesWon[3]/GamesWon[3]+GamesLost[3])&&(GamesWon[3]/GamesWon[3]+GamesLost[3])>(GamesWon[5]/GamesWon[5]+GamesLost[5]))
           System.out.printf(" Chances of winning decrease as rolls increase");
       else              
           System.out.printf(" Chances of winning increase as rolls increase");
      
   System.out.printf(" %s %d / %d = %.2f%% ", "The chances of winning are", winTotal, totalGames, (100.0*winTotal/totalGames));
   System.out.printf("The average game length is %.2f rolls. ", ((double)length/totalGames));
   }
  
   public int rollDice(){
       int die1=1+randomNumbers.nextInt(6);
       int die2=1+randomNumbers.nextInt(6);
       int sum=die1+die2;
       return sum;
   }
   public static void main(String args[]){
       GameOfCraps game=new GameOfCraps();
       game.play();
      
   }
  
}