import java.util.*;
import java.awt.*;
import java.applet.Applet;

class SetA
{
    public Vector union(Vector list1, Vector list2)
    {
        Vector Unon = new Vector();
        String ans1, ans2;
        Enumeration enum1=list1.elements();
        Enumeration enum2=list2.elements();

        while(enum1.hasMoreElements())
        {
          ans1=(String)enum1.nextElement();
          Unon.addElement(ans1);
        }

        while(enum2.hasMoreElements())
        {
            ans2=(String)enum2.nextElement();
            if(Unon.contains(ans2))
               continue;
            else
              Unon.addElement(ans2);
        }

        return Unon;
    }

    public Vector union(Vector list1, Vector list2, Vector list3)
    {
        Vector Unon = new Vector();
        String ans1, ans2, ans3;
        Enumeration enum1=list1.elements();
        Enumeration enum2=list2.elements();
        Enumeration enum3=list3.elements();

        while(enum1.hasMoreElements())
        {
          ans1=(String)enum1.nextElement();
          Unon.addElement(ans1);
        }

        while(enum2.hasMoreElements())
        {
            ans2=(String)enum2.nextElement();
            if(Unon.contains(ans2))
               continue;
            else
              Unon.addElement(ans2);
        }

        while(enum3.hasMoreElements())
        {
            ans3=(String)enum3.nextElement();
            if(Unon.contains(ans3))
               continue;
            else
               Unon.addElement(ans3);
        }

        return Unon;
    }

    public Vector intersection(Vector list1, Vector list2)
    {
        Vector Inter=new Vector();
        String ans;
        Enumeration enum=list1.elements();
        while(enum.hasMoreElements())
        {
            ans=(String)enum.nextElement();
            if(list2.contains(ans))
               Inter.addElement(ans);
        }
        return Inter;
    }


    public Vector intersection(Vector list1, Vector list2, Vector list3)
    {
        Vector Inter=new Vector();
        String ans;
        Enumeration enum=list1.elements();
        while(enum.hasMoreElements())
        {
            ans=(String)enum.nextElement();
            if(list2.contains(ans) && list3.contains(ans))
               Inter.addElement(ans);
        }
        return Inter;
    }

    public Vector substract(Vector list1, Vector list2)
    {
        Vector subst=new Vector();
        String ans;
        Enumeration enum= list1.elements();
        while(enum.hasMoreElements())
        {
            ans=(String)enum.nextElement();
            if(list2.contains(ans))
               continue;
            else
               subst.addElement(ans);

        }
    return subst;
    }

    public Vector UnionMinusIntersection(Vector list1, Vector list2)
    {
        Vector UMI=new Vector();
        String ans1, ans2;
        Enumeration enum1= list1.elements();
        Enumeration enum2= list2.elements();

        while(enum1.hasMoreElements())
        {
            ans1=(String)enum1.nextElement();
            if(list2.contains(ans1))
               continue;
            else
               UMI.addElement(ans1);
        }
        while(enum2.hasMoreElements())
        {
          ans2 = (String)enum2.nextElement();
          if(list1.contains(ans2))
             continue;
          else
             UMI.addElement(ans2);
        }
    return UMI;
}}

public class setTest2 extends Applet {

    SetA    set = new SetA();   // Reference to SetA
    Label label1, prompt1, prompt2, prompt3;
    TextField str1,str2,str3;
    TextArea output;            //similar to TextField

// Use the dynamic array Vector to store list elements
    Vector list1 = new Vector();
    Vector list2 = new Vector();
    Vector list3 = new Vector();

// Declare references for results
    Vector ansUn;
    Vector ansUn1;
    Vector ansInt;
    Vector ansInt1;
    Vector ansSub;
    Vector ansUMI;

    public void init() {
        prompt1 = new Label("Enter first list: ");
        str1 = new TextField(50);
        prompt2 = new Label("Enter second list: ");
        str2 = new TextField(50);
        prompt3 = new Label("Enter third list: ");
        str3 = new TextField(50);

//Define output region for displaying all the results
        output = new TextArea(10,40);
        output.setEditable(false);
        output.setText("");

        add(prompt1);
        add(str1);
        add(prompt2);
        add(str2);
        add(prompt3);
        add(str3);
        add(output);

    }

    public void InputA(String string, Vector list) {
        StringTokenizer tokens = new StringTokenizer(string);
        while(tokens.hasMoreTokens()) {
            String test = tokens.nextToken();
            list.addElement(test);
        }
    }

    public void OutputA(Vector v) {
        Enumeration enum = v.elements();
        while(enum.hasMoreElements()) {
            String ans = (String)enum.nextElement();
            output.appendText(ans + "\n");
        }
    }

    public boolean action(Event e, Object o) {

         if(e.target==str1){
            String stringToTokenize = o.toString();
            InputA(stringToTokenize,list1);

        }

          if(e.target==str2) {

            String stringToTokenize = o.toString();
            InputA(stringToTokenize,list2);
        }

        if(e.target==str3) {

            String stringToTokenize = o.toString();
            InputA(stringToTokenize,list3);





// Forming the union and the intersection of the two lists.

            ansUn = set.union(list1,list2);
            ansUn1 = set.union(list1,list2, list3);
            ansInt = set.intersection(list1,list2);
            ansInt1= set.intersection(list1,list2,list3);
            ansSub = set.substract(list1,list2);
            ansUMI = set.UnionMinusIntersection(list1, list2);

 // outputting all the results
            output.appendText("\nElements of the union of the two lists: \n");
            OutputA(ansUn);
            output.appendText("\nElements of the union of the three lists: \n");
            OutputA(ansUn1);
            output.appendText("\nElements of the intersection of the two lists: \n");
            OutputA(ansInt);
            output.appendText("\nElements of the intersection of the three lists: \n");
            OutputA(ansInt1);
            output.appendText("\nElements of the Substraction of the two lists: \n");
            OutputA(ansSub);
            output.appendText("\nElements of the UnionMinusIntersecton of the two lists: \n");
            OutputA(ansUMI);

        }


            return true;
    }


}
