In: Computer Science
Due 2/14 by end of day Your first assignment is to create the barebones outline of your first data structure - The ArrayList - with minimal functionality. 1) Open up your IDE and create a new Project. Call it "PDSSpring2019". 2) Inside your project create a new package. Call it "adt" 3) Inside the adt package create an interface. Call it "List" 4) You are to fully program the List interface using Java generics and Javadoc. Please note - an interface doesn't account for implementation. It is just the function definitions and Javadoc. The following functions need to be present - boolean add(E e) void add(int index, E element) boolean addAll(Collection<? extends E> c) boolean addAll(int index, Collection<? extends E> c) void clear() boolean contains(Object o) boolean containsAll(Collection<?> c) boolean equals(Object o) E get(int index) int indexOf(Object o) boolean isEmpty() int lastIndexOf(Object o) E remove(int index) boolean remove(Object o) boolean removeAll(Collection<?> c) E set(int index, E element) int size() List<E> subList(int fromIndex, int toIndex) Object[] toArray() You also need the appropriate Javadoc. To get the outline of the functions please visit the Javadoc at https://docs.oracle.com/javase/8/docs/api/java/util/List.html NOTE - It's OK to leave off the Exceptions for now because you aren't sure which Exceptions you're going to need to throw yet. 5) You need to create a new file, ArrayList.java that implements the List interface. For most of your methods you can create stubs but you will implement the following Constructors: All 3 located here https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html Methods: boolean add(E e) void add(int index, E element) int size() NOTE * You will probably need the add method to perform one of the constructors.
Solution(s):
//Interface implementation
interface List
{
boolean add(E e);
void add(int index E element);
boolean addAll(Collection<? extends E> c);
boolean addAll(int index,Collection<? extends E> c);
void clear();
boolean contains(Object o);
boolean containsAll(Collection<? extends E> c);
boolean equals(Object o);
E get (int index);
int indexOf(Object o);
boolean isEmpty();
E remove(int index);
boolean remove(Object o);
boolean removeAll(Collection<? extends E> c);
E set(int index,Element e);
int size();
List<E> sublist(int fromIndex, int toIndex);
Object[] toArray();
}
class ArrayList implements List
{
private Object[] arrayList;
private int size = 0;
//initializes empty arrayList of default size 10
public ArrayList()
{
arrayList=new Object[10];
}
//initializes empty arrayList of given size
public ArrayList(int capacity)
{
arrayList=new Object[capacity];
}
//add object to arrayList
public boolean add(E e)
{
if(arrayList.length-size <= 5){
increaseListSize();
}
arrayList[size++] = e;
return true;
}
//adds all Objects to arrayList
public boolean addAll(Collection<? extends E> c) {
//converting Collection to array
Object[] a = c.toArray();
int newlength = a.length;
increaseListSize(size + newlength);
System.arraycopy(a, 0, arrayList, size, newlength);
size += newlength;
return newlength != 0;
}
//Uses indexOf() to find the index of the object
public boolean contains(Object o)
{
//it ll return -1 if the object is not in the arrayList
if(arrayList.indexOf(o)>0)
return true;
return false;
}
//use equals() to check if all the objects are in the
arrayList
boolean containsAll(Collection<? extends E> c)
{
Object[] a = c.toArray();
int check=0;
int newlength=a.length;
if(a!=size)
return false;
for(int i=0;i<size;i++)
{
if(arrayList.equals(a[i]))
check++;
}
if(check==size)
return true;
return false;
}
boolean equals(Object o)
{
for(int i=0;i<size;i++)
{
if(arrayList[i]==o)
return true;
}
return false;
}
public Object get(int index){
if(index < size){
return arrayList[index];
} else {
return null;
}
}
int indexOf(Object o)
{
for(int i=0;i<size;i++)
{
if(arrayList[i].equals(o))
return i;
}
return -1;
}
public void clear()
{
//setting arrayList objects to null
for(int i=0;i<size;i++)
arrayList[i]=null;
size=0;
}
boolean isEmpty()
{
//returns false if size !=0
return(size==0);
}
//calls remove(int index) to delete the object
public boolean remove(Object o)
{
if(arrayList.contains(o))
{
int s=size;
int index=indexOf(o)
Object o=remove(index);
if(s>size)
return true;
}
return false;
}
public Object remove(int index){
if(index < size){
Object obj = arrayList[index];
arrayList[index] = null;
int temp = index;
//shifting the array
while(temp < size){
arrayList[temp] = arrayList[temp+1];
arrayList[temp+1] = null;
temp++;
}
size--;
return obj;
} else {
return null;
}
}
public boolean removeAll()
{
boolean removed=false;
Iterator<?> e = iterator();
while (e.hasNext()) {
if (arrayList.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}
public Object set(int index,Element e)
{
arrayList[index]=e;
return e;
}
//to control growth of arrayList
private void increaseListSize(){
arrayList = Arrays.copyOf(arrayList, arrayList.length*2);
}
int size()
{
return size;
}
List<Object> sublist(int fromIndex, int toIndex)
{
//new ArrayList
ArrayList<Object> newList=new ArrayList<>();
for(int i=fromIndex;i<toIndex;i++)
{
newList.add(arrayList[i]);
}
return newList;
}
public Object[] toArray()
{
Object[] myArray = new Object[ObjectList.size()];
ObjectList.toArray(newArray);
}
}
Hope this helps. Thanks!