This file shows two examples of running the program TestIterator. Each uses a different strategy for iterating thru the Bag. Either output is fine.

It's also possible that one or more of you will come up with yet another way to iterate thru the Bag.

I can't think of any other simple way to do it, but I can't rule out that one of you (or an AI you consult) can.
The order that the iterator goes thru the Bag's contents is irrelevant. What's important is that all items from the Bag get visited by the iterator exactly once. It's also important that using the iterator's remove method doesn't change that: every item gets visited exacly once.

Oh, and it's also important that you throw the right exceptions at the right times.

OK, there's more than one thing that's important. If you code doesn't match either of the examples below, then you might want to come see me during my office hours to see if you're still OK.

Return to assignment description

Sample Output

Version 1: iterating from front to back of the array

=== Parts 1 & 2 === Here is the bag: [This, is, the, bag] There is a "This" in the bag There is a "is" in the bag There is a "the" in the bag There is a "bag" in the bag There is nothing more in the bag Good! You noticed that change! Good! You noticed that change! === Part 3 === The bag is: [10, 20, 30, 42, 50] The first call to next returns 10 The second call to next returns 20 I will try to remove that. Good. That seems to have worked. The next item should be 50. Good! There should be two items remaining:     30     42 The bag is now: [10, 50, 30, 42] === Part 4 === The bag is [10.1, 20.2, 30.3] Testing remove before next Good! correct exception thrown Testing remove after remove Good! correct exception thrown There should be two items remaining:     30.3     20.2 The bag should now be empty: [] === Part 5 === Here is the bag: [to, boldly, go, where, no, one, has, gone, before] Removing words 'go' 'no' 'so' 'to' and 'zo' Bag is now: [before, boldly, gone, where, has, one] Retaining words 'before' 'boldly' 'go' 'sleep' Bag is now: [before, boldly] === Part 6 === Here is the bag: [1, 2, 3, 4, 5] Removing an item using it1 That seems to have worked Advancing using it1 That seemed to work -- got 5 Advancing using it2 Good! Noticed the external change Removing using it2 Good! Noticed the external change Advancing (again) using it1 That seemed to work -- got 2

Version 2: iterating from back to front of the array

=== Parts 1 & 2 === Here is the bag: [This, is, the, bag] There is a "bag" in the bag There is a "the" in the bag There is a "is" in the bag There is a "This" in the bag There is nothing more in the bag Good! You noticed that change! Good! You noticed that change! === Part 3 === The bag is: [10, 20, 30, 42, 50] The first call to next returns 50 The second call to next returns 42 I will try to remove that. Good. That seems to have worked. The next item should be 30. Good! There should be two items remaining:     20     10 The bag is now: [10, 20, 30, 50] === Part 4 === The bag is [10.1, 20.2, 30.3] Testing remove before next Good! correct exception thrown Testing remove after remove Good! correct exception thrown There should be two items remaining:     20.2     10.1 The bag should now be empty: [] === Part 5 === Here is the bag: [to, boldly, go, where, no, one, has, gone, before] Removing words 'go' 'no' 'so' 'to' and 'zo' Bag is now: [has, boldly, gone, where, before, one] Retaining words 'before' 'boldly' 'go' 'sleep' Bag is now: [before, boldly] === Part 6 === Here is the bag: [1, 2, 3, 4, 5] Removing an item using it1 That seems to have worked Advancing using it1 That seemed to work -- got 4 Advancing using it2 Good! Noticed the external change Removing using it2 Good! Noticed the external change Advancing (again) using it1 That seemed to work -- got 3

The differences

Here are the two outputs side by side, with differences noted in red. Any lines that are the same in these two versions should be the same in your output. Any lines that are different in these two versions may be different in yours.
Version #1Equal?Version #2
=== Parts 1 & 2 ======== Parts 1 & 2 ===
Here is the bag: [This, is, the, bag]==Here is the bag: [This, is, the, bag]
There is a "This" in the bag!=There is a "bag" in the bag
There is a "is" in the bag!=There is a "the" in the bag
There is a "the" in the bag!=There is a "is" in the bag
There is a "bag" in the bag!=There is a "This" in the bag
There is nothing more in the bag==There is nothing more in the bag
Good! You noticed that change!==Good! You noticed that change!
Good! You noticed that change!==Good! You noticed that change!
==
=== Part 3 ======== Part 3 ===
The bag is: [10, 20, 30, 42, 50]==The bag is: [10, 20, 30, 42, 50]
The first call to next returns 10!=The first call to next returns 50
The second call to next returns 20!=The second call to next returns 42
I will try to remove that.==I will try to remove that.
Good. That seems to have worked.==Good. That seems to have worked.
The next item should be 50.!=The next item should be 30.
Good!==Good!
There should be two items remaining:==There should be two items remaining:
    30!=    20
    42!=    10
The bag is now: [10, 50, 30, 42]!=The bag is now: [10, 20, 30, 50]
==
=== Part 4 ======== Part 4 ===
The bag is [10.1, 20.2, 30.3]==The bag is [10.1, 20.2, 30.3]
Testing remove before next==Testing remove before next
Good! correct exception thrown==Good! correct exception thrown
Testing remove after remove==Testing remove after remove
Good! correct exception thrown==Good! correct exception thrown
There should be two items remaining:==There should be two items remaining:
    30.3!=    20.2
    20.2!=    10.1
The bag should now be empty: []==The bag should now be empty: []
==
=== Part 5 ======== Part 5 ===
Here is the bag: [to, boldly, go, where, no, one, has, gone, before]==Here is the bag: [to, boldly, go, where, no, one, has, gone, before]
Removing words 'go' 'no' 'so' 'to' and 'zo'==Removing words 'go' 'no' 'so' 'to' and 'zo'
Bag is now: [before, boldly, gone, where, has, one]!=Bag is now: [has, boldly, gone, where, before, one]
Retaining words 'before' 'boldly' 'go' 'sleep'==Retaining words 'before' 'boldly' 'go' 'sleep'
Bag is now: [before, boldly]==Bag is now: [before, boldly]
==
=== Part 6 ======== Part 6 ===
Here is the bag: [1, 2, 3, 4, 5]==Here is the bag: [1, 2, 3, 4, 5]
Removing an item using it1==Removing an item using it1
That seems to have worked==That seems to have worked
Advancing using it1==Advancing using it1
That seemed to work -- got 5!=That seemed to work -- got 4
Advancing using it2==Advancing using it2
Good! Noticed the external change==Good! Noticed the external change
Removing using it2==Removing using it2
Good! Noticed the external change==Good! Noticed the external change
Advancing (again) using it1==Advancing (again) using it1
That seemed to work -- got 2!=That seemed to work -- got 3


Return to assignment description