1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Stack RPN calculator

  1. Oct 31, 2011 #1
    I have no idea what wrong my cods when I press = button of my calculator nothing happens.
    can someone help me to fix my codes thanks.

    Code (Text):

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

    public class Ahsan 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;
     
    char ch;
     Stack<String> cStack=new Stack<String>();
     public Ahsan() {
     


    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);
     
    ButtonHandler buttonHandler=new ButtonHandler();
     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 enterHandler implements ActionListener
     {
     public void actionPerformed(ActionEvent evt)
     {
     int a;
     int b;
     
    if (evt.getSource() == jbtnEnter)
     {
     //JButton hooked = (JButton) evt.getSource();
     
    jtfMain.setText(jtfMain.getText() + " ");
     
    String value = jtfMain.getText();
     
    //if(value.lastIndexOf().equals("+"))
     //{
     String [] itemValue = value.split(" ");
     //System.out.println(itemValue.length);
     
    for (int i=0;i<itemValue.length;i++)
     {
     cStack.push(itemValue[i]);
     }
     

    if(cStack.pop().equals("+"))
     {
     
    BigInteger first = new BigInteger(cStack.pop());
     BigInteger second = new BigInteger(cStack.pop());
     
    first = first.add(second);
     cStack.push(first + "");
     }
     if(cStack.pop().equals("-"))
     {
     
    BigInteger first = new BigInteger(cStack.pop());
     BigInteger second = new BigInteger(cStack.pop());
     
    first = first.subtract(second);
     cStack.push(first + "");
     }
     
    /*else if(cStack.pop().equals("-"))
     {
     BigInteger first = new BigInteger(cStack.pop());
     BigInteger second = new BigInteger(cStack.pop());
     
    first = first.subtract(second);
     cStack.push(first + "");
     }*/
     //jtfMain.setText(cStack.pop());
     /*switch(ch)
     {
     case "/" : a = first.divide(second);
     }*/
     }
     }
     
    }
     //}
     
    public class ButtonHandler implements ActionListener
     {
     public void actionPerformed(ActionEvent evt)
     {
     JButton clicked = (JButton) evt.getSource();
     
    if(clicked == jbtnOn)
     {
     jtfMain.setText("");
     }
     else if(clicked == jbtnOne)
     {
     jtfMain.setText(jtfMain.getText() + "1");
     //cStack.push(1);
     }
     else if(clicked== jbtnTwo)
     {
     jtfMain.setText(jtfMain.getText() + "2");
     //cStack.push(2);
     }
     else if(clicked== jbtnThree)
     {
     jtfMain.setText(jtfMain.getText() + "3");
     //cStack.push(3);
     }
     else if(clicked== jbtnFour)
     {
     jtfMain.setText(jtfMain.getText() + "4");
     //cStack.push(4);
     }
     else if(clicked== jbtnFive)
     {
     jtfMain.setText(jtfMain.getText() + "5");
     //cStack.push(5);
     }
     else if(clicked== jbtnSix)
     {
     jtfMain.setText(jtfMain.getText() + "6");
     //cStack.push(6);
     }
     else if(clicked== jbtnSeven)
     {
     jtfMain.setText(jtfMain.getText() + "7");
     //cStack.push(7);
     }
     else if(clicked== jbtnEight)
     {
     jtfMain.setText(jtfMain.getText() + "8");
     //cStack.push(8);
     }
     else if(clicked == jbtnNine)
     {
     jtfMain.setText(jtfMain.getText() + "9");
     //cStack.push(9);
     }
     else if(clicked == jbtnZero)
     {
     jtfMain.setText(jtfMain.getText() + "0");
     //add.numbers[];
     }
     else if(clicked == jbtnDivide)
     {
     jtfMain.setText(jtfMain.getText() + "/");
     //ch = '/';
     }
     
    else if(clicked== jbtnMultiply)
     {
     jtfMain.setText(jtfMain.getText() + "X");
     
    }
     else if(clicked== jbtnPlus)
     {
     jtfMain.setText(jtfMain.getText() + "+");
     //ch = 'a';
     }
     else if(clicked== jbtnMinus)
     {
     jtfMain.setText(jtfMain.getText() + "-");
     
    }
     /*else if(clicked == jbtnEnter)
     {
     //cStack.push(numbers[0]);
     jtfMain.setText(jtfMain.getText() + " ");
     }*/
     else if(clicked == jbtnEqual)
     {
     jtfMain.setText(jtfMain.getText() + cStack.pop());
     }
     }
     }
     
    public static void main(String[] args) {
     Ahsan main = new Ahsan();
     }
     }
     
    Last edited by a moderator: Nov 1, 2011
  2. jcsd
  3. Oct 31, 2011 #2

    Mark44

    Staff: Mentor

    Do not double post your questions.
     
  4. Oct 31, 2011 #3

    NascentOxygen

    User Avatar

    Staff: Mentor

    I wouldn't know the first thing about java. But I do know that RPN calculators do not have an equals sign. They do not use an equals sign; it has no part to play in their operation. So if everything else works for your calculator, except the equals sign, then perhaps you are worrying unnecessarily?
     
  5. Oct 31, 2011 #4

    Mark44

    Staff: Mentor

    Taking what NascentOxygen said into account, if all you did was press =, what did you expect to happen?

    Instead of an = button, you should have an Enter button. With an RPN calculator, to add 5 and 3, you would do this:
    Click 5
    Click Enter
    Click 3
    Click +

    The result should be 8.
     
  6. Oct 31, 2011 #5
    I do have enter button but when i press it nothing happens
     
  7. Oct 31, 2011 #6

    NascentOxygen

    User Avatar

    Staff: Mentor

    It sounds like you are not familiar with the code you are using.

    To add two numbers, type these 4 keypresses:
    5
    enter
    2
    +
     
  8. Oct 31, 2011 #7
    I know how RPN calculator works but the problem is when I put numbers and press enter I don't get any result. thanks
     
  9. Nov 1, 2011 #8

    Mark44

    Staff: Mentor

    What is supposed to happen when you press enter? And by that, I mean, what part of the code should you be looking at? Since this is code that you wrote, you should have some idea of how the pieces fit together.
     
  10. Nov 1, 2011 #9

    NascentOxygen

    User Avatar

    Staff: Mentor

    Because you have it commented out.
     
  11. Nov 1, 2011 #10
    To subtract two numbers it should be like this
    7
    enter
    1
    -
     
  12. Nov 1, 2011 #11

    Mark44

    Staff: Mentor

    Yes.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Stack RPN calculator
  1. Need java stack help (Replies: 3)

  2. Energy calculations (Replies: 4)

Loading...