Skip to content

Latest commit

 

History

History
48 lines (35 loc) · 1.81 KB

a6e7.md

File metadata and controls

48 lines (35 loc) · 1.81 KB

Back to questions

a6e7: Int set iterators

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.

Step 1

Create a IntSetIterator interface, with the following methods:

public boolean hasNext();

public int next();

Step 2

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.

Step 3

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.

Step 4

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);

Step 5

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.