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:
Set()
the constructor takes no arguments and creates an empty set.String toString()
returns a string with all the elements of this set in printed form, as usual.void add(T x)
adds elementx
to the set. Ifx
is already in the set then it does nothing.boolean contains(T x)
returns true if the set contains the elementx
, false otherwise.void add(Set<T> other)
adds the complete contents of the setother
to this set.void subtract(Set<T> other)
removes every element ofother
that is in this set.boolean isSubset(Set<T> other)
returns true if and only if this set is a subset ofother
, that is, if all the elements in this set are also contained inother
.
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:
Post a Comment