Your job is to help estimate the "strength" of various combination configurations. Sure, you could just calculate the total number of possible combinations, given the length of a combination and the range of possible values at each position, and use that to estimate the strength of a configuration but it'll be more fun to write a program to test it! You'll write a program that's similar to the "guessing game" lab — your program will generate random combinations, and count how many attempts it take before it correctly guesses a specific combination.
I've created a new project to get you started, but it only contains the beginnings of the Combination class. You'll need to create a new class for ComboGuesser when the time comes, but I'm recommending that you start by getting the Combination class working before writing the ComboGuesser class. Your Combination class should contain the following methods:
toString that builds and returns a string containing the values in the combination. For full credit, the values should be separated by commas, with square brackets around the entire thing. There should not be a comma after the last value. For example, if the combination contains the values 1, 2, and 3, the output from your method should be "[1, 2, 3]". The sample interactions below illustrate the creation of several different Combination objects, and output from toString for each:
> Combination c = new Combination(4, 2); > c.toString() "[2, 1, 2, 0]" (String) > c = new Combination(4, 2); > c.toString() "[1, 1, 0, 2]" (String) > c = new Combination(6, 10); > c.toString() "[1, 6, 10, 8, 7, 0]" (String) > c = new Combination(1, 2); > c.toString() "[2]" (String)
setValue method that takes two integers: The (zero-based) index of the value within the sequence to be changed, and the new value to be stored there. The value should only be changed if the index is valid, and the specified value is within the allowed range.
> Combination c = new Combination(4, 2); > c.toString() "[1, 0, 0, 2]" (String) > c.setValue(0,2); > c.toString() "[2, 0, 0, 2]" (String) > c.setValue(3,1); > c.toString() "[2, 0, 0, 1]" (String) > c.setValue(3,3); > c.toString() "[2, 0, 0, 1]" (String) > c.setValue(4,0); > c.toString() "[2, 0, 0, 1]" (String)
equals that takes a Combination instance as its argument, and returns a boolean value: If the Combination instance passed in has the same length and contents as the instance on which the method is invoked, the method should return true. It should return false if the length or contents differ.
> Combination c1 = new Combination(4, 2); > Combination c2 = new Combination(4, 2); > c1.toString() "[0, 0, 0, 1]" (String) > c2.toString() "[2, 0, 2, 1]" (String) > c1.equals(c2) false (boolean) > c1.equals(c1) true (boolean) > c2.setValue(0,0); > c2.setValue(2,0); > c2.toString() "[0, 0, 0, 1]" (String) > c1.equals(c2) true (boolean)
guessCombo that generates random Combination instances until it finds one that matches the key. It should then print information about the key and the number of guesses required.
The sample outputs below first show a ComboGuesser guessing at a randomly-determined key. Then, a specific key is constructed (setting values within the key to the maximimum and minimum possible helps ensure thorough testing), and a new ComboGuesser is created to guess the specified key. (The output from guessCombo would appear in the terminal window, not the codepad.)
> ComboGuesser g1 = new ComboGuesser(5, 20); > g1.guessCombo(); It took 8160758 guesses to guess [13, 10, 4, 3, 11] > Combination key = new Combination(5, 10); > key.toString() "[5, 3, 9, 4, 4]" (String) > key.setValue(0, 10); > key.setValue(4, 0); > key.toString() "[10, 3, 9, 4, 0]" (String) > ComboGuesser g2 = new ComboGuesser(5, 10, key); > g2.guessCombo(); It took 116561 guesses to guess [10, 3, 9, 4, 0]
/** ... */). For full credit, you should use the @param and @return tags as appropriate in these method comments. Each instance variable should have a brief comment as well. Don't forget the main comment at the top of the class either — I'm looking for more than just a sentence or two.
guessCombo method is only required to find the key once, then quit. It's possible that we'll get lucky on our first effort, and stumble upon the key after relatively few guesses. You could extend the method so that it tries a few times and averages the number of guesses required each time.