21 September, 2011

Generics - Union a Set [java]

So I created my ArraySet class from an interface. I'm trying to test my union method in my SetApp class, and I truly don't know where to begin. Can someone push me into the right direction? Btw, I know my SetApp class is messy atm, but I was just testing the methods.
Code:
import java.util.Iterator;
import java.util.Random;
import java.util.Arrays;

public class ArraySet<T> implements SetADT<T> {
    private static Random generator = new Random();
    private final int DEFAULT_CAPACITY = 100;
    private T[] contents;
    private int count;

    public ArraySet(int initialCapacity) {
        this.count = 0;
        this.contents = (T[]) (new Object[initialCapacity]);
    }

    public ArraySet() {
        this.count = 0;
        this.contents = (T[]) (new Object[DEFAULT_CAPACITY]);
    }

    @Override
    public void add(T element) {
        if (!this.contains(element)) {

            if (size() == contents.length) {
                expandCapacity();
            }
            contents[count] = element;
            count++;
        }
    }

    private void expandCapacity() {
        T[] larger = (T[]) (new Object[contents.length * 2]);

        for (int index = 0; index < contents.length; index++) {
            larger[index] = contents[index];
        }
        contents = larger;
    }

    @Override
    public T removeRandom() throws EmptySetException {
        if (isEmpty()) {
            throw new EmptySetException();
        }
        int index = generator.nextInt(count);
        T element = contents[index];
        contents[index] = contents[count - 1];
        contents[count - 1] = null;
        count--;
        return element;

    }

    @Override
    public void remove(T element) {
        for (int i = 0; i < count - 1; ++i) {
            if (contents[i].equals(element)) {
                for (int j = i; j < count - 1; ++j)
        contents[j] = contents[j + 1];
                --count;
            }
        }
    }

    @Override
    public SetADT<T> union(SetADT<T> set) {
        ArraySet<T> newSet = new ArraySet<T>();

        for (int i = 0; i < this.size(); i++) {
            newSet.add(this.contents[i]);
        }

        Iterator<T> t = set.iterator();

        while (t.hasNext()) {
            newSet.add(t.next());
        }

        return newSet;
    }

    @Override
    public boolean contains(T target) {
        for (int i = 0; i < count; i++) {
            if (contents[i].equals(target)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean equals(SetADT<T> set) {
        Iterator<T> t = set.iterator();

        while (t.hasNext()) {
            if (!this.contains(t.next()))
                return false;
        }

        return true;
    }

    @Override
    public boolean isEmpty() {
        if (this.count == 0) {
            return true;
        } else {
            return false;
        }
        /*
         * More tersely return(count==0);
         */
        /*
         * or if(count==0)return true; else return false;
         */
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return count;
    }

    @Override
    public Iterator<T> iterator() {
        return Arrays.asList(this.contents).iterator();
    }

}

Code:
import java.util.Iterator;

public class SetApp {

    public static void main(String[] args) {

        ArraySet<String> as = new ArraySet<String>(3);
        ArraySet<String> asTest = new ArraySet<String>(3);

        ArraySet<String> asEmptyTest = new ArraySet<String>();

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home