Spring 2008

HW4: Class Design & Conditionals

For the first part of this assignment, you will extend the existing PairOfDice class. This class encapsulates two six-sided dice so that they can be rolled together.

- Currently, the number of sides on the two dice is hard-coded in the constructor. If we wanted to change the number of die sides, we would have to change both instances of 6 in the constructor to the new number. Instead, modify the class by adding a constant (i.e., a static final field) that is assigned to be 6, then use that constant in the constructor code.
- Add an accessor method named
`getDieSides`

that returns the number of sides on either die. - Add additional state to a
`PairOfDice`

object so that the number of doubles rolled so far is maintained. - Add a method named
`percentageOfDoubles`

that returns the percentage of rolls so far that were doubles. It should return 0.0 if called before any rolls have taken place.

For the second part of this assignment, you are to design and implement a class
for playing a simple guessing game. When created, a `GuessingGame`

object
should randomly select a number between 1 and some (user-specified) maximum, and then allow the
user to try to guess the number. Each time a guess is made, the object should
inform the user whether they are correct, too high, or too low. It should also
be possible to determine the number of guesses made by the user.

For example, suppose a `GuessingGame`

object picked the number 37. If the user
initially called the `makeGuess`

method with a guess of 50, then the
method should return the string `"50 is too high. Guess again..."`

.
If he/she then
called `makeGuess`

with a guess of 30, then the method should return
`"30 is too low. Guess again..."`

. Finally, if he/she calls
`makeGuess`

with the correct guess of 37, then the method should return
`"37 is correct!"`

.

The detailed behavior of this class is defined by the GuessingGame.html javadoc page. Your class should meet the specifications in this page, and include comments that would generate a similar page.

One you have your class implemented, you can consider strategies for playing the guessing game. If you make guesses at random, you might get lucky and guess the correct number quickly or it might take a very long time. It turns out that there is an optimal strategy for making guesses that minimizes the worst case. That is, you can guarantee finding the answer in a set number of guesses, and that number is as small as it can be (in terms of a guarantee). The strategy is simply to pick the middle number in the possible range of numbers on each guess. For example, if the range is 1 to 100, then you start by guessing 50. If that is too high, then you know the number is between 1 and 49. Next, pick the middle number in that range, 25. If that is too low, then you know the number is between 26 and 49. Pick the middle number in that range, 37, and so on until the number is guessed.

Answer the following questions using your `GuessingGame`

class:

- Suppose you know the number is from the range 1 to 100. How many guesses
would it take to guess the number
*in the best case*? That is, of all the numbers in the range, which one would require the fewest guesses to guess using the optimal strategy?

- Similarly, suppose you know the number is from the range 1 to 100. How many guesses
would it take to guess the number
*in the worst case*? That is, of all the numbers in the range, which one would require the most guesses to guess using the optimal strategy?

- Now suppose you know the number is from the range 1 to 200. How many guesses
would it take to guess the number
*in the best case*?

- Similarly, suppose you know the number is from the range 1 to 200. How many guesses
would it take to guess the number
*in the worst case*?

- In general, if you know that it takes N guesses
*in the worst case*to guess a number from the range 1 to X, how many guesses would it take*in the worst case*to guess a number from the range 1 to 2X? Justify your answer.