I need help in creating a RPN calculator! I'm currently stuck in making a stack. here is my codes so for.This is my first time I am using stack.

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

public class JavaCalculator extends JFrame{
	Stack s = new Stack();

	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)
		{
			s.push("1");
			jtfMain.setText(jtfMain.getText() + "1");

		}
		else if(evt.getSource()== jbtnTwo)
		{
			s.push("2");
			jtfMain.setText(jtfMain.getText() + "2");

		}
		else if(evt.getSource()== jbtnThree)
		{
			s.push("3");
			jtfMain.setText(jtfMain.getText() + "3");

		}
		else if(evt.getSource()== jbtnFour)
		{
			s.push("4");
			jtfMain.setText(jtfMain.getText() + "4");

		}
		else if(evt.getSource()== jbtnFive)
		{
			s.push("5");
			jtfMain.setText(jtfMain.getText() + "5");

		}
		else if(evt.getSource()== jbtnSix)
		{
			s.push("6");
			jtfMain.setText(jtfMain.getText() + "6");

		}
		else if(evt.getSource()== jbtnSeven)
		{
			s.push("7");
			jtfMain.setText(jtfMain.getText() + "7");

		}
		else if(evt.getSource()== jbtnEight)
		{
			s.push("8");
			jtfMain.setText(jtfMain.getText() + "8");

		}
		else if(evt.getSource()== jbtnNine)
		{
			s.push("9");
			jtfMain.setText(jtfMain.getText() + "9");

		}
		else if(evt.getSource()== jbtnZero)
		{
			s.push("0");
			jtfMain.setText(jtfMain.getText() + "0");

		}
		else if(evt.getSource()== jbtnDivide)
		{
			s.push("/");
			jtfMain.setText(jtfMain.getText() + "/");

		}
		else if(evt.getSource()== jbtnMultiply)
		{
			s.push("X");
			jtfMain.setText(jtfMain.getText() + "X");

		}
		else if(evt.getSource()== jbtnPlus)
		{
			s.push("+");
			jtfMain.setText(jtfMain.getText() + "+");


		}
		else if(evt.getSource()== jbtnMinus)
		{
			s.push("-");
			jtfMain.setText(jtfMain.getText() + "-");


		}
		else if(evt.getSource() == jbtnEnter)
		{
			s.push(" ");
			jtfMain.setText(jtfMain.getText() + " ");
		}
	}
}

	public static void main(String[] args) {
		JavaCalculator main = new JavaCalculator();
	}
}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

you're not supposed to "make" a Stack, just use it. everything you need from a Stack is already been written.

so what exactly is your problem? I don't really see a question in your post.

I don't think I am done yet. I don't know how to write pop method.and also I am not sure if my push method is correct or not.

that's my point ... you don't have to write a pop method, you have to call it.
take a look at the Stack api, maybe that 'll clear something up.

Besides: is there a specific reason why you are starting two threads for a single question?

This article has been dead for over six months. Start a new discussion instead.