Question: public class BagSetWrapper { public static interface Set { public int size(); public boolean isEmpty(); public void add(E e); public boolean isMember(E e); public boolean

 public class BagSetWrapper { public static interface Set { public int

public class BagSetWrapper { public static interface Set { public int size(); public boolean isEmpty(); public void add(E e); public boolean isMember(E e); public boolean remove(E e); public void clear(); public boolean isSubset(Set S); public Set union(Set S); public Set difference(Set S);

public Set intersection(Set S);

public Object[] toArray(); public int retain(Set S); }

@SuppressWarnings("unchecked") public static class ArraySet implements Set { private E[] elements; private int currentSize; private static final int DEFAULT_SIZE = 10;

public ArraySet(int initialSize) { if (initialSize

public ArraySet() { this(DEFAULT_SIZE); }

@Override public int size() { return this.currentSize; }

@Override public boolean isEmpty() { return this.size() == 0; }

@Override public void add(E e) { if (!this.isMember(e)) { if (this.size() == this.elements.length) { reAllocate(); } this.elements[this.currentSize++] = e; }

}

private void reAllocate() { E temp[] = (E[]) new Object[2*this.size()]; for (int i=0; i

@Override public boolean isMember(E e) { for (int i=0; i

@Override public boolean remove(E e) { for (int i=0; i

}

@Override public void clear() { for (int i=0; i

@Override public boolean isSubset(Set S) { for (int i=0; i

@Override public Set union(Set S) {

Set result = new ArraySet(this.size() + S.size()); for (int i=0; i

@Override public Set difference(Set S) {

Set result = new ArraySet(); for (int i=0; i

}

@Override public Set intersection(Set S) { return this.difference(this.difference(S)); }

@Override public Object[] toArray() { @SuppressWarnings("unchecked") Object result[] = new Object[this.size()]; for (int i=0; i

} @Override public int retain(Set S) { Set result = new ArraySet();

return this.intersection(S).size();

}

} }

Consider a member method retain() of the Set ADT. This method removes from this set all the elements except for those found in another set S that is passed as parameter. The method returns the number of elements that were removed. For example, if s1 - (Bob, Ned, Ron, Jil, Kim) and S2 = {Bob, Ron, Al) than a call to S1.retain(S2) turns S1 into (Bob, Ron) and returns 3. Implement method retain for the ArraySet class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!