import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class SetA
{
    public ArrayList<String> union(ArrayList<String> list1, ArrayList<String> list2)
    {
        ArrayList<String> unionArray = new ArrayList<String>();
        String s1,s2;
        Iterator n1=list1.iterator();
        Iterator n2=list2.iterator();
        while(n1.hasNext())
        {  s1=(String)n1.next();
            unionArray.add(s1);
        }
        while(n2.hasNext())
        {
            s2=(String)n2.next();
            if(!unionArray.contains(s2))
                unionArray.add(s2);
        }
        return unionArray;
    }

    public ArrayList<String> intersection(ArrayList<String> list1, ArrayList<String> list2)
    {
        ArrayList<String> intersectionArray = new ArrayList<String>();
        String s;
        Iterator n = list1.iterator();
        while(n.hasNext())
        {
            s=(String)n.next();
            if(list2.contains(s))
                intersectionArray.add(s);
        }
        return intersectionArray;
    }
}

public class  SetTest extends JFrame implements ActionListener
{
    SetA    set = new SetA();   // SetA
    JLabel L1, P1, P2;
    JTextField T1,T2;
    JTextArea output;

//  dinamik boyutlu degisken sinifi Vector'u kullanarak
//  iki vector listesi yarat
    ArrayList<String> list1 = new ArrayList<String>();
    ArrayList<String>list2  = new ArrayList<String>();

//  Sonuclari yine vektor cinsinden de§iskenler
//  kullanarak aktar
    ArrayList<String> ansUn;
    ArrayList<String> ansInt;
    Container c;

    public SetTest()
    {
        super("set test");
        c=getContentPane();
        c.setLayout(new FlowLayout());
        P1 = new JLabel("Enter first list : ");
        T1 = new JTextField(30);
        T1.setBackground(c.getBackground());
        P2 = new JLabel("Enter second list : ");
        T2 = new JTextField(30);
        T2.setBackground(c.getBackground());
        output = new JTextArea();
        output.setEditable(false);
        output.setText("");
        output.setBackground(c.getBackground());
        c.add(P1);
        c.add(T1);
        T1.addActionListener(this);
        c.add(P2);
        c.add(T2);
        T2.addActionListener(this);
        JScrollPane jp=new  JScrollPane(output);
        jp.setPreferredSize(new Dimension(320,200));
        c.add(jp);
    }

    public void inputA(String string, ArrayList<String> list)
    {
        StringTokenizer tokens = new StringTokenizer(string);
        while(tokens.hasMoreTokens()) {
            String test = tokens.nextToken();
            list.add(test);
        }
    }

    public void outputA(ArrayList<String> v)
    {
        Iterator enm = v.iterator();
        while(enm.hasNext())
        {
            String ans = (String)enm.next();
            output.append(ans + "\n");
        }
    }

    public void actionPerformed(ActionEvent e)
    {
         if(e.getSource()==T1)
         {
            String stringToTokenize = T1.getText();
            inputA(stringToTokenize,list1);
         }
        else if(e.getSource()==T2)
        {
            String stringToTokenize = T2.getText();
            inputA(stringToTokenize,list2);
//          calculate union and interface sets for two input set
            ansUn = set.union(list1,list2);
            ansInt = set.intersection(list1,list2);
//          show results in the output field
            output.setText("");
            output.append("Union of two set : \n");
            outputA(ansUn);
            output.append("İki listenin kesişim kümesi : \n");
            outputA(ansInt);
        }
    } //end of actionperformed method
      public static void main(String[] args)
      {
      SetTest pencere = new SetTest();
      pencere.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      pencere.setSize(400,350);
      pencere.setVisible(true);
      }

    }
