This website is preserved for historical and scholarly reference and is no longer actively maintained.
CpSc 210, Section 1
Assignment 5
February 19, 2002
Due: Wednesday, February 27 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 (so we shall not
allow the compareTo method to be used on items in a Set).
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
should be private instead of public.
Use the instanceof operator to test that other is a Set in the
equals method.
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 methods should work with otherSet, instead of other.
Sample results are:
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 }
Grading Rubric:
5 pts. reasonable attempt to solve problem as assigned
10 pts. also compiles
5 pts. style standards (includes clarity)
5 pts. addElement method
5 pts. contains method
5 pts. isEmpty method
5 pts. removeItem method
5 pts. size method
5 pts. toString method
5 pts. equals method
20 pts. intersection method
20 pts. union method
5 pts. elegance 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).