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:
Sequence
objects.
Perm
objects.
Set
objects.
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.
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.
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());}
Perm
, Set
,
and Multiset
, each extending the recursive (Assignment 2)
version of Sequence
. This means
overwriting the following methods:
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?
toString
to print the new names of the twelve elements rather than
{1,...,12}.
toString
method.
Last modified 29 January 2009