0% found this document useful (0 votes)
105 views12 pages

Problem IN JAVA - in The Game of - C-R-A-P-S - (Chegg...

Uploaded by

Hadiqa Shahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views12 pages

Problem IN JAVA - in The Game of - C-R-A-P-S - (Chegg...

Uploaded by

Hadiqa Shahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.

com

 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / problem in java : in the game of "c-r-a-p-s"(chegg wo…

Question: Problem IN JAVA : In the game of "c-r-a-p-s"(Chegg won't let … Post a question
Answers from our experts for your tough
homework questions
See this question in the app
Enter question

Problem IN JAVA
: In the game of "c-r-a-p-s"(Chegg won't let me post the actual word), a pass line bet proceeds as follows.
Two six sided dice are rolled: the rst roll of the dice in a c-r-a-p-s round is called the “come out roll.” A Continue to post
come
20 questions remaining
out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically
loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll that number becomes “the
point.” The player keeps rolling the dice until either 7 or the point is rolled. If the point is
rolled rst, then the player wins the bet. If a 7 is rolled rst, then the player loses. Snap a photo from your
phone to post a question
Write a program that simulates a game of c-r-a-p using these rules without human input.
We'll send you a one-time download
Instead of asking for a wage, the program should calculate whether the player would win link
or lose. The program should simulate rolling two dice and calculate the sum. Add a loop
so that the program plays 10,000 games. Add counters that count how many times the 888-888-8888 Text me
player wins, and how many times the player loses. At the end of the 10,000 games,
By providing your phone number, you agree to receive a one-time
compute the probability of winning (wins/ (wins + losses)) and out put this value. automated text message with a link to get the app. Standard
messaging rates may apply.
Note: to simulate the dice rolling use Random class to generate numbers between 1 and 6
inclusive or you can use the random method from the Math class.
Here is an example series:
The shooter throws the dice on a come out roll, which starts a new series, and a 5 is rolled. (Recall that My Textbook Solutions
rolling a 7 or 11 would have been winners and 2, 3, or 12 would have been losers.)
          
             The shooter has established a Point of 5.
The shooter throws the dice again and rolls an 8.
              Nothing happens and player rolls the dice again.
Laboratory... Electronic... Signals and...
The shooter throws the dice again and rolls a 3. (Note that rolling a 2, 3, 11, or 12 after a point is established
means nothing ) 9th Edition 11th Edition 2nd Edition

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 1/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
The shooter throws the dice again and rolls a 5. View all solutions
 
NEW!
            This Textbook
is the shooter's point so Solutions
this is a win Expert
and the series ends.Q&A Study Pack Practice  

Your program should:


* Declare the variables numOfWins and numOfLoss to keep track of the number of times player wins or Computer Science Chegg
loses
tutors who can help right
* Add a loop to your program so that the game is played 10,000 times.
now
* Add another loop so the player can start a new set of games.
* declare a constant for the number of games being played. (10,000)
Aditya A.
767
* Simulate rolling the two dice and calculate the sum. This rst roll will establish the comeOutRoll . (use NIT Bhopal
Random class to generate random numbers for the dice)
* Pass this rst comeOutRoll to a method called winOrLose. This method will return a string. If it returns: Bishrut J.
110
BE Engineering
* “win”: means a win. Then the variable numOfWins must be incremented in the main method.
* “loss”: means a loss then the variable numOfLoss must be incremented in the main method
Rashi A.
213
* “The Point”: means a point has been established. Therefore set the variable thePoint to the comeOutRoll. G.B.pant university
Then the method keepRollingmust be called to roll the dice until a 7 or “the point” is rolled. This method
will return a string:
* “seven” means a loss. therefore the variable numOfLoss must be incremented in the main method Find me a tutor
* “the point” means a win. Therefore, the variable numOfWin must be incremented in the main method
* At the end of the 10,000 games, call the method winProability to compute the probability of winning
(wins/wins + losses)
* output the value returned
* Ask the user if she/he wants to start a new game.
* Output a good by message if the user does not want to play again.
List of the methods for the C-r-a-p-s game
1. main method: Has only one line of code which is a call to the method play
public static void main(String[] args){
               play()
              description() }
      
2. method play:
           public static void play()
          {
             Declare your variables numOfwin, numOfLoss, thePoint, comeOutRoll,
            numOfPlays and set them to zero
            boolean playagain = true
           While (playAgain)
         {

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 2/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
            for (int i =0; i< 10,000; i++)
          { Textbook Solutions
 NEW!
Expert Q&A Study Pack Practice  

* Since you roll the dice two times you need to generate two
                random numbers between 1 and 6 inclusive   and then add them
               up. This will be the value for the variable
               comeOutRol
* Call the method winOrLoss
based on the returned value from this method:
              * increment the variable numOfWins
* or increment the variable numOfLoss
* or set the variable thePoint to comeOutRoll and then call the method keepRolling.
                 Based on the value returned from this method adjust the value of numOfLoss and NumOfWin
}
call the method winProability
output the return value from the method winProability
reset all the variables
ask the user if he/she wants to start another set of games
}
3. winOrLoss method: in this method you can only have one return statement.
public static String winOrLoss(int comeOutRoll)
{
// if comeOutroll is 7 or 11 return “win”
//if comeOutRoll is 2, 3, or 12 return the string “Loss”
//if comeOutRoll is 4, 5, 6,8, 9, or 10 return the string”the point”
}
4. description method: outputs the description of the game
public static void description()
{
   //output the description
}
5. KeepRolling method : you must only use one return statement.
public static String(int thePoint)
{
   //keep rolling the dice until you get thePoint or your get seven.
   //return the string “seven” if you get a seven
  //return the string “thePoint” if you get the point
https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 3/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
}
 
NEW!
6. winProability method : Textbook Solutions Expert Q&A Study Pack Practice  

  public static double winProability(int wins, int losses)


{   
    //calculate the proability
    //return the value
}
Sample output:
Computer will play a c-r-a-p game for you. Here are the
rules of the game:
Two six sided dice is rolled
Come out roll: The rst roll of the dice in a c-r-a-p-s
round
A come out roll of 7 or 11 automatically wins
A come out roll of 2, 3, or 12 automatically losses
A come out roll of 4, 5, 6, 8, 9, or 10 becomes The Point
If the player gets the point he/she will keep playing by
rolling the dice until he/she gets a 7 or the point.
If the point is rolled rst, then the player wins the
bet.
If a 7 is rolled rst, then the player loses.
Let’s start playing
In the simulation we :
won 4905 times
lost 5095 times,
for a probability of 0.4905
Hit enter key to continue
Would you like to play another game yes/no?dsfdf
Would you like to play another game yes/no?sdf
Would you like to play another game yes/no?ds
Would you like to play another game yes/no?123
Would you like to play another game yes/no?yes
In the simulation we :
won 9750 times
lost 10250 times,
for a probability of 0.4875
https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 4/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
Hit enter key to continue
Would 
 NEW!
Textbook
you like to play another game yes/no?Solutions
yes Expert Q&A Study Pack Practice  

In the simulation we :
won 14722 times
lost 15278 times,
for a probability of 0.4907
Hit enter key to continue
Would you like to play another game yes/no? no
Have a nice day. GoodBye

Expert Answer

Anonymous answered this


Was this answer helpful? 1 0
313 answers

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template le, choose Tools | Templates

* and open the template in the editor.

*/

package crapsgame;

import java.util.Random;

import java.util.Scanner;

/**

* @author Vishalkumar.Jain

*/

public class CrapsGame {

// initializing class variables

static int numOfwin = 0, numOfLoss = 0, thePoint = 0, comeOutRoll = 0, numOfPlays = 0;

/**

* Rolls two dices and returns the sum of them


https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 5/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
* @return sum of two rolled dices
 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  
*/

public static int rollDices() {

Random rand = new Random();

int num = rand.nextInt(6) + 1;

return num + rand.nextInt(6) + 1;

/**

* Takes a comeOutRoll and return appropriate value, based upon this

* @param comeOutRoll

* @return result

*/

public static String winOrLose(int comeOutRoll) {

switch (comeOutRoll) {

case 7:

case 11:

return "win";

case 2:

case 3:

case 12:

return "loss";

default:

return "thepoint";

/**

* Prints the description of the Game

*/

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 6/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
public static void description() {
 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  
System.out.println("Computer will play a c-r-a-p game for you. Here are the\n"

+ "\n"

+ "rules of the game:\n"

+ "\n"

+ "Two six sided dice is rolled\n"

+ "\n"

+ "Come out roll: The rst roll of the dice in a c-r-a-p-s\n"

+ "\n"

+ "round\n"

+ "\n"

+ "A come out roll of 7 or 11 automatically wins\n"

+ "\n"

+ "A come out roll of 2, 3, or 12 automatically losses\n"

+ "\n"

+ "A come out roll of 4, 5, 6, 8, 9, or 10 becomes The Point\n"

+ "\n"

+ "If the player gets the point he/she will keep playing by\n"

+ "\n"

+ "rolling the dice until he/she gets a 7 or the point.\n"

+ "\n"

+ "If the point is rolled rst, then the player wins the\n"

+ "\n"

+ "bet.\n"

+ "\n"

+ "If a 7 is rolled rst, then the player loses.\n"

+ "\n"

+ "Let’s start playing...\n");

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 7/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
}
 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  
/**

* This method keeps rolling the dices until the number comes out

* to be equal to thepoint or seven

* @param thepoint

* @return string value based on the outcome of the dices at the end

*/

public static String keepRolling(int thepoint) {

int num;

do {

num = rollDices();

} while (num != 7 && num != thepoint);

return num == 7 ? "seven" : "thepoint";

/**

* Calculates and returns the winning probability as per the formula:

* (win / (win + losses))

* @return winProbability

*/

public static double winProbability() {

double probability = (double)numOfwin / (double)(numOfwin + numOfLoss);

return probability;

/**

* Invokes the gameplay

*/

public static void play() {

numOfPlays++;

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 8/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
boolean playagain = true;
 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  
while (playagain) {

for (int i = 0; i < 10000; i++) {

comeOutRoll = rollDices();

switch (winOrLose(comeOutRoll)) {

case "win":

numOfwin++;

break;

case "lose":

numOfLoss++;

break;

case "thepoint":

thePoint = comeOutRoll;

String result = keepRolling(thePoint);

if (result.equals("thepoint")) {

numOfwin++;

} else {

numOfLoss++;

break;

System.out.println("In the simulation we:");

System.out.println("won " + numOfwin + " times");

System.out.println("lost " + numOfLoss + " times");

System.out.println("for a probability of " + winProbability());

boolean inCorrectInput = true;

do {

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 9/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
System.out.println("Would you like to play another game yes/no? ");
 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  
Scanner sc=new Scanner(System.in);  

String ans = sc.next();

if (ans.toLowerCase().equals("no")) {

System.out.println("Have a nice day. GoodBye");

playagain = inCorrectInput = false;

} else if (ans.toLowerCase().equals("yes")) {

numOfwin = numOfLoss = thePoint = comeOutRoll = 0;

inCorrectInput = false;

} while (inCorrectInput);

public static void main(String[] args) {

description();

play();

// TODO code application logic here

View comments (1)




Practice with similar questions

Q: Problem IN JAVA : In the game of "c-r-a-p-s"(Chegg won't let me post the actual word), a pass line bet proceeds as
follows. Two six sided dice are rolled: the rst roll of the dice in a c-r-a-p-s round is called the “come out roll.” A come
out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled
on the come...

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 10/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com
A: See answer
 
NEW!
Textbook Solutions Expert Q&A Study Pack Practice  

Up next for you in Computer Science

In cryptarithmetic In cryptarithmetic
puzzles, puzzles,
mathematical equations mathematical equations
are are
writtenusing letters.Each written using letters.Each See more questions
letter can be a digit from letter can be a digit from for subjects you study
0 to 0 to
9, but no two letters can 9, but no two letters can

See answer See answer

Questions viewed by other students

Q: Crap Game: Problem: In the game of craps, a pass line bet proceeds as follows. Two six sided dice are rolled: the rst
roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come
out roll of 2, 3, or 12 automatically loses. if 4, 5, 6,8,9, or 10 is rolled on the come out roll, that number becomes “the
point.” The player...

A: See answer

Q: In the game of craps, a pass line bet proceeds as follows. Two six-sided die are rolled: the rst roll of the dice in a
craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or
12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll that number becomes “the point.” The player
keeps rolling the...

A: See answer

Show more 

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 11/12
10/27/2020 Problem IN JAVA : In The Game Of "c-r-a-p-s"(Chegg... | Chegg.com

 
NEW!
COMPANY Textbook Solutions Expert Q&A Study Pack Practice  

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2020 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/problem-java-game-c-r-p-s-chegg-won-t-let-post-actual-word-pass-line-bet-proceeds-follows--q28523272 12/12

You might also like