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

MyList.java: public interface MyList<E> { /** Add a new element at the end of th

ID: 3621777 • Letter: M

Question

MyList.java:
public interface MyList<E> {
/** Add a new element at the end of this list */
public void add(E e);

/** Add a new element at the specified index in this list */
public void add(int index, E e);

/** Clear the list */
public void clear();

/** Return true if this list contains the element */
public boolean contains(E e);

/** Return the element from this list at the specified index */
public E get(int index);

/** Return the index of the first matching element in this list.
* Return -1 if no match. */
public int indexOf(E e);

/** Return true if this list contains no elements */
public boolean isEmpty();

/** Return the index of the last matching element in this list
* Return -1 if no match. */
public int lastIndexOf(E e);

/** Remove the first occurrence of the element o from this list.
* Shift any subsequent elements to the left.
* Return true if the element is removed. */
public boolean remove(E e);

/** Remove the element at the specified position in this list
* Shift any subsequent elements to the left.
* Return the element that was removed from the list. */
public E remove(int index);

/** Replace the element at the specified position in this list
* with the specified element and returns the new set. */
public Object set(int index, E e);

/** Return the number of elements in this list */
public int size();
}

MyAbstractList.java:

public abstract class MyAbstractList<E> implements MyList<E> {
protected int size = 0; // The size of the list

/** Create a default list */
protected MyAbstractList() {
}

/** Create a list from an array of objects */
protected MyAbstractList(E[] objects) {
for (int i = 0; i < objects.length; i++)
add(objects[i]);
}

/** Add a new element at the end of this list */
public void add(E e) {
add(size, e);
}

/** Return true if this list contains no elements */
public boolean isEmpty() {
return size == 0;
}

/** Return the number of elements in this list */
public int size() {
return size;
}

/** Remove the first occurrence of the element o from this list.
* Shift any subsequent elements to the left.
* Return true if the element is removed. */
public boolean remove(E e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
}
else
return false;
}
}

Add and implement the follwoing methods in MyList and implement them in MyAbstractList:

/** Adds the elements in otherList to this list.
* Returns true if this list changed as a result of the call *?
public boolean addAll(MyList<E> otherList)

/** Removes all the elements in otherList from this list
* Returns true if this list changed as a result of the call */
public boolean removeAll(MyList<E> otherList)

/** Retains the elements in this list that are also in otherList
* Returns true if this list changed as a result of the call */
public boolean retainAll(MyList<E> otherList)

Write a test program that creates two MyArrayLists, list1 and list2, with the initial values {"Tom", "George", "Peter", "Jean", "Jane"} and {"Tom", "George", "Michael", "Michelle", "Daniel"}, then invokes list1.addAll(list2), list1.removeAll(list2), and list1.retainAll(list2) and displays the resulting new list1.

Explanation / Answer

The methods in MyAbstractList: /** Adds the elements in otherList to this list. * Returns true if this list changed as a result of the call */ public boolean addAll(MyList otherList) { for (int i=0; i