Methods Programming Assignment

The assignment

Do programming exercise 5.29 on pages 176-177 of the text.

Hints and Suggestions

The first small technical problem you will confront is how to write the code to simulate the throw of a single die. The solution to this is to use the Math.random() method we used in the second lab to generate random dart locations. In this case, to generate a random integer between 1 and 6 you should use this code

(int) (6*Math.random() + 1)

The next issue you will confront is deciding which methods you will need to construct your program. I suggest you write the following two methods:

int firstThrow()
boolean subsequentThrow(int point)

In both cases these methods will simulate the throw of two dice and print out messages to indicate what happened. For example, calling firstThrow() may result in the message

You rolled 4 + 4 = 8
point is 8

while a call to subsequentThrow(8) may result in the message

You rolled 2 + 4 = 6

or

You rolled 2 + 6 = 8
You win

or

You rolled 2 + 5 = 7
You lose

The firstThrow method also returns the point value that has to be thrown to win, or 0 if the player has won or lost on the first throw. The subsequentThrow method should return true if the player must roll again or false if that throw ended the game.