Complete the class ArraySet-1.java which implements the SetADT.java interface. Implement only
the methods
public Set<E> union(Set<E> set)
public Set<E> intersection(Set<E> set)
ArraySet-1.java
public class ArraySet<E> implements Set<E>, Iterable<E> {
private static Random rand = new Random();
private final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
private int count; // the current number of elements in the set
private E[] contents;
private int modCount;
public ArraySet() {
count = 0;
modCount = 0;
contents = (E[])(new Object[DEFAULT_CAPACITY]);
}
public ArraySet(int initialCapacity) {
count = 0;
contents = (E[])(new Object[initialCapacity]);
}
public boolean add(E element) {
boolean result = false;
if (!(contains(element))) {
if (size() == contents.length) {
expandCapacity();
}
contents[count] = element;
count += 1;
result = true;
}
modCount += 1;
return result;
}
public void addAll(Set<E> set) {
Iterator<E> scan = set.iterator();
while (scan.hasNext()) {
add (scan.next());
}
modCount += 1;
}
public E removeRandom() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("ArraySet");
}
int choice = rand.nextInt(count);
E result = contents[choice];
contents[choice] = contents[count-1]; // fill the gap
contents[count-1] = null;
count--;
modCount += 1;
return result;
}
public E remove(E target) throws NoSuchElementException, NoSuchElementException {
int search = NOT_FOUND;
if (isEmpty()) {
throw new NoSuchElementException("ArraySet");
}
for (int index=0; index < count && search == NOT_FOUND; index += 1) {
if (contents[index].equals(target)) {
search = index;
}
}
if (search == NOT_FOUND) {
throw new NoSuchElementException();
}
E result = contents[search];
contents[search] = contents[count-1];
contents[count-1] = null;
count--;
modCount += 1;
return result;
}
public Set<E> union(Set<E> set) {
// Implement this method
return null; // Replace when implementinging this method
}
public Set<E> difference(Set<E> set) {
ArraySet<E> diff = new ArraySet<E>();
for (int index = 0; index < count; index += 1) {
if (!(set.contains(contents[index]))) {
diff.add (contents[index]);
}
}
return diff;
}
public Set<E> intersection(Set<E> set) {
// Implement this method
return null; // Replace when implmenting this method
}
public boolean contains (E target) {
int search = NOT_FOUND;
for (int index=0; index < count && search == NOT_FOUND; index += 1) {
if (contents[index].equals(target)) {
search = index;
}
}
return (search != NOT_FOUND);
}
public boolean equals(Set<E> set) {
boolean result = false;
ArraySet<E> temp1 = new ArraySet<E>();
ArraySet<E> temp2 = new ArraySet<E>();
E element;
if (size() == set.size()) {
temp1.addAll(this);
temp2.addAll(set);
Iterator<E> scan = set.iterator();
while (scan.hasNext()) {
element = scan.next();
if (temp1.contains(element)) {
temp1.remove(element);
temp2.remove(element);
}
}
result = (temp1.isEmpty() && temp2.isEmpty());
}
return result;
}
public boolean isEmpty() {
return (count == 0).
Complete the class ArraySet1java which implements the SetA.pdf
1. Complete the class ArraySet-1.java which implements the SetADT.java interface. Implement only
the methods
public Set<E> union(Set<E> set)
public Set<E> intersection(Set<E> set)
ArraySet-1.java
public class ArraySet<E> implements Set<E>, Iterable<E> {
private static Random rand = new Random();
private final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
private int count; // the current number of elements in the set
private E[] contents;
private int modCount;
public ArraySet() {
count = 0;
modCount = 0;
contents = (E[])(new Object[DEFAULT_CAPACITY]);
}
public ArraySet(int initialCapacity) {
count = 0;
contents = (E[])(new Object[initialCapacity]);
}
public boolean add(E element) {
boolean result = false;
if (!(contains(element))) {
if (size() == contents.length) {
expandCapacity();
}
contents[count] = element;
count += 1;
result = true;
}
modCount += 1;
return result;
}
public void addAll(Set<E> set) {
Iterator<E> scan = set.iterator();
while (scan.hasNext()) {
add (scan.next());
}
modCount += 1;
}
public E removeRandom() throws NoSuchElementException {
2. if (isEmpty()) {
throw new NoSuchElementException("ArraySet");
}
int choice = rand.nextInt(count);
E result = contents[choice];
contents[choice] = contents[count-1]; // fill the gap
contents[count-1] = null;
count--;
modCount += 1;
return result;
}
public E remove(E target) throws NoSuchElementException, NoSuchElementException {
int search = NOT_FOUND;
if (isEmpty()) {
throw new NoSuchElementException("ArraySet");
}
for (int index=0; index < count && search == NOT_FOUND; index += 1) {
if (contents[index].equals(target)) {
search = index;
}
}
if (search == NOT_FOUND) {
throw new NoSuchElementException();
}
E result = contents[search];
contents[search] = contents[count-1];
contents[count-1] = null;
count--;
modCount += 1;
return result;
}
public Set<E> union(Set<E> set) {
// Implement this method
return null; // Replace when implementinging this method
}
public Set<E> difference(Set<E> set) {
ArraySet<E> diff = new ArraySet<E>();
for (int index = 0; index < count; index += 1) {
if (!(set.contains(contents[index]))) {
diff.add (contents[index]);
}
}
3. return diff;
}
public Set<E> intersection(Set<E> set) {
// Implement this method
return null; // Replace when implmenting this method
}
public boolean contains (E target) {
int search = NOT_FOUND;
for (int index=0; index < count && search == NOT_FOUND; index += 1) {
if (contents[index].equals(target)) {
search = index;
}
}
return (search != NOT_FOUND);
}
public boolean equals(Set<E> set) {
boolean result = false;
ArraySet<E> temp1 = new ArraySet<E>();
ArraySet<E> temp2 = new ArraySet<E>();
E element;
if (size() == set.size()) {
temp1.addAll(this);
temp2.addAll(set);
Iterator<E> scan = set.iterator();
while (scan.hasNext()) {
element = scan.next();
if (temp1.contains(element)) {
temp1.remove(element);
temp2.remove(element);
}
}
result = (temp1.isEmpty() && temp2.isEmpty());
}
return result;
}
public boolean isEmpty() {
return (count == 0);
}
public int size() {
return count;
}
public Iterator<E> iterator() {
4. return new ArraySetIterator();
}
public String toString() {
String result = "";
for (int index=0; index < count; index += 1) {
result = result + contents[index].toString() + "n";
}
return result;
}
private void expandCapacity() {
E[] larger = (E[])(new Object[contents.length*2]);
for (int index=0; index < contents.length; index += 1) {
larger[index] = contents[index];
}
contents = larger;
}
private class ArraySetIterator implements Iterator<E> {
int iteratorModCount;
int current;
public ArraySetIterator() {
iteratorModCount = modCount;
current = 0;
}
public boolean hasNext() throws ConcurrentModificationException {
if (iteratorModCount != modCount) {
throw new ConcurrentModificationException();
}
return (current < count);
}
public E next() throws ConcurrentModificationException {
if (!hasNext()) {
throw new NoSuchElementException();
}
current += 1;
return contents[current - 1];
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
}
5. SetADT.java
public interface SetADT<T>
{
/**
* Adds one element to this set, ignoring duplicates.
*
* @param element the element to be added to this set
* @return true if the element was successfully added
*/
public boolean add(T element);
/**
* Removes and returns a random element from this set.
*
* @return a random element from this set
*/
public T removeRandom();
/**
* Removes and returns the specified element from this set.
*
* @param element the element to be removed from this list
* @return the element just removed from this list
*/
public T remove(T element);
/**
* Returns the union of this set and the parameter
*
* @param set the set to be unioned with this set
* @return a set that is the union of this set and the parameter
*/
public SetADT<T> union(SetADT<T> set);
/**
* Returns true if this set contains the parameter
*
* @param target the element being sought in this set
* @return true if this set contains the parameter
*/
public boolean contains(T target);
/**
* Returns true if this set and the parameter contain exactly
* the same elements
*
* @param set the set to be compared with this set
6. * @return true if this set and the parameter contain exactly
* the same elements
*/
public boolean equals(SetADT<T> set);
/**
* Returns true if this set contains no elements
*
* @return true if this set contains no elements
*/
public boolean isEmpty();
/**
* Returns the number of elements in this set
*
* @return the integer number of elements in this set
*/
public int size();
/**
* Returns an iterator for the elements in this set
*
* @return an iterator for the elements in this set
*/
public Iterator<T> iterator();
/**
* Returns a string representation of this set
*
* @return a string representation of this set
*/
public String toString();
}