I need help in creating a RPN calculator! I'm currently stuck in making a stack. here is my codes so for.
/**
*
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
public class JavaCalculator extends JFrame{
private JFrame f;
private JPanel p1,p2;
private JTextField jtfMain;
private JButton jbtnOn, jbtnMRC, jbtnMminus, jbtnMadd, jbtnDivide, jbtnOff, jbtnSeven, jbtnEight, jbtnNine, jbtnMultiply,
jbtnSqrt, jbtnFour, jbtnFive, jbtnSix, jbtnMinus, jbtnSq, jbtnOne, jbtnTwo, jbtnThree, jbtnPlus, jbtn1x, jbtnZero, jbtnDecimal,
jbtnEqual, jbtnEnter;
public JavaCalculator() {
setSize(400,190);
setTitle("Reverse Polish Notation Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
jtfMain = new JTextField("");
jbtnOn = new JButton("ON/AC");
jbtnMRC = new JButton("MRC");
jbtnMminus = new JButton("M-");
jbtnMadd = new JButton("M+");
jbtnDivide = new JButton("/");
jbtnOff = new JButton("OFF");
jbtnSeven = new JButton("7");
jbtnEight = new JButton("8");
jbtnNine = new JButton("9");
jbtnMultiply = new JButton("X");
jbtnSqrt = new JButton("SQRT");
jbtnFour = new JButton("4");
jbtnFive = new JButton("5");
jbtnSix = new JButton("6");
jbtnMinus = new JButton("-");
jbtnSq = new JButton("SQ");
jbtnOne = new JButton("1");
jbtnTwo = new JButton("2");
jbtnThree = new JButton("3");
jbtnPlus = new JButton("+");
jbtn1x = new JButton("1/x");
jbtnZero = new JButton("0");
jbtnDecimal = new JButton(".");
jbtnEqual = new JButton("=");
jbtnEnter = new JButton("ENTER");
p1 = new JPanel();
p1.setLayout(new GridLayout(1,1));
p2 = new JPanel();
p2.setLayout(new GridLayout(5,5));
p1.add(jtfMain);
p2.add(jbtnOn);
p2.add(jbtnMRC);
p2.add(jbtnMadd);
p2.add(jbtnMminus);
p2.add(jbtnDivide);
p2.add(jbtnSeven);
p2.add(jbtnEight);
p2.add(jbtnNine);
p2.add(jbtnMultiply);
p2.add(jbtnSqrt);
p2.add(jbtnFour);
p2.add(jbtnFive);
p2.add(jbtnSix);
p2.add(jbtnMinus);
p2.add(jbtnSq);
p2.add(jbtnOne);
p2.add(jbtnTwo);
p2.add(jbtnThree);
p2.add(jbtnPlus);
p2.add(jbtn1x);
p2.add(jbtnZero);
p2.add(jbtnDecimal);
p2.add(jbtnEqual);
p2.add(jbtnEnter);
p2.add(jbtnOff);
jbtnOn.addActionListener(new ButtonHandler());
jbtnMRC.addActionListener(new ButtonHandler());
jbtnMminus.addActionListener(new ButtonHandler());
jbtnDivide.addActionListener(new ButtonHandler());
jbtnOff.addActionListener(new ButtonHandler());
jbtnSeven.addActionListener(new ButtonHandler());
jbtnEight.addActionListener(new ButtonHandler());
jbtnNine.addActionListener(new ButtonHandler());
jbtnMultiply.addActionListener(new ButtonHandler());
jbtnSqrt.addActionListener(new ButtonHandler());
jbtnFour.addActionListener(new ButtonHandler());
jbtnFive.addActionListener(new ButtonHandler());
jbtnSix.addActionListener(new ButtonHandler());
jbtnMinus.addActionListener(new ButtonHandler());
jbtnSq.addActionListener(new ButtonHandler());
jbtnOne.addActionListener(new ButtonHandler());
jbtnTwo.addActionListener(new ButtonHandler());
jbtnThree.addActionListener(new ButtonHandler());
jbtnPlus.addActionListener(new ButtonHandler());
jbtn1x.addActionListener(new ButtonHandler());
jbtnZero.addActionListener(new ButtonHandler());
jbtnDecimal.addActionListener(new ButtonHandler());
jbtnEqual.addActionListener(new ButtonHandler());
jbtnEnter.addActionListener(new ButtonHandler());
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
setVisible(true);
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()== jbtnOn)
{
jtfMain.setText("");
}
else if(evt.getSource()== jbtnOne)
{
Stack.push(1);
jtfMain.setText(jtfMain.getText() + "1");
}
else if(evt.getSource()== jbtnTwo)
{
Stack.push(2);
jtfMain.setText(jtfMain.getText() + "2");
}
else if(evt.getSource()== jbtnThree)
{
Stack.push(3);
jtfMain.setText(jtfMain.getText() + "3");
}
else if(evt.getSource()== jbtnFour)
{
Stack.push(4);
jtfMain.setText(jtfMain.getText() + "4");
}
else if(evt.getSource()== jbtnFive)
{
Stack.push(5);
jtfMain.setText(jtfMain.getText() + "5");
}
else if(evt.getSource()== jbtnSix)
{
Stack.push(6);
jtfMain.setText(jtfMain.getText() + "6");
}
else if(evt.getSource()== jbtnSeven)
{
Stack.push(7);
jtfMain.setText(jtfMain.getText() + "7");
}
else if(evt.getSource()== jbtnEight)
{
Stack.push(8);
jtfMain.setText(jtfMain.getText() + "8");
}
else if(evt.getSource()== jbtnNine)
{
Stack.push(9);
jtfMain.setText(jtfMain.getText() + "9");
}
else if(evt.getSource()== jbtnZero)
{
Stack.push(0);
jtfMain.setText(jtfMain.getText() + "0");
}
else if(evt.getSource()== jbtnDivide)
{
Stack.push(/);
jtfMain.setText(jtfMain.getText() + "/");
}
else if(evt.getSource()== jbtnMultiply)
{
Stack.push(X);
jtfMain.setText(jtfMain.getText() + "X");
}
else if(evt.getSource()== jbtnPlus)
{
Stack.push(+);
jtfMain.setText(jtfMain.getText() + "+");
}
else if(evt.getSource()== jbtnMinus)
{
Stack.push(-);
jtfMain.setText(jtfMain.getText() + "-");
}
else if(evt.getSource() == jbtnEnter)
{
Stack.push();
jtfMain.setText(jtfMain.getText() + " ");
}
}
}
public static void main(String[] args) {
JavaCalculator main = new JavaCalculator();
}
}