Question
instruction: Write a non-member method foundInAllSets. This method receives two parameters: setArray - an array of objects, in which each element is an instance of
instruction: Write a non-member method foundInAllSets. This method receives two parameters: setArray - an array of objects, in which each element is an instance of Set stringArray - an array of strings The method foundInAllSets returns a new Set with all the strings in the array that are found in all the sets in the set array. The method returns an empty set if none of the strings appear in all the sets. For example, if the stringArray contains: [Ron, Jil, Ken], and the setArray contains three sets: S1 = {Ken, Ron, Joe, Amy, Xi}, S2 = {Ken, Ron, Jil}, S3 = {Xi, Amy, Ken, Ron, Joe}, then a call to foundInAllSets(setArray, stringArray) will return the set R = {Ron, Ken} because Ron and Ken are found in all sets.
public class Set1Wrapper { 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(); } @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 < 1) { throw new IllegalArgumentException("Size must be at least 1."); } this.elements = (E[]) new Object[initialSize]; this.currentSize = 0; } 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 < this.size(); ++i) { temp[i] = this.elements[i]; } this.elements = temp; } @Override public boolean isMember(E e) { for (int i=0; i < this.size(); ++i) { if (this.elements[i].equals(e)) { return true; } } return false; } @Override public boolean remove(E e) { for (int i=0; i < this.size(); ++i) { if (this.elements[i].equals(e)) { this.elements[i] = this.elements[this.currentSize-1]; this.elements[this.currentSize-1] = null; this.currentSize--; return true; } } return false; } @Override public void clear() { for (int i=0; i < this.size(); ++i) { this.elements[i] = null; } this.currentSize = 0; } @Override public boolean isSubset(Set S) { for (int i=0; i < this.size(); ++i) { if (!S.isMember(this.elements[i])) { return false; } } return true; } @Override public Set union(Set S) { Set result = new ArraySet(); for (int i=0; i < this.size(); ++i) { result.add(this.elements[i]); } Object[] temp = S.toArray(); for (int i=0; i < S.size(); ++i) { if (!result.isMember((E)temp[i])) { result.add((E)temp[i]); } } return result; } @Override public Set difference(Set S) { Set result = new ArraySet(); for (int i=0; i < this.size(); ++i) { if (!S.isMember(this.elements[i])) { result.add(this.elements[i]); } } return result; } @Override public Set intersection(Set S) { return this.difference(this.difference(S)); } @Override public Object[] toArray() { Object result[] = new Object[this.size()]; for (int i=0; i < this.size(); ++i) { result[i] = this.elements[i]; } return result; } } // NON-MEMBER METHOD @SuppressWarnings("unchecked") public static Set foundInAllSets(Object[] setArray, String[] stringArray){ // ADD YOUR CODE HERE } }
11:59
Quinto Ejercicio
11:59
Enunciado de la pregunta Consider a member method keepEven for the List ADT. This method modifies the current list and only keeps the elements in the even positions (i.e., 0, 2, 4, ...). The method returns the number of elements in odd positions that were discarded. Implement this method for the ArrayList class. For example, if L = {Jim, Ned, Ron, Bob, Kim}, then L.keepEven() sets L = {Jim, Ron, Kim} and returns 2 because it removed Ned and Bob.
11:59
import java.util.Iterator; import java.util.NoSuchElementException; public class Quiz3bWrapper { public static interface List extends Iterable { public int size(); public boolean isEmpty(); public boolean isMember(E e); public int firstIndexOf(E e); public int lastIndexOf(E e); public void add(E e); public void add(E e, int position); public E get(int position); public E remove(int position); public E replace(int position, E newElement); public void clear(); public Object[] toArray(); public int keepEven(); } @SuppressWarnings("unchecked") public static class ArrayList implements List { @SuppressWarnings("hiding") private class ArrayListIterator implements Iterator { private int currentPosition; public ArrayListIterator() { super(); this.currentPosition = 0; } @Override public boolean hasNext() { return this.currentPosition < currentSize; } @Override public E next() { if (this.hasNext()) { E result = (E) elements[this.currentPosition++]; // elements is array in enclosing class return result; } else { throw new NoSuchElementException(); } } } private E[] elements; private int currentSize; private static final int DEFAULT_SIZE = 10; public ArrayList(int initialSize) { if (initialSize < 1) { throw new IllegalArgumentException("Size must be at least 1."); } this.elements = (E[]) new Object[initialSize]; this.currentSize = 0; } public ArrayList() { this(DEFAULT_SIZE); } @Override public int size() { return this.currentSize; } @Override public boolean isEmpty() { return this.size() == 0; } @Override public boolean isMember(E e) { return this.firstIndexOf(e) >= 0; } @Override public int firstIndexOf(E e) { for (int i=0; i < this.size(); ++i) { if (this.elements[i].equals(e)) { return i; } } return -1; } @Override public void add(E e) { if (this.size() == this.elements.length) { this.reAllocate(); } this.elements[this.currentSize++] = e; } private void reAllocate() { E[] temp = (E[]) new Object[2*this.size()]; for (int i=0; i < this.size(); ++i) { temp[i] = this.elements[i]; } this.elements = temp; } @Override public void add(E e, int position) { if ((position < 0) || (position > this.currentSize)){ throw new IndexOutOfBoundsException("Illegal position"); } if (position == this.currentSize) { this.add(e); } else { if (this.size() == this.elements.length) { this.reAllocate(); } for (int i=this.currentSize; i > position; --i) { this.elements[i] = this.elements[i-1]; } this.elements[position] = e; this.currentSize++; } } @Override public E get(int position) { if ((position < 0) || (position >= this.currentSize)) { throw new IndexOutOfBoundsException("Illegal position"); } return this.elements[position]; } @Override public E remove(int position) { if ((position < 0) || (position >= this.currentSize)) { throw new IndexOutOfBoundsException("Illegal position"); } E result = this.elements[position]; for (int i=position; i < this.size() - 1; ++i) { this.elements[i] = this.elements[i + 1]; } this.elements[this.currentSize-1] = null; this.currentSize--; return result; } @Override public E replace(int position, E newElement) { if ((position < 0) || (position >= this.currentSize)) { throw new IndexOutOfBoundsException("Illegal position"); } E result = this.elements[position]; this.elements[position] = newElement; return result; } @Override public void clear() { while(!this.isEmpty()) { this.remove(0); } } @Override public Object[] toArray() { Object[] result = (E[]) new Object[this.size()]; System.arraycopy(this.elements, 0, result, 0, this.size()); // for (int i=0; i < this.size(); ++i) { // result[i] = this.elements[i]; // } return result; } @Override public Iterator iterator() { return new ArrayListIterator(); } @Override public int lastIndexOf(E e) { for (int i=this.currentSize-1; i>= 0; --i) { if (this.elements[i].equals(e)) { return i; } } // not found return -1; } @Override public int keepEven() { // ADD YOUR CODE HERE } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started