Goal: Modification to IntSet data abstraction and implementation.
Basic Assignment
Assignment:
1Rewrite the data abstraction for the IntSet method insert for a sorted list.
2Modify the current implementation of IntSet to fulfill the sorted list specification in bullet 1.
3Generate a set of test inputs to validate and verify that the implementation is correct with respect to bullet1.
Submit the following:
1Provide Java source files for the new implementation of IntSet. Make sure methods include Requires; Modifies; and Effects.
2Provide the test cases in an output file and describe how they will validate and verify the correctness of the your implementation. This file must be called IntSetTester.java.
3. Provide the test case output in the IntSetTester class. Provide comments for all output that indicates which test case the output is related to from bullet
File needed: Intset.Java, IntsetClient.java, TestIntset(JUnit)
import java.util.Vector;
/** OVERVIEW: IntSets are unbounded, mutable sets
of integers. A typical IntSet is {x1,...,xn}.
*/
public class IntSet {
private Vector< Integer> els; // the rep
/** EFFECTS: Constructor. Initializes this to be
empty.
*/
public IntSet () {
els = new Vector< Integer>();
}
/** REQUIRES: v is not null.
EFFECTS: Private Constructor. Initializes this to have the same
elements as those in v.
*/
private IntSet (Vector< Integer> v) {
els = new Vector< Integer>();
for (int i = 0; i < v.size(); i++)
els.add(v.get(i));
}
/** MODIFIES: this
EFFECTS: Adds x to the elements of this.
*/
public void insert (int x) {
Integer y = x;
if (getIndex(y) < 0) els.add(y);
}
/** MODIFIES: this
EFFECTS: Removes x from this.
*/
public void remove (int x) {
int i = getIndex(x);
if (i < 0) return;
els.set(i, els.lastElement( ));
els.remove(els.size() - 1);
}
/** EFFECTS: Returns true if x is in this;
else returns false.
*/
public boolean isIn (int x) {
return getIndex(x) > 0;
}
/** EFFECTS: If x is in this returns the index
where x appears; else returns ¡ª1.
*/
private int getIndex (Integer x) {
for (int i = 0; i < els.size(); i++)
if (x.equals(els.get(i))) return i;
return -1;
}
/** EFFECTS: Returns the cardinality of this.
*/
public int size () {
return els.size();
}
/** EFFECTS: If this is empty throws
EmptyException; else returns an arbitrary
element of this.
*/
public int choose () throws EmptyException {
if (els.size() == 0)
throw new EmptyException("IntSet.choose");
return els.lastElement();
}
public Object clone () {
return new IntSet(els);
}
public String toString () {
if (els.size() == 0) return "IntSet: { }";
String s = "IntSet: {" + els.elementAt(0);
for (int i = 1; i < els.size(); i++)
s = s + ", " + els.elementAt(i);
return s + "}";
}
public boolean repOk() {
if (els == null) return false;
for (int i = 0; i < els.size(); i++) {
Integer x = els.get(i);
for (int j = i + 1; j < els.size(); j++)
if (x.equals(els.get(j))) return false;
}
return true;
}
}