Starting from:
$35

$29

PROGRAM #10 Solution

Outcomes:    
    • Implement methods following specifications.
    • Use loops to implement a game.

Background:
In the game of "Craps", two dice are rolled, and the player wins or loses according to the following rules:
    • If the dice sum is 7 or 11, the player automatically wins.
    • If the dice sum is 2, 3, or 12, the player automatically loses.
    • If the player rolls any other sum, then the player continues rolling until one of the following occurs:
        ◦ that same sum is rolled again, in which case the player wins, or…
        ◦ a 7 is rolled, in which case the player loses (note that rolling a 7 on the first roll is an automatic win, but rolling a 7 on any other roll is a loss)

Assignment:
You are to write a program that asks the user how many games of craps to play, and then plays those games, reporting the percentage of wins.  Here is a sample output for your program (the red numbers are values entered by the user.  You should repeatedly prompt the user until a positive number is entered.
How many rounds of craps do you want to play? -2
Enter a positive number please: -18
Enter a positive number please: 0
Enter a positive number please: 5
Round #1: 3 (lose)
Round #2: 9 8 2 5 9 (win)
Round #3: 12 (lose)
Round #4: 5 2 6 8 12 2 3 11 4 9 7 (lose)
Round #5: 11 (win)
------------------------------
Wins: 2 out of 5 (40.0%)

Specifics:
Create a class named Program10.  Your program should have the following 3 methods (other helper methods are allowed, but these three methods must be implemented as specified): 
    • rollDice() which has no parameters, and returns the sum of two dice as an int.  Note that, with real dice, certain sums are more likely than other sums.  For example, 6 is a much more likely sum to obtain than 11 because there are six different ways to roll a 7 (1 and 5, 2 and 4, 3 and 3, 4 and 2, 5 and 1) but there are only two ways to roll 11 (5 and 6, 6 and 5).  Your method should return values based on this.


    • craps() which has no parameters, and which plays one round of craps (using the rollDice() method for each roll), printing the sums that were rolled, and returning a boolean at the end to indicate whether the player wins or loses.  For example, the method might print 9 2 11 8 9 and return true because the player's first roll was a 9, and then the player rolled repeatedly getting a 9 before getting a 7.  Note that this method does not print the word "win" or "lose".  Those words are printed by the main() method (see below).  Here are some samples of what could happen if you call the craps() method:
 
What might get printed by craps()
What would get returned
4 5 8 12 2 4
true
8 8
true
5 2 7
false
7
true
3
false
 
    • main() asks the user how many rounds of craps to play, repeatedly prompting until the user enters a positive number.  The program then would play that many rounds, one line per round, printing "win" or "lose" at the end of each one.  Format your output to match the format shown in the sample, including rounding the percentage to one place past the decimal point, showing the round # at the beginning of each line, and drawing 30 hyphens before the result.

Submit your source code as a zip file.

Advice: 
    1. Save the main() method for last.  First make sure that rollDice() works.  Once it works, then work on the craps() method to be sure that you can play one game of craps().  Once craps() is working, then begin thinking about the main() method.
    2. When writing the main() method, there should be a check to make sure the user enters a valid number.  You can make that the very last part of the main() method that you implement.  Make sure that your main() method works with good numbers first.  Then, you can insert the necessary code to check for valid user input.
    3. Remember that you can upload your code to the course website as often as you want.  If you reach an important milestone with your code (such as completing a method), upload your code to the website as a draft.  That way, if anything goes wrong, you at least have a previous draft that is safe on the website.

More products