CpSc 210, Section 1
                             Assignment 4 
                           February 17, 2003 


Due:  Wednesday, February 26,  at 11:59 p.m.  

Assignment:  Implementing a class Set


Total point value of assignment:  100 points
Not accepted late.

This assignment is to be done INDIVIDUALLY.

Assignment:  Design and implement a class, Set, which must be stored in a
     file named Set.java.  (Failure to name file or class correctly is an
     automatic 20 point deduction.)  Mathematically, a set is a collection
     of objects without duplicates and without order.  Since not all
     Objects can be compared using the compareTo operator, you should
     not use the compareTo method.  Instead, you may assume that any
     Object you will store in a Set has an equals method. 

     The Set is to be implemented using a Vector as instance variable
     to hold the elements of the Set.  The following methods are required.

     public Set ( )
     // Default constructor, constructs an empty Set

     public void addItem ( Object item )
     // Pre:  item is not null
     // Post:  If item is not in this Set, item is added to this Set.
     //        If item is already in this Set, this Set remains unchanged.

     public boolean contains ( Object item )
     // Pre:  item is not null
     // Post:  returns true if item is in Set.  Otherwise, returns false.

     public boolean isEmpty ( )
     // Post:  returns true if this Set is empty.  Otherwise, returns false.

     public void removeItem ( Object item )
     // Pre:  item is in this Set
     // Post:  item has been removed from this Set

     public int size ( )
     // Post:  returns the number of items in this Set

     public String toString ( )
     // Post:  returns String representation of Set in the form
     //        { elt1, elt2, elt3, ... }   
     //       That is, enclose elements between braces and separate with commas

     public boolean equals ( Object other )
     // Pre:  other is not null and other is a Set
     // Post:  returns true if this and other contain the same collection
     //        of Objects.  Otherwise, returns false.

     public Set union ( Set other )
     // Pre:  other is a Set and is not null (other may be empty)
     // Post:  returns the union of Sets this and other

     public Set intersection ( Set other )
     // Pre:  other is a Set and is not null (other may be empty)
     // Post:  returns the intersection of Sets this and other

     You may include any additional methods you need.  Additional methods
     must be private instead of public.

     Note that the parameter to the equals method is type Object, not type
     Set.  Use the instanceof operator to test that other is a Set. 

         if ( ! ( other instanceof Set ) )  throw new IllegalArgumentException
            ("Argument to equals method must be a set.");            

	You should follow that test with a statement that casts other to a Set,
	such as

	     Set otherSet = (Set) other;

	Then, your code should work with otherSet, instead of other.         
            

 	Sample results are (for Integers):
	Let A = { 1, 2, 3, 4 }, let B = { 5, 6, 7 }, and let C = { 4, 5 },
         the size of Set A is 4, the size of Set B is 3, and the size of C is 2
         A union B is { 1, 2, 3, 4, 5, 6, 7 }
         A union C is { 1, 2, 3, 4, 5 }
         A intersect B is the empty set
         A intersect C is { 4 }

	Sample results are:
	Let A = {"cat", "dog"}, let B = {"dog", "bird"}, let C = { "cat" }
	     A union B is { "cat", "dog", "bird" }
         A union C is { "cat", "dog" }, so A union C == A
         A intersect B is { "dog" }
         A intersect C is { "cat" }
         B intersect C is the empty set, { }

    Sample results:
    Let A = { 'a', 'b' } and let B = { 'b', 'a' }.
         A union B is { 'a', 'b' } or { 'b', 'a' }, since A and B are equal.
         A intersect B is also either { 'a', 'b' } or { 'b', 'a' }.

	The contents of the set can be any Object type.

Additional constraints:

     The ONLY Vector methods that you may use are those found on
     pages 303-304 of Java Elements (Appendix D.7) and those used
     in the Java Elements text.  You may assume that all classes
     have an "equals" method.  Caution:  The equals method for
     Vectors is NOT what you want to test if Sets are equal.

     You may want to check the Java API or experiment with the
     Vector methods in Appendix D.7, to be sure how they work.

     Consider sets of items such as Color (Appendix D.1),
     Vectors, Lines or rectangles -- classes that have no implied
     ordering.  

Grading Rubric:

	 5 pts. reasonable attempt to solve problem as assigned
	 5 pts. also compiles
	 5 pts. style standards  (see link below)

         5 pts. addElement method
         5 pts. contains method
         5 pts. isEmpty method
         5 pts. removeItem method
         5 pts. size method
         5 pts. toString method
        15 pts. equals method
        15 pts. intersection method
        15 pts. union method

        10 pts. elegance and clarity of code
     



Other requirements: The first operation of your main program must be to print "CPSC 210", your name, the assignment number, and a brief description of the assignment.

Follow the style standards to be found on your instructor's web page for CPSC 210. Submit your program using the handin command. There is a link to the handin description on the CPSC 210 lab page (www.cs.clemson.edu/~lab210).