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.
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:
- Add two integer fields to the class for storing the number of guesses and the number of correct guesses.
- Add assignment statements to the ESPTester constructor to initialize both fields.
- Add a statement to the makeGuess method that increments the number of guesses. This statement should be placed in the method so that it is executed every time the method is called.
- Add a statement to the makeGuess method that increments the number of correct guesses. This statement should be placed in the method so that it is executed only when the guess is correct.
- Add accessor methods named getNumGuesses and getNumCorrect that return the number of guesses and number of correct guesses, respectively. Similar to existing methods, your new methods should have comments that describe their behavior.
Be sure to test your modifications carefully by creating objects and inspecting their state before and after method calls.
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:
= 100.0 * / = 100.0 /
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.
- What verdict, ESP or no ESP, should showStats display if you have not yet made any guesses? Explain your reasoning, and verify your prediction using the ESPTester object.
- Make one guess using the object. Was your guess correct? What verdict should showStats display now? Verify your prediction.
- Make nine more guesses, for a total of ten. How many guesses out of the ten were correct? What verdict should showStats display now? Verify your prediction.
- Make ten more guesses, for a total of twenty. How many guesses out of the twenty were correct? What verdict should showStats display now? Verify your prediction.
- Given a potential range of 5 numbers, would it surprise you if a subject guessed correctly 2 out of 3 times? Would it surprise you more or less if they guessed correctly 200 out of 300 times? Explain.