Programming Project 1: Listing Solutions to the Counting Problems

In this project our goal is to create methods that will list all solutions to our four counting problems, given parameters n (the number of items to be chosen from, which will be the set {1,...,n}) and k (the number of items to be chosen). Remember the types of sequences we are counting in each case:

1. First Problem: Count all sequences of length k where the items are in the set {1,...,n}. We will call these sequences `Sequence` objects.
2. Second Problem: Count only the sequences that have no element occurring more than once. We will call these sequences `Perm` objects.
3. Third Problem: Count sequences with no repeated elements that are also sorted, so that no larger element occurs before a smaller element. We call these sequences `Set` objects.
4. Fourth Problem: Count the sorted sequences, even if they include repeated elements. We call these sequences `Multiset` objects.

We will thus have a class `Sequence` and classes ```Perm ```, `Set`, and `Multiset` extending ``` Sequence```. You can see one attempt at the basic methods for the `Sequence` class in Excursion 6.8 of the book, on page 6-41. But we're going to make a few changes in that.

In particular, the `iterate` method, that changes a sequence to the one that follows it in lexicographic order, is much easier to implement if it is recursive, and to do that we don't want to commit ourselves to storing the values as an array of `int`'s. Thus we'll replace the array `values` with a method `getValues` that will return the array of values.

Assignment 1:

Write a class `Sequence` that stores its values in an integer array `values`, filling in the code in the skeletons below. You should place your code in a file in the `cs240` directory of your edlab account.

Assignment 2:

Write a class `Sequence` that stores its values using an `int` named `first` and a `Sequence` named `theRest`, filling in the code in the skeletons below. Your methods should operate recursively when possible. This code should be in a separate file from your solution to Assignment 1.

Here is a skeleton version of the code for `Sequence`, indicating the API:

``````    public class Sequence {
public int n; // elements will be in {1,...,n}
public int k; // number of elements

protected int[] values; // the elements, in non-recursive version
protected int first; // first element, in recursive version
protected Sequence theRest; // rest of elements, in recursive version

public Sequence(int n, int k)
{// constructor, gives first sequence, all 1's
// you write code, two versions
}

public Sequence(int n, int[] values)
{// constructor, gives Sequence of length values.length, setting n
// you write code, two versions
}

public int [] getValues()
{// returns the elements as an array
// you write code, two versions
}

public void initialize()
{// replaces values of calling object with first sequence (all 1's)
// you write code, two versions
}

public String toString() {
String w = "";
if (k == 0) return w;
else {
w = Integer.toString(getValues()[0]);
for (int i=1; i<k; i++)
w += (" " + Integer.toString(getValues()[i]));}
return w;}

public boolean isFirst()
{// returns whether this is the first sequence of its type
for (int i=0; i<k; i++)
if (getValues()[i] != 1) return false;
return true;}

public void iterate ()
{// changes this sequence to next one in order
// you write code, two versions
// last sequence iterates to first one
}

public void printAll()
{// prints all sequences with length k and range n of calling object
// destroys value of current object
initialize();
do {
System.out.println(toString());
iterate();}
while (!isFirst());}
``````

Assignment 3:

Write recursive code for the three classes `Perm`, `Set`, and `Multiset`, each extending the recursive (Assignment 2) version of `Sequence`. This means overwriting the following methods:
• the constructors, to either create an initialized object or create an object with a given array as its values
• `initialize`, to reset the values to the first object
• `isFirst`, to test for the first object
• `iterate`, to give the next object of the new class
• `printAllFromHere`, which is like `printAll` but starts with the current value of the calling object and prints only values that come after it in the `printAll` list. Can you write this only once for the four classes?

Assignment 4:

Also write static methods that use your classes to give the following output:
• Lists of all the sequences, all the perms, all the sets, and all the multisets with n = 4 and k = 3.
• A list of all five-element subsets of the set {JC, JD, JH, JS, QC, QD, QH, QS, KC, KD, KH, KS} that do not contain three or more J's, three or more Q's, or three or more K's. This solution should be useful for Problem 6.6.3 on the homework. Note that you will need a different version of `toString` to print the new names of the twelve elements rather than {1,...,12}.
• A list of the three-letter words over the alphabet {A,...,Z} (all upper case) that come after "WOW", and then lists of all the perms, sets, and multisets (respectively) within this list. Note that "WOW" itself is not a perm, a set, or a multiset. Again, you will need a new `toString` method.