Name: _________________________________________
In the real world, making a simple yes or no decision is not always that simple. To assist the indecisive, there are many novelty items on the market, from Mattel's Magic 8-Ball, to Hasbro's Ouija Board, to ThinkGeek's Schrödinger's Cat Executive Decision Maker. Using any of these devices, a person can avoid the responsibility of making a decision for himself/herself, and instead rely on random chance.
For this assignment, you are given a simple class that can randomly select a yes or no answer to a question entered by the user. This class, DecisionMaker, has a
constructor with one parameter, specifying the expected percentage of yes answers. For example, if you construct a DecisionMaker
object with 50 as the constructor parameter, it will be equally likely (50/50) to answer yes or no. However, constructing a DecisionMaker
object with 60 as the constructor parameter would increase the likelihood (60/40) of answering yes. The getAnswer method has one parameter, the user's question (a String), and returns either "YES" or "NO" (with the appropriate likelihood).
EXERCISE 1: Load this class into the BlueJ IDE and create a DecisionMaker object that generates yes/no answers with equal likelihood. Call the getAnswer method 10 times and keep track of the number of yes and no answers. Are they roughly equal?
Would it surprise you if 7 out of 10 answers were "YES"? Would it surprise you more or less if 70 out of 100 were "YES"? How about 700 out of 1,000? Explain your answers.
EXERCISE 2: Create anotherDecisionMaker
object, only this time specify 70 as the constructor parameter. Then call thegetAnswer
method 10 times and keep track of the number of yes answers. Is the count close to 7? Make another 10 calls and combine the number of yes answers. Is that total, out of 20, closer to the expected 14? Would you expect it to be?
EXERCISE 3: If you created aDecisionMaker
object with a constructor parameter of 100, what would you expect the behavior of thegetAnswer
method to be? Explain your answer and verify it. Similarly, what would the behavior be if the constructor parameter were 0? Explain and verify.
EXERCISE 4: What would happen if you created aDecisionMaker
object with a constructor parameter outside of the range 0 to 100? Would there be an error? If not, how would thegetAnswer
method behave? Explain your answer and verify it.
EXERCISE 5: It is sometimes desirable for a class to have more than one constructor. This usually occurs when there are multiple ways to create and initialize an object. For example, the currentDecisionMaker
constructor requires the user to specify an arbitrary yes percentage, but most of the time you would expect them to want a fair and balanced decider. To save them from having to specify 50 in these default cases, we could define a second constructor that automatically sets the yes/no probability to 50/50. This default constructor would not require a parameter, since it behaves the same every time. The constructor below accomplishes this by calling the existing constructor with a parameter of 50. In this way, the new default constructor does not duplicate the assignment in the existing constructor; it instead makes use of it./** * Constructs an object that chooses between "YES" and "NO" (with equal probability). */ public DecisionMaker() { this(50); // calls the existing constructor with 50 as parameter } Add the new default constructor to the DecisionMaker class, above (but NOT inside!) the existing constructor. Recompile your modified class and verify that BlueJ now provides the option of creating a
DecisionMaker
using either constructor.
To be convinced that the DecisionMaker
is behaving according to specs, we would like to be able to maintain and view statistics on its behavior. In particular, we would like, at any point, to view the percentage of yes answers that it has given, as well as the percentage of no answers. This requires maintaining the yes and no counts as part of the DecisionMaker
's state. Initially, when a DecisionMaker
object is
created, the number of yes and no answers should be initialized to 0. Each time the
getAnswer
method is called, the appropriate count (depending on whether yes or no was selected) should be incremented. Finally, methods that access the counts and calculate the percentages should be added.
EXERCISE 6: Modify the DecisionMaker class so that it maintains the number of yes and no answers as part of an object's state. In particular:
- Add two fields to the class, both ints, named numYesses and numNos.
- Add assignment statements to the DecisionMaker constructor (the version with a parameter) to initialize both of the fields to 0.
- Add assignment statements to the getAnswer method that add one to each count when the corresponding answer is selected.
- Add methods named getYesPercentage and getNoPercentage, which return the percentage of yes and no answers, 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.
EXERCISE 7: The following method,checkStats
, calls thegetYesPercentage
method to get the percentage of yes answers so far, then compares that with the expected percentage (as stored in theexpectedYesPercentage
field). It then displays an evaluative message, depending on whether the percentages were within 10 of each other./** * Checks whether the object is within 10% of the expected yes percentage. * @return a String specifying whether the percentage of yesses reported so far * is within 10% of the expected percentage */ public String checkStats() { double yesP = this.getYesPercentage(); if (Math.abs(yesP - this.expectedYesPercentage) <= 10) { return "Everything appears to be working as expected."; } else { return "Anomalous results - may warrant further checking."; } } Add this new method at the bottom of the DecisionMaker class, before the final curly-brace (but NOT inside another method!). What would happen if you called the
checkStats
method without having calledgetAnswer
? Would this cause an error? If not, what message would be returned? Explain your answers and verify.
Modify this method so that it can return 3 possible messages. In addition to the "
Everything appears to be working as expected.
" message, it should be able to distinguish between "Anomalous results - too many yesses.
" and "Anomalous results - too many nos.
"