CSC 221: Computer Programming I
Spring 2008

HW4: Class Design & Conditionals


PART 1: Pair Of Dice

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.

PART 2: Guessing Game

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.

PART 3: Guessing Strategy

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:

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

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

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

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

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