Name: _________________________________________


CSC 222: Object-Oriented Programming
Fall 2017

HW 2: Classes and ESP

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.


Adding State to Maintain Statistics

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:

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 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. or 1 out of 4 guesses were correct (25 %). You performed exactly as expected. or 1 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.


Submit your modified ESPTester class via the BlueLine dropbox, and turn in these sheets with your written answers. You may scan and submit these sheets via the dropbox if you prefer.