Name: _________________________________________

CSC 221: Object-Oriented Programming
Spring 2012

HW 1: 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 guessNumber 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 guessNumber 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 the following accessor method to the ESPTester, which allows the user to see the value of the maxNumber field. Recompile your modified class and test this method on several different ESPTester objects to verify that it behaves as described. /** * Accessor method that allows the user to determine the range for guesses. * @return the maximum number in the range to be guessed (the minimum number is 1) */ public int getMaxPossible() { return this.maxNumber; }

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 guessNumber 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 new methods to the class.

EXERCISE 5:    Add a new method named percentageCorrect, which computes and returns the percentage of time the user guessed correctly. Note that the following general formula defines this percentage: <percentage correct> = 100.0 * <number of correct guesses> / <total number of guesses> Since a percentage need not be a whole number, your method should return a value of type double. Be careful in defining your method so that it behaves reasonably when the number of guesses is zero. If the percentageCorrect method is called before any guessing takes place, it should return 0.0. As before, your method should have descriptive comments. Be sure to test your method thoroughly.

EXERCISE 6:    Consider the following method, ESPverdict, which compares the subject's percentage of correct guesses with the expected percentage and returns a String stating whether they have ESP. /** * Determines whether the user might have ESP by comparing his/her * percentage of correct guesses with the expected percentage. * @return a String specifying whether the user guessed better than expected */ public String ESPverdict() { double percent = this.percentageCorrect(); // percentage user was correct double expected = 100.0/this.maxNumber; // expected percentage if guessed randomly if (percent > expected) { // if user did better than expected, return "You just might have ESP!"; // admit they MIGHT have ESP } // else { // otherwise, break the bad news return "You definitely do not have ESP."; } }

Modify this method so that it can return 3 possible messages. If the user's percentage and the expected percentage are equal, return the message "The results are inconclusive.". Otherwise, it should return one of the two messages above, depending on whether the user did better or worse than expected. Hint: since there are three alternatives here, you will want to implement a cascading if statement. Add this method to the ESPTester class and test it thoroughly.

EXERCISE 7:    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.