Here are the instructions for the week 8 CSC148H lab. To earn your lab mark, you must actively participate in the lab. As always, you will work as a pair, taking turns being the driver and the navigator. The driver types, and the navigator watches for mistakes, thinking ahead. At the end of each exercise, show your TA what you've done, and then switch roles.
This week's exercises are on the topic of iterators.
We don't want to focus this lab on the proper use of generic type parameters, even though the classes you use and write here are a natural example where generics would be useful. You'll find a comment on this in one of the classes, and you might like to consider how to do this -- or even, if you're reasonably familiar with generics, work through revising all the code on your own.
Iterator
When it's your first turn as driver, begin
by making a new folder "lab8
".
In lab8
, save the files that we've provided for you:
LinkedNode.java
,
LinkedList.java
and
Driver.java
.
LinkedNode
and LinkedList
.
Notice that the iterator()
method in the class
LinkedList
is not implemented yet;
you will change this later.
main()
method in class
Driver
, following the directions given in the comments
in main()
.
Iterator
interfaceLinkedIterator
to implement the
Iterator
interface for the LinkedList
class.
Iterator
to refresh your memory.
import java.util.*;
at the top of your file
(or you won't be able to use Iterator
).
remove()
;
that is, write a body for the method that only throws a new
UnsupportedOperationException
.
(See the iterator()
method in class LinkedList
for an example.)
removeLast()
method in class LinkedList
for an example
of throwing a
NoSuchElementException
when next()
is called
with no list items left to return.
LinkedIterator
must keep track of in order
to be able to
traverse the elements in a LinkedList
one by one.
Discuss this with your partner and make sure that you agree
on the details.
LinkedIterator
will always be
created from inside an instance of LinkedList
:
think carefully about the information that must be given to the
LinkedIterator
by the LinkedList
and write an appropriate constructor for class
LinkedIterator
.
iterator()
in class
LinkedList
.
(Remove the line "throw ...
".)
Iterator
interface.
You do not need a large number of cases.
Iterator
LinkedList
in the reverse direction
(from the last element to the first).
LinkedList
class,
and adding code to the main()
method to test
your implementation.