Name: _________________________________________



CSC 221: Computer Programming I         Fall 2005

HW 2: Classes and ESP

The traditional test for determining whether someone has ESP (Extra-Sensory Perception) involves having them guess randomly selected values. For example, you might generate random integers in some range, say 1 to 3, and then have the person try to guess each number. Given three possible values, you would expect random guessing to be correct roughly one third of the time. If a person were correct significantly more often, you might be tempted to say that they have ESP (or, more scientifically, that they were just lucky this time).

For this assignment, you are given a simple class that can generate random integers in some range and compare them with the user's guesses. This class, ESPTester, has a constructor with one parameter, specifying the maximum value in the range of random numbers. For example, if you construct an ESPTester object with 3 as the constructor parameter, it will generate random numbers in the range 1..3. The makeGuess method has one parameter, the user's guess, and returns a String specifying whether that guess matched a randomly selected number from the range.


EXERCISE 1:    Load this class into the BlueJ IDE and create an ESPTester object that generates numbers from the range 1 to 3. Call the makeGuess method several times and try to guess each number. What message is returned when your guess is correct? What message is returned when your guess is incorrect?





Does an error occur if you call the method with a guess outside of the specified range, e.g., -1? If not, what happens?






EXERCISE 2:    What would happen if you created an ESPTester object and specified the value 1 for the constructor? Would this object still work? How predictable would it be? Create such an object and verify your answer.






EXERCISE 3:    As it is currently written, the user specifies the maximum number in the range when she constructs the ESPTester object. However, if she forgets this number after the object has been created, there is no way of reminding herself (short of inspecting its fields). Add an accessor method named getMaxPossible that returns the value of the maxNumber field. Be sure your method has description comments, including a @return entry that describes the return value of the method.


Adding State to Maintain Statistics

If you were going to perform an ESP test on a subject, you would have him or her repeatedly guess numbers and keep track of how many times they guessed correctly. While this could be done using the current version of ESPTester, it does place a burden on the experimenter to keep track of the number of guesses and how many of those guesses were correct. A better solution would be to make these numbers part of the ESPTester object's state. Initially, when an ESPTester object is created, the number of guesses and number of correct guesses is zero. Each time the makeGuess method is called, the number of guesses should increase by one, and the number of correct guesses should increase by one if the user's guess matches the program's pick. Accessor methods could then be added to the class to allow the experimenter to view these numbers at any point in the experiment.


EXERCISE 4:    Modify the ESPTester class so that it maintains the number of guesses and correct guesses as part of an object's state. In particular:

Be sure to test your modifications carefully by creating objects and inspecting their state before and after method calls.



Adding Functionality to Compute Statistics

While the additional state you added to the ESPTester class makes it easier to access the raw statistics, it still requires the experimenter to compute the percentage of correct guesses and then compare that with the expected percentage to determine if the subject has ESP. These tasks can easily be automated by adding a new method to the class.


EXERCISE 5:    Add a new method named showStats that displays the current statistics from the experiment. When called, the method should display the number of correct guesses, the total number of guesses, the percentage correct, and a determination as to whether that percentage suggests ESP on the part of the subject. Note that the following general fomulas apply:

<percentage correct> = 100.0 * <number of correct guesses> / <total number of guesses> <expected percentage> = 100.0 / <maximum number in the guessing range>

If the percentage of correct guesses exceeds the expected percentage, then the message should acknowledge that they might have ESP. For example, to whether the user has ESP or not should be displayed based on whether the expected assuming the maximum number in the guessing range is 4, the following messages might be displayed as a result of calling showStats.

8 out of 20 guesses were correct (40 %). You might have ESP, although it is far more likely that you were just lucky. 1 out of 6 guesses were correct (16.666666666666668 %). You definitely do not have ESP.

Be sure that the method behaves reasonably if it is called before any guesses are made.


EXERCISE 6:    Using your new class definition, create an ESPTester object with a maximum value of 5, and experiment on yourself.







Hand in a printout of your modified ESPTester class along with these sheets. In order to provide feedback on this assignment before TEST 1, late assignments will not be accepted after the beginning of class on 9/27.