## CSci 2101 Problem set 4: Java classes, sorting

### Due Friday, April 1 at 11:59pm (by e-mail)

#### Problem 1 (30 points)

Problem 14 on p. 239 specifies methods and behavior of a
set. Implement and test a class Set<E> with the following differences
from the one described in the book:

- The class must be generic
`Set<E>`

, where E is
the type of the elements in the set. For instance, you would be able
to create `Set<String>`

or `Set<Integer>`

. All methods that take or
return elements should take or return objects of type E.
- The class should use
`ArrayList`

, and not an array, to
store the elements.
- Instead of the method
`createSet`

just write a
constructor that creates an empty set, i.e. you should be able to
create a set of strings as `new Set<String>()`

- Other methods are:
`isEmpty()`

that returns true if this set is empty and
false otherwise,
`size()`

that returns the size of the set (the number
of elements)
`add(E element)`

adds an element to a set if it's not
already present.
`contains(E element)`

returns true if an element is present in
the set and false otherwise.
`union(Set<E> otherSet)`

returns a
new `Set<E>`

that is the
union of this set and the other set. Make sure you follow definition
of set union.
`intersection(Set<E> otherSet)`

returns a
new `Set<E>`

that is the
intersection of this set and the other set. Make sure you follow definition
of set intersection.
`removeAll()`

removes all elements from the set.

Recall that a set contains every element at most once, so don't add
duplicated elements to a set. That's especially important for union
and intersection operations.

Write tests for your methods before you implement the methods. Add
them as you are adding new methods.

Use test-first development for this problem: write **JUnit** tests first, then implement the feature,
then write a JUnit test for the next feature, etc.
Submit your Set class and your JUnit class. The quality of your testing
will be a part of
the grade.

#### Problem 2 (20 points)

Use the same setup that we have used for Insertion sort and Quicksort to implement
Comb sort. Comb sort sorts
the array by choosing a gap and sorting subsets of elements at the gap distance using bubble sort.
Then the gap gets reduced, and the process is repeated. It stops when the gap is reduced to 1, at
which point just the regular bubble sort is performed.

The rationale behind this approach is to quickly eliminate small values (known as "turtles") near the end
of the array since they slow down the bubble sort.

#### How to submit

Submit your Java file(s) to me by e-mail. The subject must be
**Problem Set N**, where N is the problem set number. If working
in a group, CC your group partner.

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.