marți, 5 iunie 2012

Stiva generica

package pack;



public class Stack <T> {

    private T[] v;
    private int dim;
    private int sp=-1;
   
    public Stack(int dim)
    {
        this.dim=dim;
        //v=new int[dim];
        v=(T[])new Object[dim];
    }
   
    public Stack()
    {
        this(20);
    }
   
    public boolean isEmpty()
    {
       
        return sp==-1;
    }
   
    public boolean isFull()
    {
       
        return sp==dim-1;
    }
   
    public void push (T val) throws StackFullException, StackInvalidArgumentException
    {
        if (isFull())
        {
            throw new StackFullException();
            }
        else
        {
            if (val == null)
            {
                throw new StackInvalidArgumentException();
            }
       
               v[++sp]=val;
    }
    }
   
    public T pop() throws StackEmptyException
    {
        if (isEmpty())
        {
            throw new StackEmptyException();
        }
        else
        {
            return v[sp--];
        }   
    }
   
    public T top() throws StackEmptyException
    {
        if (isEmpty())
        {
            throw new StackEmptyException();
        }
        else
        {
            return v[sp];
        }
    }
    public static void main(String[] args)
    throws Exception
    {
        new Frame1();
    }

}
.......clasa exceptii....
package pack;

public class StackEmptyException extends Exception{
     public String toString()
     {
         return "Stiva este goala: "+super.toString();
     }
     public String  getMessage()
     {
            return "Stiva e goala"+super.getMessage();
       
     }
   
}
........
package pack;


public class StackFullException extends Exception {

     public String toString()
     {
         return "Stiva este plina: "+super.toString();
     }
     public String  getMessage()
     {
            return "Stiva e plina"+super.getMessage();
       
     }
   
   
   
}
...........
package pack;


public class StackInvalidArgumentException extends Exception{

   
     public String toString()
     {
         return "Stiva este goala: null "+super.toString();
     }
     public String  getMessage()
     {
            return "Stiva e goala: null"+super.getMessage();
       
     }
   
   
}
..............frame....
package pack;

import java.awt.*;
import java.awt.event.*;
import java.math.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Frame1 extends JFrame implements ActionListener {
   
    private JButton btn1;
    private JButton btn2;
    private JButton btn3;
    private JButton btn4;
    private JButton btnClear;
    private JTextField txtMess;
   
   
    Stack <String> s=new Stack <String> ();

   
    public Frame1() {
        super("Stiva");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
        initComponents();
        pack();
   
        setResizable(true);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void initComponents() {
//        setLayout(new FlowLayout());
        setLayout(new BorderLayout());
       
        JPanel pnlWest = new JPanel(new FlowLayout(FlowLayout.RIGHT));
       
        btn1 = new JButton("PUSH");
        btn1.addActionListener(this);
        pnlWest.add(btn1);
       
        btn2 = new JButton("POP");
        btn2.addActionListener(this);
        pnlWest.add(btn2);
       
       
        btn3=new JButton("TOP");
        btn3.addActionListener(this);
        pnlWest.add(btn3);
       
        btn4=new JButton("Stack");
        btn4.addActionListener(this);
        pnlWest.add(btn4);
       
        btnClear = new JButton("CLEAR");
        btnClear.addActionListener(this);
        pnlWest.add(btnClear);
       
        add(pnlWest, BorderLayout.SOUTH);
       
        txtMess = new JTextField();
        add(txtMess);
        txtMess.setBackground(Color.white);
           
    }
   
    public void actionPerformed(ActionEvent e) {
        String a,b = null,c = null;
       
        if (e.getSource() == btn1) {
            a=txtMess.getText();
            try {
                s.push(a);
            } catch (StackFullException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (StackInvalidArgumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            txtMess.setText("Valoarea "+a+" a fost introdusa in stiva!!");
            return;
        }
       
        if (e.getSource() == btn2) {
            try {
                b=s.pop();
            } catch (StackEmptyException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            txtMess.setText("Valoarea extrasa=  "+b);
            return;
        }
       
        if (e.getSource() == btn3) {
            try {
                b=s.top();
            } catch (StackEmptyException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            txtMess.setText("Valoarea din varf=  "+b);
            return;
        }
       
        if (e.getSource() == btn4) {
            txtMess.setText("Elementele stivei: ");
            do{
                try {
                    c=s.pop();
                } catch (StackEmptyException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
               
                txtMess.setText(txtMess.getText()+c+"  ");
               
            }while (!s.isEmpty());
               
            return;   
    }
       
        if (e.getSource() == btnClear) {
            txtMess.setText("");
            return;
        }
    }   
}

Niciun comentariu:

Trimiteți un comentariu