Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the interface code: public interface SetInterface<T> { /** Gets the curr

ID: 3878867 • Letter: H

Question

Here is the interface code:

public interface SetInterface<T>

{

/** Gets the current number of entries in this set.

@return The integer number of entries currently in the set. */

public int getCurrentSize();

/** Sees whether this set is empty.

@return True if the set is empty, or false if not. */

public boolean isEmpty();

/** Adds a new entry to this set, avoiding duplicates.

@param newEntry The object to be added as a new entry.

@return True if the addition is successful, or

false if the item already is in the set. */

public boolean add(T newEntry);

/** Removes a specific entry from this set, if possible.

@param anEntry The entry to be removed.

@return True if the removal was successful, or false if not. */

public boolean remove(T anEntry);

/** Removes one unspecified entry from this set, if possible.

@return Either the removed entry, if the removal was successful,

or null. */

public T remove();

/** Removes all entries from this set. */

public void clear();

/** Tests whether this set contains a given entry.

@param anEntry The entry to locate.

@return True if the set contains anEntry, or false if not .*/

public boolean contains(T anEntry);

/** Retrieves all entries that are in this set.

@return A newly allocated array of all the entries in the set. */

public T[] toArray();

} // end SetInterface

Explanation / Answer


As mentioned by you, instead of ResizableArrayBag, have used the ArrayList class. Hope it helps.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.util.ArrayList;
public class ArraySet<T> implements SetInterface<T> {
private ArrayList<T> items;
public ArraySet()
{
items = new ArrayList<T>();
}
@Override
public int getCurrentSize() {
return items.size();
}
@Override
public boolean isEmpty() {
return items.isEmpty();
}
@Override
public boolean add(T newEntry) {
if(items.contains(newEntry)) //duplicate
return false;
else
{
items.add(newEntry);
return true;
}
}
@Override
public boolean remove(T anEntry) {
if(items.contains(anEntry))
{
items.remove(anEntry);
return true;
}
else
return false;
}
@Override
public T remove() {
if(isEmpty())
return null;
int index = (int)( Math.random() * items.size());
return items.remove(index);
}
@Override
public void clear() {
items.clear();
}
@Override
public boolean contains(T anEntry) {
return items.contains(anEntry);
}
@Override
public T[] toArray() {
T[] arr = (T[])new Object[items.size()];
for(int i = 0 ; i < items.size(); i++)
arr[i] = items.get(i);
return arr;
}
}