Due Date:
Friday, February 9
File(s) to be submitted:
ArrayBag.java
Sample Output:
SampleOutput.html
Starter Files:
Summary
Complete the definitions of the X-All methods (addAll
,containsAll
,removeAll
,retainAll
) in the ArrayBag class.Test your implementations using the A04 program provided.
OK, I have a confession to make: my sample solution doesn't doremoveAll
the way it's supposed to be done. Sorry!I have made a modification to the code in A04.java that will test whether you did
removeAll
the way I told you to do it, or the way it's actually supposed to be done. It then runs the last test using the appropriate return value for which way you did it.No one will be penalized for doing it the way I said. But anyone who figures out the correct way to do it will get bonus points.
So long as you do everything else correctly, you won't see a FAIL message either way.
The sample implementation of ArrayBag
is missing definitions for five methods.
(Actually, there are definitions,
but each one throws an UnsupportedOperationException.)
You are to complete the definitions for four of them
(skip iterator
).
For each of these four methods,
the return value is boolean
.
For three of them,
(other than containsAll
)
the method should return true
if the Bag's contents get changed,
and false
otherwise.
The easiest way to do that
is to make a note of how many elements were in the Bag when you started,
and compare it to the number of items in the Bag when you're done.
The addAll
and removeAll
methods are probably easiest.
You just have to go thru the given Collection
(element by element)
and add/remove each of them to/from the Bag.
(You may have to cast the element to the correct type.)
Don't forget to return the correct value.
The method containsAll
should not be hard.
Again,
go thru the given Collection
element by element
and check to see if it's in the Bag.
If it's not,
you can return false
right away.
On the other hand,
if you get all the way thru the Collection,
then every element must have been in this Bag,
so you can return true
.
The method retainAll
is probably the trickiest.
For this method you're supposed to remove all the elements
that aren't in the given Collection.
That means you need to run thru the contents
array,
checking whether the element is in the Collection.
If it's not,
you need to remove it from the array.
Going thru the array from front to back won't work!
Items get moved around in the array when you remove something.
For instance,
if your contents
array contains [A, B, C]
and the Container you're given has just [B],
then:
contents
array now contains [C, B]
instead of the [B] it's supposed to contain.
So you'll need to figure out a different way to go thru the array.
It's OK to use any of the other methods defined in ArrayBag, and also to use Iterators on the Collections you're given.
Do not modify any parts of ArrayBag other than the four methods I'm asking you to implement! You can, however, add more private methods to ArrayBag if you want.