We define the basic methods for MultiSets by the following Java interface.
// // public interface MultiSetI{ public int cardinality(); // number of instances in the MultiSet public int diversity(); // number of elements in the MultiSet public boolean isEmpty(); // true if cardinality is 0, false otherwise public void addInstance(Object o); // add instance of Object o to the MultiSet public void removeInstance(Object o); // remove one instance of Object o public void removeElement(Object o); // remove all instances of Object o public boolean isMember(Object o); // true if an instance of Object o is in the MultiSet public int count(Object o); // number of instances of Object o in the MultiSet public Object [] elements(); // an array of the distinct elements in the MultSet (its length is diversity) public Object [] instances(); // an array of the instances in the MultiSet (its length is cardinality) public String toString(); // returns string of form: "[" followed by // the toString of each element followed by " : " // followed by the count for that element followed by "; " // ended by "]" }
public class MultiSetB implements MultiSetI{ // inner class private class ElementNode { public Object el; public int count; //create a node for a new element public ElementNode(Object e) {el = e; count = 1; } } // encapsulated data elements private ElementNode[] theElements; private int numElements; // invariants // numElements is the number of distinct elements in the MultiSet // theElements.length >= numElements // ElementNodes occur in locations 0 to numElements-1 of the array // distinct is determined by use of equals method // constructors // the following constructor creates an empty MultiSetB with // array space allocated to hold 4 nodes public MultiSetB() { } // The following constructor acts as a "copy constructor", // creating a MultiSetB with same contents as given argument. // One can not assume the other is a MultiSetB object. public MultiSetB(MultiSetI other) { } // methods implementing the MultiSetI interface }
Note: The method for addInstance expands theElements array when necessary,
making it
twice as long (e.g. if 4 elements in length, it becomes 8 elements long).
You will hand in printouts of file MultiSetB.java.
Your comments should argue for correctness of your methods.
In
your comments, indicate the Big-O complexity of each method
in terms of cardinality c and diversity d .>
For
example, a method may have complexity O(cd) .
Code:60; comments: 20.
You will hand in the code for the test driver in TestMultiSet.java, as well as the output of the tests.