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.
For those who don't know, a union combines the elements of two sets into a third new set with no duplicates.
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();
}
}
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>();
String[] ar1 = new String[] { "test1", "test2", "test3" };
for (String s : ar1) {
as.add(s);
asTest.add(s);
}
System.out.println(String.format("Testing contains method: %s",
as.contains(ar1[0])));
System.out.println(String.format("Testing equals method: %s",
as.equals(asTest)));
asTest.add("dfasdfdas");
System.out.println(String.format("Added to asTest var -- Testing equals method: %s",
as.equals(asTest)));
System.out.println("Testing EmptySetException method:");
try {
asEmptyTest.removeRandom();
} catch (EmptySetException e) {
e.toString();
}
System.out.println("The size of set 1 is:" + as.size() + "\n");
System.out.println("Now I will remove an element in the set");
as.remove("test1");
System.out.println("Now the size of the set is:" + as.size());
}
}