public class SetOfInts{

/******
    We represent here the set of integers as an array
    and we prevent duplicate elements.

 The menu has: 
  public boolean isMember(int element)
  public void    addElement(int element)
  public void    deleteElement(int element)
  public void    union(SetOfInts A)
  public void    intersect(SetOfInts A)
  public void    print()
  public int     getElement(int i)
  public int     setElement(int i, int element)
  public int     getsize()

  public void    setsize()
  public int findElement(int element)
  public void insert(int element)
  public void deleteAtIndex(int index)
******/

/* ALL DATA IS private: */

  private int[] elements; // array of integers
  private int   size; // current number of elements

  public static final int NOT_FOUND =  -1 ; // code for not found 

/* AND THERE ARE 3 implementation dependant methods used by the public methods:
  public int findElement(int element)
  public void insert(int element)
  public void deleteAtIndex(int index)
*/


  public SetOfInts(int capacity)
  {
    elements = new int[capacity];
    size = 0;
  }

  public boolean isMember(int element){
    return (findElement(element) != NOT_FOUND );
  }

  public void addElement(int element){
    // add the element to the array of elements
    // unless its already there and if the 
    // representation is not at full capacity

    if (! isMember(element))
      if (getsize() < elements.length)
        insert(element);
      else
        System.out.println("set size overflow");
  }

  public void deleteElement(int element){
    // delete the element from the 
    // array if it is there

    int index = findElement(element);
    if (index != NOT_FOUND ) deleteAtIndex(index);
  }

  public void union(SetOfInts A){
    // union the elements of this set with those of A
    for (int i = 0; i < A.getsize(); i=i+1)
      this.addElement(A.getElement(i));
  }


  public void intersect(SetOfInts A){
    // intersect  the elements of this set with those of A 
    int i = 0;
    while (i < this.getsize())
      if (A.isMember(this.getElement(i)))
        i = i+1; // increments i
      else
        this.deleteAtIndex(i); // decrements size (rechecks i)
  }

  public void print(){
    System.out.print("{ ");
    for (int i = 0; i<getsize()-1; i=i+1){
      System.out.print(getElement(i));
      System.out.print(", ");
    }
    if (getsize() > 0) System.out.print(getElement(getsize()-1));
    System.out.print(" }");
    System.out.println();
  }

  public int getElement(int i){ return elements[i]; }

  public void setElement(int i, int e){ elements[i]=e; }

  public int     getsize(){ return size;}

  public void setsize(int i){ size=i;};

// Implementation dependant methods. These can be re-implemented in the inherited class.

  public int findElement(int element){
    // returns the index of the element in the array
    // or NOT_FOUND  if its not there.

   for (int i=0; i < getsize(); i=i+1)
      if (getElement(i) == element) return i;
   return NOT_FOUND ;
  }        

  public void insert(int element){
    // the set is not at full capacity and 
    // the element is not already there
    setElement(getsize(), element);
    setsize(getsize()+1);
  }


  public void deleteAtIndex(int index){
    // deletes the item at the given index in the array
   setElement(index, getElement(getsize()-1) );
   setsize(getsize()-1);
  }

}


