In: Computer Science
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up
In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList.
Requirements
You will implement the MyList ADT according to the following:
1. MyList must implement the List interface. It will look something like below:
public class MyList implements List {
... } Note: As said in the class, when implementing an interface, you have to implement each method in it. There are a lot of methods in the List interface. We are going to address this issue below.
2. The initial capacity of MyList must be 2. This is not an ideal initial capacity. A smaller initial
capacity allows us to test out our code more quickly.
3. You must implement the following methods:
@SuppressWarnings("unchecked") public MyList() {
... } public int size() {
... } public boolean isEmpty() {
... } public boolean add(E e) {
... } public void add(int index, E element) {
... } public E get(int index) {
... } public E set(int index, E element) {
... } public E remove(int index) {
... } You can choose to implement any other methods as you need – private or public. For any method you don’t need but is required by the List interface, you can stub them out like below:
public void clear() {
StackTraceElement[] stackTrace =
new Throwable().getStackTrace(); String methodName = stackTrace[0].getMethodName(); throw new UnsupportedOperationException("Method '"
+ methodName + "' not implemented!"); }
What each method will do:
void add(E item) | add item to the end of the List |
void add(int pos, E item) | add item at position pos in the List, moving the items originally in positions pos through size()-1 one place to the right to make room (error if pos is less than 0 or greater than size()) |
int size() | return the number of items in the List |
boolean isEmpty() | return true iff the List is empty |
E get(int pos) | return the item at position pos in the List (error if pos is less than 0 or greater than or equal to size()) |
E remove(int pos) | remove and return the item at position pos in the List, moving the items originally in positions pos+1 through size()-1 one place to the left to fill in the gap (error if pos is less than 0 or greater than or equal to size()) |
4. There is at least one place where you will encounter an avoidable “unchecked” compiler warning. To get a clean compilation, use: @SuppressWarnings("unchecked").
But don’t abuse it for any other places where a warning is avoidable but due to your questionable coding skills.
MyList implemented as per the specifications in question is given
below.
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyList<E> implements List<E> {
private E[] data;
private int size, capacity;
@SuppressWarnings("unchecked")
public MyList() {
capacity = 2;
size = 0;
data = (E[]) new
Object[capacity];
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
@SuppressWarnings("unchecked")
//helper method which will double the array capacity
when it is full, so more elements can be added
private void ensureSpace() {
if(size() == capacity) {
capacity *= 2;
//double the capacity
E[] temp =
(E[])new Object[capacity];
for(int i = 0; i
< size; i++) {
temp[i] = data[i];
}
data =
temp;
}
}
public boolean add(E e) {
ensureSpace();
data[size] = e;
size++;
return true;
}
public void add(int index, E element) {
if(index < 0 || index >
size())
throw new
IndexOutOfBoundsException();
ensureSpace();
//shift the elements
for(int i = size()-1; i >=
index; i--)
data[i+1] =
data[i];
data[index] = element;
size++;
}
public E get(int index) {
if(index < 0 || index >=
size())
throw new
IndexOutOfBoundsException();
return data[index];
}
public E set(int index, E element) {
if(index < 0 || index >=
size())
throw new
IndexOutOfBoundsException();
E oldVal = data[index];
data[index] = element;
return oldVal;
}
public E remove(int index) {
if(index < 0 || index >=
size())
throw new
IndexOutOfBoundsException();
E val = data[index];
for(int i = index + 1; i <
size(); i++)
data[i-1] =
data[i];
size--;
return val;
}
public boolean remove(Object o) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public boolean contains(Object o) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public Iterator<E> iterator() {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public Object[] toArray() {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public <T> T[] toArray(T[] a) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public boolean containsAll(Collection<?> c)
{
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public boolean addAll(Collection<? extends E> c)
{
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public boolean addAll(int index, Collection<?
extends E> c) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public boolean removeAll(Collection<?> c)
{
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public boolean retainAll(Collection<?> c)
{
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public void clear() {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public int indexOf(Object o) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public int lastIndexOf(Object o) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public ListIterator<E> listIterator() {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public ListIterator<E> listIterator(int index)
{
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
public List<E> subList(int fromIndex, int
toIndex) {
StackTraceElement[] stackTrace
=
new Throwable().getStackTrace();
String methodName =
stackTrace[0].getMethodName();
throw new
UnsupportedOperationException("Method '" + methodName + "' not
implemented!");
}
}