copyright Steve J. Hodges   http://steveh.net/cs20j/cs20j-hw04.html

CS20j Fall 2018

Assignment 4 (Integer Sets)

Directory Name

20j-4

 

Class Names

IntegerSet and Program4

 

Provided starter code

IntegerSet-stubs.java


Program Description

Create a class, named IntegerSet, that implements a set that will store integer values in the range of 0 through 1000, inclusive. (We are using a static const for this upper bound, so that the value can be modified, and your program re-compiled, to work with an alternate value.) If you don't remember the basics of sets and set operations, you may find the set entry at Wikipedia helpful. The required member functions are described below—do not modify them without consulting with me first.

You must use the code from the provided class definition and main function. Do not modify the provided code and class member requirements, except where indicated or to add comments, without consulting me and getting permission via email. Don't forget to add all the proper array and parameter checking. If you modify the main function or testing purposes while you are developing your program (doing additional testing is always recomended), return it to the provided code before you submit your assignment. Note that when I test your IntegerSet class, I may use another test routine. Make sure that your IntegerSet class conforms exactly to the specification.

Your default constructor must allow between zero and five elements to be inserted into the set initally. If you use varargs (which is not required) you should support any number of initial values.

Your member functions insertElement and deleteElement should work by adding (or removing) the specified element to the set. If insertion or deletion is attempted on an element that is out of range, do nothing.

The toString member function should return a string containing standard set notation (comma seperated list inside curley braces)

example sets:
{1, 2, 4, 6, 34, 58}
{1}
{}

The member function equals(IntegerSet) should have an integer set as its parameter and return type boolean. It should determine if two sets have exactly the same members.

The member function hasElement should have return type bool and determine if the parameter is an element of the set. Out of range elements will return return false.

The intersection and union functions are called intersectionOf and unionOf. Both should take two integer sets as their parameters and perform the requested operation by modifying the contents of the set on which the function was invoked.

For any function that gets an IntegerSet reference as a parameter, treat a null reference parameter the same as an empy set.

// s1, s2 and s3 are IntegerSet objects
// s1 becomes the union of s2 and s3, and NOT the union of s1, s2, and s3
// s2 and s3 are unmodified
s1.unionOf(s2,s3);

The function getMaxSetValue returns the largest value that could be in the set (not that largest value that is in the set.) I coded this function for you.

Sample Run with the provided main

[steveh@pengo CS20j]$ java Program4
CS20j Integer Sets
put your name and email here
is3 (random): {4, 10, 14, 23, 29, 44, 47, 52, 71, 72, 83, 87, 104, 129, 134, 143, 147, 170, 179, 211, 218, 241, 256, 275, 277, 308, 309, 333, 339, 348, 353, 354, 385, 394, 411, 441, 462, 463, 484, 487, 497, 501, 502, 507, 521, 527, 532, 547, 550, 598, 610, 613, 625, 637, 647, 677, 692, 713, 715, 722, 724, 731, 733, 746, 767, 771, 776, 783, 786, 804, 817, 839, 848, 850, 853, 854, 859, 866, 874, 879, 883, 899, 909, 919, 941, 945, 962, 964, 969, 972, 979, 985, 989, 991, 992, 993}
is3 (union): {1, 2, 4, 5}
is3 (intersection): {2, 5}
1: is3 == is3
2: is3 != is2
3: is1 has 5
4: is1 does not have 7
[steveh@pengo CS20j]$ 

 

What to turn in

For this assignment, you will be creating a project with several files. All of your project files should be placed in a directory named 20j-4 inside your home directory on Pengo. This directory should not contain any files not part of your project, nor should it contain any subdirectories or binaries. I will collect all of the files from this directory.