Class Set

java.lang.Object
  |
  +--Set
All Implemented Interfaces:
java.lang.Cloneable

public class Set
extends java.lang.Object
implements java.lang.Cloneable

An abstract data type meant to serve as a representation of a mathematical set, containing non-negative integers below a certain value and with methods to manipulate the set.


Field Summary
static int MAXIMUM
          The maximum valid integer element that a Set object can contain.
static int MINIMUM
          The minimum valid integer element that a Set object can contain.
 
Constructor Summary
Set()
          Constructs an instance of the Set class that is an empty set.
 
Method Summary
 void addElement(int element)
          Adds a new element to the Set.
 java.lang.Object clone()
          Generates a copy of this Set object.
static Set complement(Set s)
          Generates and returns the complement of the given Set object.
static Set difference(Set s1, Set s2)
          Generates and returns the difference of two given Set objects.
 boolean equals(java.lang.Object obj)
          Compares this Set to another object for equality.
 int getSize()
          Accessor method for getting the number of elements in the Set object.
static Set intersection(Set s1, Set s2)
          Generates and returns the intersection of two given Set objects.
 boolean isMember(int element)
          Determines if the given element is a member of this Set.
static Set[] powerset(Set s)
          Generates and returns the powerset of the given Set object.
 int setOrderComparision(Set otherSet)
          A method that compares this Set object to another to determine in which order the sets would appear lexicographically.
static Set[] sortPowerset(Set[] ps)
          Generates an array of Set objects that contains all the elements from another array of Set objects after they have been placed into a more presentable and readable order.
 java.lang.String toString()
          Returns the String representation of this Set object.
static Set union(Set s1, Set s2)
          Generates and returns the union of two given Set objects.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAXIMUM

public static final int MAXIMUM
The maximum valid integer element that a Set object can contain.

MINIMUM

public static final int MINIMUM
The minimum valid integer element that a Set object can contain.
Constructor Detail

Set

public Set()
Constructs an instance of the Set class that is an empty set.
Postcondition:
This Set has been initialized as an empty set.
Throws:
OutOfMemoryError - Indicates insufficient memory for creating this Set object.
Method Detail

addElement

public void addElement(int element)
                throws IllegalElementException
Adds a new element to the Set.
Parameters:
element - a new element to be added to this Set
Precondition:
This Set has been instantiated and element is within the valid range.
Postcondition:
If the element is already in this Set object, nothing will change. Otherwise, the element is added to the set.
Throws:
IllegalElementException - Indicates that element is outside of the permitted range.

clone

public java.lang.Object clone()
                       throws java.lang.RuntimeException
Generates a copy of this Set object.
Overrides:
clone in class java.lang.Object
Returns:
The return value is a copy of this Set. Subsequent changes to the copy will not affect the original, nor vice versa. Note that the return value must be typecast to a Set before it can be used.
Throws:
OutOfMemoryError - Indicates insufficient memory for cloning this Set object.
java.lang.RuntimeException - Indicates that the class doesn't implement the Cloneable interface. (though this should never occur)
Note:
Remember to typecast the returned Object to a Set before use.

complement

public static Set complement(Set s)
                      throws IllegalElementException
Generates and returns the complement of the given Set object.
Parameters:
s - the Set for which a complement is to be generated for
Precondition:
The Set object referenced by s has been instantiated.
Returns:
A Set containing the complement of the given Set parameter.
Throws:
IllegalElementException - Indicates that the set containes an element oustide of the valid range.
Note:
The return value is null if s is null.

difference

public static Set difference(Set s1,
                             Set s2)
                      throws IllegalElementException
Generates and returns the difference of two given Set objects.
Parameters:
s1 - the first Set
s2 - the second Set
Precondition:
The Set objects referenced by s1 and s2 have both been instantiated.
Returns:
A Set containing the difference of the given Set parameters.
Throws:
IllegalElementException - Indicates that one of the sets containes an element oustide of the valid range.
Note:
The return value is null if either s1 or s2 is null.

equals

public boolean equals(java.lang.Object obj)
Compares this Set to another object for equality.
Overrides:
equals in class java.lang.Object
Parameters:
obj - an object to which this Set is being compared with
Returns:
A return value of true indicates that obj refers to a Set object with the same elements as this Set. Otherwise, the return value is false.

getSize

public int getSize()
Accessor method for getting the number of elements in the Set object.
Returns:
The return value is the number of elements in the Set object.

intersection

public static Set intersection(Set s1,
                               Set s2)
                        throws IllegalElementException
Generates and returns the intersection of two given Set objects.
Parameters:
s1 - the first Set
s2 - the second Set
Precondition:
The Set objects referenced by s1 and s2 have both been instantiated.
Returns:
A Set containing the intersection of the given Set parameters.
Throws:
IllegalElementException - Indicates that one of the sets containes an element oustide of the valid range.
Note:
The return value is null if either s1 or s2 is null.

isMember

public boolean isMember(int element)
                 throws IllegalElementException
Determines if the given element is a member of this Set.
Parameters:
element - the element to be searched for in the Set
Precondition:
This Set has been instantiated and element is within the valid range.
Returns:
A return value of true indicates that the given element is a member of this set. Otherwise, the return value is false
Throws:
IllegalElementException - Indicates that element is outside of the permitted range.

powerset

public static Set[] powerset(Set s)
                      throws IllegalElementException
Generates and returns the powerset of the given Set object.
Parameters:
s - the Set for which a powerset is to be generated for
Precondition:
The Set object referenced by s has been instantiated.
Returns:
An array of Set objects containing sets which are elements of the powerset of the given Set parameter.
Throws:
IllegalElementException - Indicates that a set containes an element oustide of the valid range.
Note:
The return value is null if s is null.

setOrderComparision

public int setOrderComparision(Set otherSet)
A method that compares this Set object to another to determine in which order the sets would appear lexicographically.
Parameters:
otherSet - the Set object to which this one is being compared with
Returns:
A return value of -1 indicates that this Set object comes before the one it is compared to, 1 means that it comes after it, and 0 means that both sets are the same.
Throws:
NullPointerException - Indicates that otherSet is a null reference.

sortPowerset

public static Set[] sortPowerset(Set[] ps)
Generates an array of Set objects that contains all the elements from another array of Set objects after they have been placed into a more presentable and readable order.
Parameters:
ps - the array of Set objects to be sorted
Precondition:
The parameter ps is not null.
Returns:
The return value is an array of Set objects that is a sorted version of the parameter ps.
Throws:
NullPointerException - Indicates that ps is a null reference.

toString

public java.lang.String toString()
Returns the String representation of this Set object.
Overrides:
toString in class java.lang.Object
Returns:
The return value is the String representation of this Set object.

union

public static Set union(Set s1,
                        Set s2)
                 throws IllegalElementException
Generates and returns the union of two given Set objects.
Parameters:
s1 - the first Set
s2 - the second Set
Precondition:
The Set objects referenced by s1 and s2 have both been instantiated.
Returns:
A Set containing the union of the given Set parameters.
Throws:
IllegalElementException - Indicates that one of the sets containes an element oustide of the valid range.
Note:
The return value is null if either s1 or s2 is null.