ICS 211 Assignment 5
Iterators and Linked List removal
This assignment requires you to modify the existing code for Linked Lists. Specifically, you
must take the existing code for Linked Lists, and make it implement the
iterator method of the
java.lang.Iterable<E>
interface.
In implementing your iterator, you may make use of the code in LinkedListIterator. However,
that code does not provide an implementation for the remove
method, so you must provide that implementation.
You must also add to the Linked List class
the two remove methods specified by the
List
interface, and implement them correctly. You also need to provide
a size method, as specified by the
List interface.
You are welcome to adapt the remove code from the book.
Finally, you must provide a driver program (in a separate class) that
gives the user the possibility to exercise each of the methods of both the
LinkedList and the iterator classes. This could be similar to your
address book class in assignment 4. Your driver program should create
a linked list of strings, and allow operations on that linked list.
Specifically, the user must be able to:
- add a string at the end of the list: boolean add(E value)
- add a string at an arbitrary position in the list:
void add(int index, E value)
- remove a string at an arbitrary position in the list:
E remove(int index)
- remove a given string from the list:
boolean remove(Object o)
- create an iterator
- call hasNext, next, and remove on the
iterator, showing the results of each call
Your driver program should correctly handle any exceptions thrown
by any of these methods, print an appropriate message, and continue
operation.
Sample interaction
If using prompts and commands, your program could behave as follows.
The parts your program prints are in this font, the user input
is in this font.
Starting program, list is empty.
enter one of: add, remove, iterator, quit. add
enter string to add. Hello World
enter position for string (-1 to add at end). 99
IndexOutOfBoundsException when adding string "Hello World" at position 99.
The list is empty.
enter one of: add, remove, iterator, quit. add
enter string to add. Foo
enter position for string (-1 to add at end). -1
add("Foo") returned true
The list has one element.
enter one of: add, remove, iterator, quit. iterator
created an iterator
enter one of: hasNext, next, remove, quit. hasNext
hasNext() returned true
enter one of: hasNext, next, remove, quit. next
next() returned "Foo"
enter one of: hasNext, next, remove, quit. remove
item removed
enter one of: hasNext, next, remove, quit. remove
IllegalStateException when calling remove
enter one of: hasNext, next, remove, quit. hasNext
hasNext() returned false
enter one of: hasNext, next, remove, quit. quit
The list is empty.
enter one of: add, remove, iterator, quit. remove
enter one of: position, object. object
enter string to remove. Foo
remove("Foo") returned false
The list is empty.
enter one of: add, remove, iterator, quit. quit
quit, discarding contents of linked list.
Turning in the Assignment
Email all the java source code for your assignment to the TA. Your source
code must include all the parts of each class, even if the instructor
or the authors of the textbook originally wrote some of the code.