| CS 261 | University of Puget Sound | Spring, 2020 |
|
Computer Science II
|
||
|
Abstract Data Types and their Implementations,
Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency |
||
Warning:
This course is under development.
Although the basic structure of this course is largely established,
nothing on this Web site should be considered official or even possibly
correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.
The first part of this lab extends the ArrayList class developed in previous lab on ArrayLists, and the second part of the lab adds an iterator to the SchoolDirectory class from the earlier lab on Generalization
An Iterator is an object that allows a program to move sequentially through elements in a collection. When the collection has an order (e.g., item at index 0, item at index 1, etc.), the Iterator normally is expected to maintain the order.
In implementing an Iterator, one normally does not copy the full structure. Rather, the Iterator maintains a reference to the structure through some internal field. Also, the Iterator likely maintains a few private fields (usually just one or two fields) to keep track of where processing within the structure is taking place.
Since an Iterator moves sequentially through a structure, troubles could arise if the structure changed during processing. Thus, an Iterator normally provides a method remove to change the current item in the structure. However, any other changes to the structure typically yield an exception. Also, note that remove for the Iterator can only be called once for each call to next.
Using your ArrayList class from the previous lab on ArrayLists, implement an Iterator method for MyArrayList. See examples of iterators in MyArray1.java and MyArray2.java. (The second example may be more relevant here.)
The lab on Generalization
included class definitions for each entry of a schoolDirectory,
including Person,
Student, and
and Faculty. Further, class
SchoolDirectory
provided an implementation for a collection which maintained entries
in an array, ordered by name (last name and first name).
Implement an iterator within SchoolDirectory.java that will
return successively those entries in the directory for which the
firstName contains less than 6 characters and the lastName
contains more than 6 characters.
Expand both SchoolDirectory.java and the iterator,
to implement a remove method (with parameters
first and last names).
remove method for
SchoolDirectory.java should simply delete the
given entry from the underlying entryArray,
closing up any empty locations in the array.
remove method for the iterator also should
delete the given entry from the from the
underlying entryArray and close up empty
locations. Following the Java 9 specification for this
iterator method, remove "Removes from the underlying
collection the last element returned by this iterator
(optional operation). This method can be called only once
per call to next(). The behavior of an iterator is
unspecified if the underlying collection is modified while
the iteration is in progress in any way other than by
calling this method."
See the
Java 9 API for additional details.
Notes:
The Person class may be modified to include getters
for firstName, lastName,
and eMail, but no further changes are allowed. The
classes, Student and Faculty, may not
be changed in any way in this problem.
The implementation of the iterator must use O(1) storage. It may
reference the underlying entryArray, but it may not copy the
relevant entries to another data structure.
|
created for ArrayList class 30 March 2012 revised 3 April 2012 iterator material added 30 January 2020 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |