CMPSCI 240: Reasoning About Uncertainty

David Mix Barrington

Brandon McPhail

Fall, 2009

Programming Project 1: Listing Solutions to the Counting Problems

Posted Thursday 17 September 2009

Due by 11:59 p.m. Monday 28 September

Submit your work by placing it in your "cs240" eldab directory.

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 toArray 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 [] toArray() {
        // 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() {
        // returns a string that could be printed to the terminal
          String w = Integer.toString(toArray()[0]);
          for (int i = 1; i < k; i++)
             w += " " + Integer.toString(toArray()[i]);
          return w;
        }

       public boolean isFirst() {
        // returns whether this is the first sequence of its type
          for (int i = 0; i < k; i++)
             if (toArray()[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:

Assignment 4:

Also write static methods that use your classes to give the following output:

Last modified 17 September 2009