So I have this assignment where I am acting as a set. A set that holds numbers. In other words an array I am guessing. So he gave us a generic interface. Then we have to use another class and finish those methods. I'm having trouble because I've never used Generics before. My professor created some methods, but I don't know where to start with the others that aren't coded yet. So I guess I need to take what is says in the javadocs of the interface and implement it in the ArraySet class. My Professor gave me both of these. He implemented some methods, but not all. I have to finish the rest in the ArraySet class.
Here is the generic interface.
import java.util.Iterator;
/**
* Specifies an abstract data type for a completely unordered collection of
* generic objects that is not permitted to contain any duplicated elements, as
* defined by the generic object's equals method.
*
* @author Lewis & Chase ISBN 0-321-24584-9 Ch03
* @version 2011-09-15 by D.P. Daugherty
*
*/
public interface SetADT<T> {
/**
* Adds one element to the set, but only if an equal element is not already
* contained therein.
*
* @param element
* the element to be added
*/
public void add(T element);
/**
* Removes and returns a randomly selected element from the set and adjusts
* the set size accordingly
*
* @return the element which has been removed from the set
* @throws EmptySetException
*/
public T removeRandom() throws EmptySetException;
/**
* Removes the specified element if it is present in the set, otherwise
* makes no changes
*
* @param element
*/
public void remove(T element);
/**
* Combines the elements of two sets into a third new set with no duplicates
* permitted
*
* @param set
* the set to be combined with this set
* @return a new set that contains the union of the two sets being combined
*/
public SetADT<T> union(SetADT<T> set);
/**
* Checks to see if an element is already contained in the set
*
* @param element
* the element to be checked for containment
* @return true if element is contained in the set, false otherwise
*/
public boolean contains(T element);
/**
* Check to see if two sets are exactly equal, ignoring order of the
* elements
*
* @param set
* the set to be checked for equality
* @return true if two sets contain exactly the same elements
*/
public boolean equals(SetADT<T> set);
/**
* Check to see if set contains no elements
*
* @return true if set has no elements, false otherwise
*/
public boolean isEmpty();
/**
* Returns the number of distinct elements in the set
*
* @return count of distinct elements
*/
public int size();
public Iterator<T> iterator();
/**
* Returns a list of elements in the set separated by commas and enclosed in
* square brackets
*
* @return a string describing the contents of the set
*/
public String toString();
}
This is my other class:
import java.util.Iterator;
import java.util.Random;
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) {
// TODO Auto-generated method stub
}
@Override
public SetADT<T> union(SetADT<T> set) {
// TODO Auto-generated method stub
return null;
}
@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) {
// TODO Auto-generated method stub
return false;
}
@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() {
// TODO Auto-generated method stub
return null;
}
}