The goal of this assignment is to practice writing classes with data members (instance variables) and method members.
In this assignment, you will write a class to model as set (in the mathmatical sense). Sets can contain any type of elements, and we will see general set classes later in the semester; but for our purposes today, we will make a set of characters. Your class will have operations to display the set as a string (using the standard notation, "{ a, b, c }"), add an element to the set, compute the union of two sets, compute the intersection of two sets, determine if a set is a subset of another, determine if two sets are equal, and compute the cardinality.
For each part of this assignment, I'm giving a pointer or two on a hints page. Try to figure it out on your own first, and turn to the hints page if you're stuck. Moreover, before you even start this project, you should sit down with the in-class example from Monday and understand it.
As in previous assignments, move into your cs241
directory.
Make a directory for this assignment, and change into it.
> cd cs241 > mkdir assign9 > cd assign9
I am giving you a driver program for this assignment and also a stub Set class te get you started. Copy them from the public directory.
> cp /homeemp/tvandrun/pub/241/SetDriver.java . > cp /homeemp/tvandrun/pub/241/Set.java
Open the driver in xemacs and take a look. All the code is commented out. The only modification you will need to make to this file is to un-comment the various parts of it as you go along to test the class you are writing. Then open the Set.java file. (You may want to have them in separte windows. To do that, go to File->Open in New Frame.)
The first thing you need to do is decide how to represent that data encapsulated in the class. Think about what the class represents, and add the appropriate instance variable(s). Then compile Set.java to make sure the syntax is right.
The first method is a constructor that receives a character.
That character is to be the initial, sole element in the class.
That is, if you called it with new Set('x')
,
the resulting set should be { x }
.
Uncomment the stub in Set.java and write the body.
Then uncomment the section that tests this in
SetDriver.java. Compile and test; the program
won't have any output yet, but make sure it runs without producing
any errors.
The next method is another constructor-- in other words, we are overloading the constructor. This time we're creating an empty set. Think about how to implement that (notice that this one is simpler than the previous one), and then uncomment out the appropriate section in the driver, compile, and test.
Now you want to be able to print out the set in
a standard notation.
The only tricky thing here is how to put the commas in
between elements. Notice that in
{ x, y, z }
there is one fewer comma than elements.
Uncomment in the driver, compile, and test. Now the output should be
{x} {}
Write the body of the method void add(char element)
.
This should take the given new element and add it to the set--that is,
the contents of the object should change. It doesn't return anything.
Uncomment, compile, and test. The driver adds a few elements and
prints the new set; the ouput should be
{x} {} {x, y, z}
Write the body of the method boolean hasElement(char element)
.
This should take a new element and return false
depending on whether or not the given character is an element.
Uncomment, compile, and test; the output should be
{x} {} {x, y, z} true false
Write the body of the method Set union(Set other)
.
This should receive a set (so, now we are dealing with two sets:
the one this method is called on and the one given as a parameter)
and create a new set (...make that now three sets we have to deal with),
which it will fill with all the elements of each of the other two
and return. Note that this does not change the set on which it is called
nor the set given as a parameter.
If an element is in both of the two original sets, it should be included
only once in the new set.
Uncomment, compile, and test; the output should be:
{x} {} {x, y, z} true false {x, y, z, w}
Write the body of the method Set intersection(Set other)
.
It should return a new sest which is the intersection of the set on which this
is called and the set given as a parameter.
Uncomment, compile, and test; the output should be:
{x} {} {x, y, z} true false {x, y, z, w} {x, y}
Write the body of the method boolean hasSubset(Set other)
.
This should return true if the given set (the one in the parameter)
is a subset of the set on which the method is called, false otherwise.
Uncomment, compile, and test; the output should be:
{x} {} {x, y, z} true false {x, y, z, w} {x, y} true false
Write the body of the method boolean equals(Set other)
.
This should return true if the two sets are equal (ie, have all the same elements).
Uncomment, compile, and test; the output should be:
{x} {} {x, y, z} true false {x, y, z, w} {x, y} true false false true
The cardinality of a set is its number of elements.
Write the body of the method int cardinality()
,
which will return the number of elements in the set.
Uncomment, compile, and test; the output should be:
{x} {} {x, y, z} true false {x, y, z, w} {x, y} true false false true 0 3 4 2
Create the script file as before (cat, rm, compile, and run)--
both Set.java
and SetDriver.java
.
(You do not need to have made any changes, including
documentation, to the driver except uncommenting the
code.)
> a2ps -P sp (the name of the script file)
(This will print "two up", meaning two pages shown next to each other
on one pice of paper. If you use a2ps
on a Java file, it
will format it nicely like in the handouts I've given in class.
The command lpr
works similarly except it does no formatting
and doesn't print two up by default.)
Then turn in the hard copy by 5:00, Wed, March 23.