Due Date:
Thursday, March 19
File(s) to be submitted:
LinkedBag.java
Sample Output:
SampleOutput.html
Starter Files:
Create an Iterator for the LinkedBag class that implements theremove()method.Ensure that the Iterator returned by LinkedBag's
iteratormethod throws all the proper exceptions:
- NoSuchElementException if
next()is called when there is no next element.- IllegalStateException if
remove()is called twice in a row.- IllegalStateException if
remove()is called beforenext().- ConcurrentModificationException if the Bag has had any elements added or removed (unless they've been removed by this Iterator).
Other than activating method calls in TestInterface, all changes are to be made in the file
LinkedBag.java. If you make changes anywhere else your code will not work with the automated testing system.This assignment is essentially the same as A07, except
- It's with LinkedBag instead of ArrayBag.
- The ConcurrentModificationException code is required.
Revise LinkedBag
by implementing the iterator method.
Use a private class for the iterator.
Also implement the next and hasNext methods
in your private iterator class.
Write the next method
so that it throws a NoSuchElementException
if all of there are no more items to return.
Once you have completed the implementation, your output should look like this:
It's fine if the words come out in a different order.
remove method to your iterator.
Activate the testActivity2 method call
in TestIterator
and run the program again to ensure that you get this behaviour.
In addition to the output shown for Activity 2,
you should now see:
It's fine if the numbers are returned in a different order. What's important is that the number removed is the number returned by the second call to next.
remove
throws an IllegalStateException
whenever it's called at a bad time.
It's only OK to call remove
when next has been called
since the previous (if any) call to remove.
Activate the call to testActivity3 in TestIterator
and run it.
You should see the following additional output:
It is OK if the deleted number is different, so long as it's the same set of numbers (the "missing" number plus those remaining in the bag), and the bag ends up empty.
removeAll
and
retainAll
methods
in LinkedBag
using your private class.
When you're done,
activate the call to testActivity4.
In addition to what you saw above,
you should now see:
In this activity the actual words matter, but the order of those words doesn't.
next and remove
throw ConcurrentModificationExceptions
when they notice a change to the LinkedBag
that this Iterator didn't make.
The way to do this is to keep track of how many additions and removals get made to the LinkedBag.
First, add an instance variable to LinkedBag for the number of updates made. It should start at zero, and get bumped up by one every time an addition or removal is made.
Find the places in LinkedBag
where numInBag gets changed.
Those are the places where the number of updates
needs to be incremented.
Second, the private class needs to make a note of how many updates the LinkedBag had when it (the private class) was created. Create an instance variable for that. This is how many updates the iterator expects to see.
Every time the private class's remove method
actually removes an element from the LinkedBag,
it needs to increment its expected number of updates.
(The LinkedBag will notice that it removed an item
and increment its expected count of updates.
If the private class doesn't also increment,
the expected and actual numbers will come apart
even tho' the update was made by this iterator.)
Lastly,
before next and remove
do anything else,
they need to check whether the actual number of updates
(as recorded in the LinkedBag)
and the expected number of updates
(as recorded in the private class)
are equal.
If they are not equal,
then the method needs to throw
a ConcurrentModificationException.
Since both methods need to do the same check
and respond in the same way,
it's best to create a private method
to do the check and throw the exception as appropriate.
Activate the testActivity5 call in TestIterator
and check the output.
In addition to what you saw before,
you should see new output for Activity 5:
Once again, the particular order doesn't matter, but the pattern of results does.