This website is preserved for historical and scholarly reference and is no longer actively maintained.
CpSc 210, Section 1
Assignment 2
September 25, 2001
Due: Wednesday, October 3 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
public boolean equals ( Object other )
// Pre: other is not null
// 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.
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:
10 pts. reasonable attempt to solve problem as assigned
10 pts. also compiles
10 pts. style standards (includes clarity)
?
You will be sent (by email) an outline for this program, which contains
our testDriver. Do not change the main program sent by email except to
add to the end of the main method. The 50 pts. for the specified
testDriver will be based on the test driver sent to you (unchanged).
Thus, any changes/additions you make to the test driver must be appended
to the end of the main method. You will receive credit for every correct
answer for the specified test driver until your program terminates
(except as excluded above).
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).