CS 314 - Specification 9 - Sets

Programming Assignment 9:  Pair Assignment. You may work with one other person on this assignment using the pair programming technique. If you work with a partner, you must work with someone in the same section as you. You can complete the assignment on your own if you wish.

Review this paper on pair programming. You are not required to work in a pair on the assignment. (You may complete it by yourself if you wish.) If you begin working with one partner and do not wish to finish the assignment with that partner you must complete the assignment individually. If you work with a partner, the intent is that you work together, at the same computer. One person "drives" (does the typing and explains what they are doing) and the other person "navigates" (watches and asks questions when something is unclear). You should not partition the work, work on your own, and then put things together.

You and your partner may not acquire from any source (e.g.  another student or an internet site) a partial or complete solution to a problem or project that has been assigned. You and your partner may not show other students your solution to an assignment. You may not have another person (current student other than your partner, former student, tutor, friend, anyone) “walk you through” how to solve an assignment. You may get help from the instructional staff. You may discuss general ideas and approaches with other students. Review the class policy on collaboration from the syllabus. If you took CS314 or CS307 previously and work with a partner this semester OR worked with a partner in a previous class, then you must start from scratch on this assignment.

 The purposes of this assignment are:

  1. to practice implementing data structures
  2. to use ArrayLists and Iterators
  3. to implement sorting and searching algorithms
  4. To develop code that makes use of encapsulation, inheritance, polymorphism, interfaces, and abstract classes.

Summary: Implement three classes: AbstractSet, UnsortedSet, and SortedSet. Recall that the elements of a set have no order and duplicate items are not allowed. Link to the Wikipedia entry on Sets. For more details on how to complete the assignment see the description of classes and files below as well as the tips section.

Very Important: On this assignment you must use an ArrayList as your internal storage container in the UnsortedSet and SortedSet classes.

Very Important: On this assignment you may not use any of the methods in the Arrays or Collections classes. You are encouraged to use methods from the ArrayList class.

Very Important: For each method you write in UnsortedSet and SortedSet state the Big O of the method in a comment at the top of the method. To answer some of these questions you have to determine the Big O of methods from the ArrayList class. View the ArrayList documentation and think about how we implemented methods in our simple array-based List class to determine the Big O of methods from ArrayList. Use the class discussion group to discuss what you think the Big O of methods from ArrayList are.


  File Responsibility
Source Code ISet.java. Interface for the classes you are developing. Do not alter. Provided by me
Source Code AbstractSet.java. Complete as many methods as possible in this class without any instance variables. Provided by me and you. (Granted, mostly you.)
Source Code UnsortedSet.java A set with elements in no particular order. Provided by me and you. (Granted, mostly you.)
Source Code SortedSet.java A set with elements in sorted ascending order. Provided by me and you. (Granted, mostly you.)
Testing SetTester.java A class with various tests. You must add your own tests as specified below. Provided by me and you
Utility class Stopwatch.java. A class for calculating elapsed time when running other code. Provided by me.
Documentation ISet.html. Javadoc page for the ISet interface.
AbstractSet.html. Brief Javadoc page for the skeleton AbstractSet class.
UnsortedSet.html. Brief Javadoc page for the skeleton UnsortedSet class.
SortedSet.html. Brief Javadoc page for the skeleton SortedSet class.
Provided by me
Submission Submit a zip file named a9.zip with your four source code files: AbstractSet.java, UnsortedSet.java, SortedSet.java, and SetTester.java. Provided by you

Description of Classes and Files:

ISet: The interface for set classes. Do not alter this file.

AbstractSet: The provided source file AbstractSet.java is a skeleton file.

UnsortedSet: The provided source file UnsortedSet.java is a skeleton file. This class extends AbstractSet. The elements in this set are not maintained in any particular order from the client's point of view.  ("A client" is any other class that uses UnsortedSet.) Complete this class.

SortedSet: The provided source file SortedSet.java is a skeleton file. This class extends AbstractSet. The elements in this set are maintained in sorted order. To do this the generic data type E (data type parameter) must be a class that implements the Comparable interface. The class header for SortedSet modifies E so that we know all variables of type E in SortedSet will be data types that implement Comparable.

SetTester: The provided source file SetTester.java contains some tests for the set classes. Delete the provided tests in the version of SetTester your turn in. Add at least 1 test per method per class that implements that method to this file. (In other words if there is a method in AbstractSet and you do not override it in UnsortedSet or SortedSet, you just need to write one test for that method. On the other hand if there is a method in AbstractSet that you do override in both UnsortedSet and SortedSet you have to write code for both versions.) Use the class discussion group to share new test cases.

When writing code that performs sorts and searches you may use code from the class slides as long as you document the source with a comment. For example:

// code for binary search from class slides
... binary search code

You will not be able to use the sorts and searches from the slides "as is". You will have to adapt them to you classes. Recall your internal storage containers are ArrayLists not arrays.

WARNING: Students using Eclipse have used the following cast. This generates a warning in Eclipse. But compiles. Eclipse uses a different Java compiler the standard Java compiler. We use the standard Java compiler on the CS Linux system when grading. This code results in a syntax error with the standard Java compiler.

Therefore, ensure you do not have a cast such as this in your code.

There are many ways to work around this. If you want to be sure your code works, then compile it on the CS Linux system before turning it in. See the Transferring File and Compiling Java on the Command Line sections for information on how to actually this.

// Works (with warning) in Eclipse but not javac. DO NOT USE!
ArrayList<Comparable> newArrayList = (ArrayList<Comparable>) myCon;

Where to start: It may be difficult to know where to start with this assignment. You don't want to write all the code and then try and test it. Tracking down the errors can be difficult. You may find it useful to change AbstractSet so that it does not implement ISet. UnsortedSet still extends AbstractSet so you can create instances of UnsortedSet and test the methods you wrote from AbstractSet. Just be sure to go back and have AbstractSet implement ISet.


When your classes are completed, run the method largeTest in the SetTester class. This method has you pick a file and then adds all of the words in the file to various Sets. The method uses the SortedSet, the UnsortedSet, the Java HashSet, and Java TreeSet. The time is displayed for the operation to execute. Test this method with a small file at first to ensure it works. Then files (again Project Gutenberg is a good source as is ) of various sizes. Use at least four different files. Report your results in a comment at the top of the SetTester class. Also answer the following questions in that comment: Include the change in file size, number of words, number of distinct words, and time. Express these as a percentage compared to the previous time. (Follow the format from the slides on Maps.)

Submission: Complete the header in all four class.  Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own or with a single partner.

Create a zip file name a9.zip [case sensitive! Do not name the file A9.zip!] with your SetTester.java, AbstractSet.java, UnsortedSet.java, and SortedSet.java files. The zip file must not contain any directory structure, just the required files.

See this page for instructions on how to create a zip via Eclipse.

Turn in a9.zip via your Canvas account to programming assignment 9.

Checklist:  Did you remember to:

UnsortedSet Target Big Os:

These are the Big O's you should be able to achieve for the methods in the  UnsortedSet class. If a method is implemented in AbstractSet and it has this Big O then there is no need to override it in UnsortedSet. Assume there are already N elements in this UnsortedSet. For methods with another set assume there are N elements in that set as well.

SortedSet Target Big Os:

These are the Big O's you should be able to achieve for the methods in the  SortedSet class. If a method is implemented in AbstractSet and it has this Big O then there is no need to override it in SortedSet. Assume there are already N elements in this SortedSet. For methods that involve two sets, the calling object and another ISet, these target Big Os apply only if the other set is also a SortedSet. if it is not your target Big O is the same as in the UnsortedSet class. If there is a method where the other set is not a SortedSet you should rely on the Big O targets from UnsortedSet.

Tips: So how can you implement methods in AbstractSet when there isn't an internal storage container and no instance variables?  By using other methods in the class!

Here is an example. The ISet interface has a method named contains that determines if a given element is in the set. There is also a method named iterator which provides an Iterator object to look at all the items in the set. So in AbstractSet we could do the following:

public boolean contains(E item){
    boolean found = false;
    Iterator<E> it = this.iterator(); // get an iterator for this set.
    E temp;
    // do a linear search using the iterator object
    while(!found && it.hasNext()){
        temp = it.next();
        found = temp.equals(item); // which equals method is getting called?
    return found;

You won't be able to implement the iterator method in the AbtsractSet class. You need an internal storage container to do that. So you will be calling a method that will be implemented later. Note the use of a temporary object in the above code is actually unnecessary and the code could be streamlined to this form.

public boolean contains(E item){
    Iterator<E> it = this.iterator(); // get an iterator for this set.
    // do a linear search using the iterator object
            return true; // found it. Done!
    return false; // never found item

And there is an even simpler option. ISet extends the Java Iterable interface. This means it has a method that returns an Iterator object. It also means ISets  can be used as the set-expression in enhanced for loops. So the following works as well

public boolean contains(E obj){
    for(E val : this)
            return true;
    return false;

Use whatever form you are most comfortable with.

SortedSet tips:

In the SortedSet class when methods can be faster due to the fact that the data is sorted, you should make them faster. For example consider the contains method:

public boolean contains(E item)

This could be implemented in the AbstractSet class using an Iterator object from the iterator method. The expected run time would be O(N). However, in the SortedSet class this method should be overridden, because it can be accomplished in O(logN) time through a binary search.

There are many methods that have an explicit parameter of type ISet. If that ISet is a SortedSet we can perform some actions more efficiently. For example, the intersection method in SortedSet will look something like this:

public ISet<E> intersection(ISet<E> otherSet) {
    // we know E is a Comparable, but we don't know if
    // otherSet is a SortedSet.
    // local var of correct type so we only have to cast once
    SortedSet<E> otherSortedSet;

    // do we need to create a new SortedSet based on otherSet or is otherSet really a SortedSet?
    if( !(otherSet instanceof SortedSet<?>) )
        otherSortedSet = new SortedSet<E>(otherSet); // should be O(NlogN)
        otherSortedSet = (SortedSet<E>) otherSet;

    SortedSet<E> result = new SortedSet<E>();

    // fill result with modified merge algorithm, should be O(N)

    return result;

  Back to the CS314 homepage.