Question: Objective: Create an class (called IndexedList) that represents a [simplified] indexed list. You MUST utilize an array-based data structure, NOT a link-based data structure You
Objective: Create an class (called "IndexedList") that represents a [simplified] indexed list. You MUST utilize an array-based data structure, NOT a link-based data structure You very well could create a link-based indexed list 1. Does NOT (nor should it) implement the methods from 'ListADT'' It must work with the inherited [protected] data that was established in 'Custom_ArrayList'2. It DOES have to implement the [3] "add" methods (outlined in 'UnorderedListADT' Class 'IndexedList' DOESN'T need to have any constructors. DON'T override any methods (from 'Custom_ArrayList') You also shouldn't have to overload any methods DON'T create any more class [attribute] variables All [3] "add" methods work the way as they did with class 'UnorderedList' Tip: In fact you could copy / paste the code (with little to no tweaking of the text) from said class into 'IndexedList' Function "get" simply returns the element (in the list) at the specified index, provided that said argument:o Does NOT exceed the list's [current] size / 'rear' index If it does, throw an [IndexOutOfBounds] exception Function "replace" does TWO things: 1. Reassign the element, in the 'list' array, at the specified index, with the argument (that's in the 'T' parameter) Important: Just like with function "get", if the specified index exceeds the list's [current] size / 'rear' index, throw an [IndexOutOfBounds] exception2. Return the [old] element that was replaced See figure 2 for an example Function "indexOf" works similarly to how it does with Stringso Scan the list for the FIRST element that matches the parameter If found, return the index of where said element is (within the array), otherwise, the return result is negative 1. Note: Do not throw any exception(s) for the latter outcome. ListADT code: public interface ListADT{ public T removeFirst(); public T removeLast(); public T remove(T element); public T first(); public T last(); public boolean contains(T target); public boolean isEmpty(); public int size(); CUSTOM array list code: Indexed list extends this public abstract class Custom_ArrayList implements ListADT { private int NOT_FOUND = -1; protected T[] list; protected int rear; public Custom_ArrayList() { this(100); } public Custom_ArrayList(int capacity) { rear = 0; list = (T[]) new Object[capacity]; } public T removeFirst() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayList"); T result = list[0]; rear--; /** shift the elements */ for (int scan=0; scan < rear; scan++) list[scan] = list[scan+1]; list[rear] = null; rear++; return result; } public T removeLast() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayList"); T result; rear--; result = list[rear]; list[rear] = null; rear++; return result; } public T remove(T element) { T result; int index = find(element); if (index == NOT_FOUND) throw new RuntimeException("Element " + element + " NOT found in Custom_ArrayList."); result = list[index]; rear--; for(int scan = index; scan < rear; scan++) list[scan] = list[scan + 1]; list[rear] = null; return result; } public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayList"); return list[0]; } public T last() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayList"); return list[rear-1]; } public boolean contains(T target) { return (find(target) != NOT_FOUND); } public boolean isEmpty() { return (rear == 0); } public int size() { return rear; } private int find(T target) { if(!isEmpty()) { for(int index = 0; index < rear; index++) { if(target.equals(list[index])) return index; } } return -1; } protected void expandCapacity() { T[] newList = (T[]) new Object[list.length * 2]; for(int i = 0; i < list.length; i++) newList[i] = list[i]; list = newList; } } UnorderedListADT code: public interface UnorderedListADT extends ListADT{ public void addToFront(T element); public void addToRear(T element); public void addAfter(T element, T target);}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
