Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement a more efficient add method for ListArrayBasedPlus. Define a subclass of ListArrayBasedPlus that adds more functionality and test it with the appropriate test driver.
Implement a more efficient add method for ListArrayBasedPlus.
Define a subclass of ListArrayBasedPlus that adds more functionality and test it with the appropriate test driver.
public interface ListInterface { boolean isEmpty(); int size(); void add(int index, Object item) throws ListIndexOutOfBoundsException; Object get(int index) throws ListIndexOutOfBoundsException; void remove(int index) throws ListIndexOutOfBoundsException; void removeAll(); String toString(); } // end ListInterface
public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 3; protected Object []items; // an array of list items protected int numItems; // number of items in list public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size public void removeAll() { // Creates a new array; marks old array for // garbage collection. items = new Object[MAX_LIST]; numItems = 0; } // end removeAll public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (numItems > MAX_LIST) { throw new ListException("ListException on add"); } // end if if (index >= 0 && index <= numItems) { // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == numItems+1) for (int pos = numItems-1; pos >= index; pos--) //textbook code modified to eliminate logic error causing ArrayIndexOutOfBoundsException { items[pos+1] = items[pos]; } // end for // insert new item items[index] = item; numItems++; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on add"); } // end if } //end add public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 0 && index < numItems) { return items[index]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end if } // end get public void remove(int index) throws ListIndexOutOfBoundsException { if (index >= 0 && index < numItems) { // delete item by shifting all items at // positions > index toward the beginning of the list // (no shift if index == size) for (int pos = index+1; pos < numItems; pos++) //textbook code modified to eliminate logic error causing ArrayIndexOutOfBoundsException { items[pos-1] = items[pos]; } // end for numItems--; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } // end if } //end remove }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started