SWE/CS 332 In Class Exercise Number 13


Goal: Introduce students to understanding iteration from the perspective of abstract state.

Consider both a one-way and a two-way iterator:

public interface TwoWayIterator <E> {
	boolean hasNext();
	boolean hasPrevious();
	E next ();
	E previous ();

Poly p = ...  // 3 + 4x + 5x^3

Iterator<Poly>           itr = p.iterator();  // See Liskov for one option
TwoWayIterator<Poly> itr2way = p.someNewTwoWayIteratorMethod();
  1. Work out the behavior of these iterators for the given Poly. Note that there are several reasonable options. Hence, you'll need to pick one. In other words, you need a contract. Does this contract have any preconditions, or does it just have postconditions?

  2. Is it necessary for the Poly to be part of the abstract state? (What do you need to consider to answer this question?)

  3. What are the abstract states of these iterator at each stage of the behavior in step 1?

  4. Repeat the prior 3 steps for an iterator with a remove method:
    public interface Iterator <E> {
    	boolean hasNext ();
    	E next ();
    	void remove();
    • Is there a problem with using Poly as an example?

    • Does the List [cat, dog, mouse] work better?