Fall 2015

HW2: 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 two dice are assumed to be 6-sided. Modify the constructor so that it has two parameters, specifying the number of sides on each die. Note that the two dice do not have to have the same number of sides.
- Add two accessor methods named
`getNumSides1`

and`getNumSides2`

, which return the number of sides on the first and seecond dice, respectively. - 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 3. 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 3. How many guesses
would it take to guess the number
*in the worst case*? That is, of all the numbers in the range, identify one that would require the most guesses to guess using the optimal strategy?

- Now 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*?

- 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*?

- 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.

You will need to submit three files in the BlueLine drop box:

`PairOfDice.java`

, `GuessingGame.java`

, and a text document containing your answers to the above questions.