Rename the MyList interface to MyListInterface and modify the code to place both
ID: 3812433 • Letter: R
Question
Rename the MyList interface to MyListInterface and modify the code to place both classes in the MyList
package.
Add the following methods to MyListInterface and provide concrete implementations for each of them
in MyAbstractList.
void addhHead(E e) // Inserts element at beginning of list
void addTail(E e) // Inserts element at end of list
E removeHead(void) // Removes and returns element at beginning of list
E removeTail(void) // Removes and returns element at end of list
Since these are all abstract entities, there is no test code to write for this problem.
Source Code:
public interface MyList<E> extends java.lang.Iterable<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();
}
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]);
}
@Override /** Add a new element at the end of this list */
public void add(E e) {
add(size, e);
}
@Override /** Return true if this list contains no elements */
public boolean isEmpty() {
return size == 0;
}
@Override /** Return the number of elements in this list */
public int size() {
return size;
}
Explanation / Answer
Solution:
Changes MYListInterface with given methods.
package com.chegg.solutions
public interface MyListInterface<E> extends java.lang.Iterable<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 with Partial implemented methods:
package com.chegg.solutions;
import java.util.Iterator;
@SuppressWarnings("rawtypes")
public class MyAbstractList implements MyListInterface<Object> {
private transient Object[] elementData;
protected int size = 0; // The size of the list
/** Create a default list */
protected transient int modCount = 0;
protected MyAbstractList() {
}
/** Create a list from an array of objects */
protected MyAbstractList(Object[] objects) {
for (int i = 0; i < objects.length; i++)
add(objects[i]);
}
@Override /** Add a new element at the end of this list */
public void add(Object e) {
add(size, e);
}
@Override /** Return true if this list contains no elements */
public boolean isEmpty() {
return size == 0;
}
@Override /** Return the number of elements in this list */
public int size() {
return size;
}
@Override
public Iterator<Object> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(int index, Object e) {
// TODO Auto-generated method stub
}
@Override
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
@Override
public Object get(int index) {
// TODO Auto-generated method stub
return null;
}
/*Returns the index of the first occurrence of the specified element in this list, or -1
if this list does not contain the element. More formally, returns the lowest index i such
that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.*/
@Override
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/*This implementation first gets a list iterator that points to the end of the list (with listIterator(size())).
* Then, it iterates backwards over the list until the specified element is found, or the beginning of the list is reached.(non-Javadoc)
* @see com.chegg.solutions.MyListInterface#lastIndexOf(java.lang.Object)
*/
@Override
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;}
@Override
public boolean remove(Object o) {if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
/* return the value removed.
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
/*Removes the element at the specified position in this list. Shifts any subsequent
* elements to the left (subtracts one from their indices).(non-Javadoc)
* @see com.chegg.solutions.MyListInterface#remove(int)
*/
@Override
public Object remove(int index) {
rangeCheck(index);
modCount++;
Object oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size();
}
@Override
public Object set(int index, Object e) {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("unchecked")
Object elementData(int index) {
return (Object) elementData[index];
}
}