-
W. Spencer Smith authoredW. Spencer Smith authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
GenericSet.java 898 B
/**
* Author: S. Smith
* Revised: Mar 5, 2017
*
* Description: A Set ADT with elements of type T
*/
import java.util.ArrayList;
public class GenericSet<T>
{
protected ArrayList<T> s;
public final static int MAX_SIZE = 100;
public GenericSet()
{
s = new ArrayList<T>();
}
public Boolean mem(T t)
{
return s.contains(t);
}
public void add(T t)
{
//check exception for MAX_SIZE
if (s.size() == MAX_SIZE)
{
throw new FullException("Cannot add to set. Set is full.");
}
if (s.contains(t))
{
throw new MemberException("Cannot add to set. Member already exists.");
}
s.add(t);
}
public void del(T t)
{
if (s.contains(t))
{
throw new NotMemberException("Cannot delete the element. Not in set.");
}
s.remove(t);
}
public int size()
{
return s.size();
}
}