Introduction

Bill Gates was pretty impressed with your bouncing boulders, and has been talking you up to his poker buddies. One of them, who wishes to remain anonymous, wants your help installing a new electronic lock on the safe where he keeps his poker winnings. (Apparently, Bill loses a lot of money.) The "combination" for the lock is a sequence of integers, and the anonymous poker player is trying to decide how long this key sequence should be (how many numbers it contains), and the range of values used for each number in the key (between zero and some upper limit). He wants to make sure it's hard for intruders to guess, but his memory isn't what it used to be, and he wants to keep it on the simple side if possible.

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! Your program will generate random combinations, and count how many attempts it take before it correctly guesses a specific combination.

The Assignment

Your program will consist of two parts: A Combination class that represents the sequence of integers that make up a combination, and a ComboGuesser class that repeatedly creates Combination instances, and compares them to the "right answer": see image.

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 newclass 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:

1.A constructor that takes two integer arguments: The length of the combination instance to be constructed, and the maximum value for each of the numbers in the combination. It should initialize the Combination instance to hold random values within the appropriate range for each of the values in the sequence.

2.A method called 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)

3.A 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)

4. A method called 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)

After testing your Combination class thoroughly, create a new ComboGuesser class. The ComboGuesser class will keep track of a specific Combination (we'll call it the "key"), and repeatedly generate random Combination instances until it finds one that matches. Your ComboGuesser class should contain the following methods:

  • A constructor that takes two arguments: The length of the combination sequence to be tested, and the maximum value in each position. It generates a random Combination instance as the key.
  • A constructor that takes three arguments: The length of the combination and the maximum value, as before, plus a Combination instance to use as the key. (You may assume that the length and maximum value passed as arguments are the same as the values used to create the Combination instance.)
  • A method called 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]

Comments

Each and every method should have a "javadoc-style" comment above it (the ones that use /** ... */). 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, and you should look for opportunities to use constants to tidy up your code. Don't forget the main comment at the top of the class either I'm looking for more than just a sentence or two.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.