class SetOfGabs{

/******
    Sets of any type of elements built over class SetElement.

  public boolean isMember(SetElement element)
  public void    addElement(SetElement  element)
  public void    deleteElement(SetElement  element)
  public void    union(SetOfG A)
  public void    intersect(SetOfG A)
  public void    print()
  public SetElement      getElement(int i)
  public void    setElement(int i, SetElement  element)
  public int     getsize()
  public void    setsize()
 
 THE FOLLOWING ARE SPECIFIED BUT NOT IMPLEMENTED
  public int findElement(SetElement  element)
  public void insert(SetElement  element)
  public void deleteAtIndex(int index)
******/

/* ALL DATA IS private: */
  private SetElement[] elements; // array of integers
  private int   size; // current number of elements

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

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

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

  public void addElement(SetElement  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(SetElement  element){
    // delete the element from the 
    // array if it is there

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

  public void union(SetOfG 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(SetOfG 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){
      getElement(i).print();
      System.out.print(", ");
    }
    if (getsize() > 0) getElement(getsize()-1).print();
    System.out.print(" }");
    System.out.println();
  }

  public SetElement  getElement(int i){ return elements[i]; }
  public void setElement(int i, SetElement  e){ elements[i]=e; }
  public int     getsize(){ return size;}
  public void setsize(int i){ size=i;};

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

  public  int findElement(SetElement  element){
    // returns the index of the element in the array
    // or NOT_FOUND  if its not there.
   System.out.println(" findElement SHOULD BE IMPLEMENTED IN THE INHERITED CLASS");
   return NOT_FOUND;
  }        

  public void insert(SetElement  element){
    // the set is not at full capacity and 
    // the element is not already there
   System.out.println(" SetElement SHOULD BE IMPLEMENTED IN THE INHERITED CLASS");
  }

  public void deleteAtIndex(int index){
    // deletes the item at the given index in the array
   System.out.println(" deleteAtIndex SHOULD BE IMPLEMENTED IN THE INHERITED CLASS");
  }
}

class SetElement {
  /* This is the class of all objects of the sets. The application needs to 
     inherit SetElement to create actual  instances . It has 3 methods that
     need be overriden:
          void print() 
          boolean equals(SetElement s)
          boolean issmaller (SetElement s)
  */
          void print() { 
                   /* WARNING:  print PRINTS toString ONLY;
                   SHOULD BE OVERRIDEN WITHIN ITS INHERITED CLASS! */ 
                   System.out.print(this.toString());};

          boolean equals(SetElement s){
               /*  WARNING: equal HERE COMPARES REFERENCES ONLY;
                   equals SHOULD BE OVERRIDEN WITHIN ITS INHERITED CLASS! */ 
               return this == s;
          };

          boolean issmaller (SetElement s){
               /*  WARNING: issmaller HERE RETURNS FALSE ALWAYS 
                   SHOULD BE OVERRIDEN WITHIN ITS INHERITED CLASS! */ 
               return false; 
          };
}

