public class Stiva
{
private T v[];
private int sp;
private int dim;
@SuppressWarnings("unchecked")
public Stiva()
{
this.dim = 20;
v = (T[]) new Object[dim];
sp = -1;
}
@SuppressWarnings("unchecked")
public Stiva(int dim)
{
this.dim = dim;
v = (T[]) new Object[dim];
sp = -1;
}
public boolean isEmpty()
{
return (sp == -1);
}
public boolean isFull()
{
return (sp == dim-1);
}
public void push(T val) throws StackInvalidArgumentException, StackFullException
{
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();
}
}
package pack;
@SuppressWarnings("serial")
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();
}
}
package pack;
@SuppressWarnings("serial")
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;
@SuppressWarnings("serial")
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;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
@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;
Stiva
public Frame1()
{
super("Stiva");
s = new Stiva
initializareComponente();
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(true);
setLocationRelativeTo(null);
setVisible(true);
}
private void initializareComponente()
{
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("STIVA");
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)
{
e1.printStackTrace();
}
catch (StackInvalidArgumentException e1)
{
e1.printStackTrace();
}
txtMess.setText("Valoarea " + a + " a fost introdusa in stiva!");
return;
}
if (e.getSource() == btn2)
{
try
{
b=s.pop();
}
catch (StackEmptyException e1)
{
e1.printStackTrace();
}
txtMess.setText("Valoarea extrasa = " + b);
return;
}
if (e.getSource() == btn3)
{
try
{
b=s.top();
}
catch (StackEmptyException e1)
{
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)
{
e1.printStackTrace();
}
txtMess.setText(txtMess.getText() + c + " ");
}while (!s.isEmpty());
return;
}
if (e.getSource() == btnClear)
{
txtMess.setText("");
return;
}
}
public static void main(String[] args)
{
new Frame1();
}
}
Niciun comentariu:
Trimiteți un comentariu