/** * RecursiveList - A modification of Problem 20.10 (pg 836) from "An * Introduction to Programming and Object Oriented Design Using Java Version * 5.0" by Nino and Hosch. * * @author Kurt Mammen * @version Winter 2007 */ public class RecursiveList { ///////////////////////////////////////////////////////////////////////////// // A private internal interface. // // Do not modify in any way! // // Note that this interface is slightly different from the text's problem // 20.10. Specifically, there is no add(E element) and the return type of // the set-method is different. // // You will need to complete the implementation for two private inner classes // below that implement the methods of this interface. // // Be sure to read the javadocs for the RecursiveList methods below to // learn how these methods should behave. // private interface ListState { boolean isEmpty(); int size(); ListState add(int index, E element); E get(int index); E set (int index, E element); int indexOf(E element); ListState remove (int index); } ///////////////////////////////////////////////////////////////////////////// // TODO: Implement this class as follows: // // * With no instance variables // * With no explicit constructors. // * Only the methods of the ListState interface. // private class EmptyList implements ListState { // Your code here... } ///////////////////////////////////////////////////////////////////////////// // TODO: Implement this class as follows: // // * With only the instance variables already specified. // * With no explicit constructors. // * Only the methods of the ListState interface. // private class NonEmptyList implements ListState { public E element; // Called "head" in text's description.(20.10) public ListState list; // Called "tail" in text's description (20.10) // Your code here... } /**************************************************************************** * The RecursiveList implementation. * * DO NOT MODIFY CODE BELOW IN ANY WAY! * * But, rather, implement the private inner classes EmptyList and * NonEmptyList, above, so that these methods work as described. */ // The one and only instance variable for RecursiveList // Do not add any others! private ListState listState = new EmptyList(); /** DO NOT MODIFY! * Used to determine if the list is empty or not * @return true if empty, otherwise false */ public boolean isEmpty() { return listState.isEmpty(); } /** DO NOT MODIFY! * Used to access the number of elements in the list. Please note that * this is, in fact, an O(n) operation as specified in this * assignment. * @return The number of elements in the list. */ public int size() { return listState.size(); } /** DO NOT MODIFY! * Adds the specified element to list in the specified location. The element * at that position, if any, is not overwritten. This method may be used to * add elements to the end of the list by specifying an index equal to the * list's current size. * @param index The zero-based index of the position to add the element. * @param element The element to add to the list. * @throws IndexOutOfBoundsException if the index is not valid. */ public void add(int index, E element) { listState = listState.add(index, element); } /** DO NOT MODIFY! * Used to access the specified element of the list. * @param index The zero-based index of the desired element. * @throws IndexOutOfBoundsException if the index is not valid. * @return The requested element */ public E get(int index) { return listState.get(index); } /** DO NOT MODIFY! * Used to modify the element at the specified index. * @param index The zero-based index of the element to modify. * @param element The new element * @throws IndexOutOfBoundsException if the index is not valid. * @return The old element at the specified index. */ public E set (int index, E element) { return listState.set(index, element); } /** DO NOT MODIFY! * Used to obtain the zero-based index of the first element that is equal * to the specified element using its equals(Object) method. * @param element The element to search for in the list. * @throws java.util.NoSuchElementException() if there is not element in * the list that is equal to the specified element. * @return The zero-based index of the first element equal to the * specfied element using its equals(Object) method. */ public int indexOf(E element) { return listState.indexOf(element); } /** DO NOT MODIFY! * Used to remove the element at the specified postion in the list. * @param index The zero-based index of the element to remove from the * list. * @throws IndexOutOfBoundsException if the index is not valid. */ public void remove (int index) { listState = listState.remove(index); } }