Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago

Question

orderliness, patience and seeing a task through;

Answered: 1 week ago

Question

well defined status and roles (class distinctions);

Answered: 1 week ago