CS 307Assignment, Sets (a.k.a. The Mother of All Assignments.)

Programming Assignment 7: This is a pair assignment. You may work with one other person on this assignment using the pair programming technique. Review this paper on pair programming. You are not required to work in a pair on the assignment. 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 work together, at the same computer, on the assignment. 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 may not acquire, from any source (e.g., another student or student pair or an internet site), a partial or complete solution to a problem or project that has been assigned. You may not show another student or student pair your solution to an assignment. You may not have another person (current student, former student, tutor, friend, anyone) “walk you through” how to solve an assignment. Review the class policy on collaboration from the syllabus.

If you work with a partner you will turn in one version of your code. Pick only one account to submit the code to.

If you are working with a partner and want to use slip days you must both have the required number of slip days and both students use slip days. If the assignment is turned in 1 day late each student in a pair must have at least 1 slip day and it costs each student in the pair 1 slip day.

Description: 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, and polymorphism.

In this assignment you will be implementing three classes. AbstractSet, UnsortedSet, and SortedSet. Recall that the elements in a set has 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 listserv to discuss what you think the Big O of methods from ArrayList are.

You may ignore any warnings about type safety in your code. When grading you will not lose points if you have warnings regarding type safety.

Provided files:

  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.  You will complete this class. Provided by me and you. (Granted, mostly you.)
Source Code SortedSet.java You will complete this class. 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 A7.jar This file shall contain your versions of AbstractSet.java, UnsortedSet.java, SortedSet.java, and SetTester.java Provided by you

Description of Classes and Files:

ISet: The provided source file ISet.java contains the interface. Do not edit it.

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

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

SortedSet: The provided source file SortedSet.java is a skeleton file. This is a class that extends AbstractSet. The elements in this set are maintained in sorted order. To do this the data type that E (data type parameter) holds 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. I intend for all of the tests are correct for this assignment. 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.) You do not have to test the constructors. Use the class listserv to share new test cases.

When writing code that performs sorts and searches you may use code from the book or slides, except other pairs of students in the class, as long as you document the source with a comment. For example:

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


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 a real problem. 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.


Experiments:

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) of various sizes. Report your results in a comment at the top of the SetTester class. Also answer the following questions in that comment:


Submission: When finished turn in a jar file named A8.jar that contains your AbstractSet.java, UnsortedSet.java, SortedSet.java, and SetTester.java, classes using the turnin program. jar is a program included in the standard edition of Java loaded on the computers in the Microlab and that you have on your computer if you downloaded Java. See my page on creating jars in eclipse, Sun's page for using jar, my tips for using jar, and / or the guide to creating jars in Eclipse.

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 or it is a SortedSet that contains elements of a type different than those in this 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 or any instance variables and without making references to the sub classes? By using other methods in the class.

Here is an example. The ISet interface has a contains method 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(Object item){
    boolean found = false;
    Iterator it = this.iterator(); // get an iterator for this set.
    Object 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(Object item){
    boolean found = false;
    Iterator it = this.iterator(); // get an iterator for this set.
    // do a linear search using the iterator object
    while( !found && it.hasNext() ){
        found = item.equals( it.next() ); // which equals method is getting called?
    }
    return found;
}

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(Object obj){
    for(Object val : this)
        if( val.equals(obj ) )
            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(Object 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(log N) time through a binary search.

  Back to the CS 307 homepage.