Tuesday, November 17, 2009

Homework 9: Sets

For this homework you will implement a simple generic Set class. That is, you will implement a class Set<T> using Java's ArrayList as the data member that holds the values.

A set is defined as a collection of elements (in our case, all of the same type) such that every element appears at most once in the collection. The methods your Set<T> must implement are:

  1. Set() the constructor takes no arguments and creates an empty set.
  2. String toString() returns a string with all the elements of this set in printed form, as usual.
  3. void add(T x) adds element x to the set. If x is already in the set then it does nothing.
  4. boolean contains(T x) returns true if the set contains the element x, false otherwise.
  5. void add(Set<T> other) adds the complete contents of the set other to this set.
  6. void subtract(Set<T> other) removes every element of other that is in this set.
  7. boolean isSubset(Set<T> other) returns true if and only if this set is a subset of other, that is, if all the elements in this set are also contained in other.

You will also implement some test cases in your main() to ensure that your class works correctly. This homework is due on Tuesday, November 24 @noon.

No comments: