If you succeeded at the bulk of question 85bb
then should do something similar for your IntSet
interface and implementing classes
of question 8a61. This will allow us to explore some very cool features of abstract classes.
Create a IntSetIterator
interface, with the following methods:
public boolean hasNext();
public int next();
Create an iterator class corresponding to each of MemoryEfficientIntSet
and SpeedEfficientIntSet
, and implement
the iterator
method in MemoryEfficient``IntSet
and SpeedEfficientIntSet
. If you implemented
the integer set classes by simply wrapping the Set<Integer>
class, then your iterator classes should simply be wrappers for the
Iterator<Integer>
object that can be obtained from a Set<Integer>
by invoking its iterator()
method.
Create an abstract class AbstractIntSet
which implements IntSet
, and change MemoryEfficientIntSet
and SpeedEfficientIntSet
to extend this abstract class. Use your iterator interface to implement a suitable toString
method in
AbstractIntSet
.
Add the following additional methods to the IntSet
interface:
// Add to the set each element in 'other'
public void addAll(IntSet other);
// Remove from the set each element in 'other'
public void removeAll(IntSet other);
// Return true iff the set contains every element of 'other'
public boolean contains(IntSet other);
Implement each of these methods in AbstractIntSet
. You should be able to implement the
methods using IntSetIterator
, with no reference to the specific type of sets
(memory or speed efficient) that are being used. Very cool, huh?
Once again, this illustrates the immense power of abstract classes and interfaces.