Name: _________________________________________
Despite the absence of credible scientific evidence, there are some who continue to believe in ESP (Extra-Sensory Perception). A traditional test for ESP involves generating random integers in some range, say 1 to 3, and then having 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, a believer might say this suggests ESP (although random chance is a more reasonable conclusion).
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: Add an accessor method named getMaxPossible that returns the maximum value in the guessing range. Be sure your method has descriptive Javadoc comments, including an @return entry that describes the return value of the method.
If you were going to perform an ESP test on a person, 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.
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.
- Modify the makeGuess method so that it increments the number of guesses and the number of correct guesses appropriately.
- Add accessor methods named getNumberOfGuesses and getNumberCorrect that return the number of guesses and number of correct guesses, respectively. Similar to existing methods, your new methods should have Javadoc 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 person 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 person. Note that the expected percentage of correct guesses is 100.0 divided by the maximum number in the guessing range. For example:8 out of 20 guesses were correct (40 %). You might have ESP, although it is far more likely that you were just lucky. or1 out of 4 guesses were correct (25 %). You performed exactly as expected. or1 out of 6 guesses were correct (16.6666666666666 %). You definitely do not have ESP.
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 person guessed correctly 2 out of 3 times? Would it surprise you more or less if they guessed correctly 200 out of 300 times? Explain.