## CSci 2101 Problem set 4.

### Due Friday March 21 at 11:59pm

You may work individually or in pairs.

#### Problem 1 (25 points)

Problem 14 on p. 239 specifies methods and behavior of a set. Implement and test a class Set<E> with the following differences from the one described in the book:

• The class must be generic `Set<E>`, where E is the type of the elements in the set. For instance, you would be able to create `Set<String>` or `Set<Integer>`. All methods that take or return elements should take or return objects of type E.
• The class should use `ArrayList`, and not an array, to store the elements.
• Instead of the method `createSet` just write a constructor that creates an empty set, i.e. you should be able to create a set of strings as `new Set<String>()`
• Other methods are:
• `isEmpty()` that returns true if this set is empty and false otherwise,
• `size()` that returns the size of the set (the number of elements)
• `add(E element)` adds an element to a set if it's not already present.
• `contains(E element)` returns true if an element is present in the set and false otherwise.
• `union(Set<E> otherSet)` returns a new `Set<E>` that is the union of this set and the other set. Make sure you follow definition of set union.
• `intersection(Set<E> otherSet)` returns a new `Set<E>` that is the intersection of this set and the other set. Make sure you follow definition of set intersection.
• `removeAll()` removes all elements from the set.
Recall that a set contains every element at most once, so don't add duplicated elements to a set. That's especially important for union and intersection operations.
Write tests for your methods before you implement the methods. Add them as you are adding new methods.