Results 1 to 5 of 5

Thread: need stack help

  1. #1
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default need stack help

    First i create simple claculator.but now I am trying to change it to RPN calculator by using stack.Can some one help me((especially where to use pop method) because I don't know alot about stack.here is my codes so far.

    import java.awt.event.*;
    import javax.swing.*;

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


    public class CalcGUIPanel extends JPanel
    {



    Stack s=new Stack();


    //--\- Component referenced during execution

    private JTextField displayField; // display result / input.

    //--\- Variables representing state of the calculator

    private boolean startNumber = true; // true: num key next

    private int resultValue = 0; // result so far

    private String previousOp = "="; // previous operation

    //========================================= static (class) variables

    private static final Font BIGGER_FONT =

    new Font("monspaced", Font.PLAIN, 20);

    //================================================== ==== constructor

    public CalcGUIPanel()
    {

    //--\- Display field

    displayField = new JTextField();


    //--\- Clear button

    JButton clearButton = new JButton("CLEAR");

    clearButton.setFont(BIGGER_FONT);

    clearButton.addActionListener(new ClearListener());

    //--\- One listener for all numeric keys.

    ActionListener numListener = new NumListener();

    //--\- Layout numeric keys in a grid. Generate the buttons

    // in a loop from the chars in a string.

    String buttonOrder = "789456123 0 ";

    JPanel buttonPanel = new JPanel(new GridLayout(5, 3));

    for (int i = 0; i < buttonOrder.length(); i++) {

    String keyTop = buttonOrder.substring(i, i+1);

    if (keyTop.equals(" ")) {

    buttonPanel.add(new JLabel(""));

    } else {

    JButton b = new JButton(keyTop);

    b.addActionListener(numListener);
    s.push("0");
    s.push("1");
    s.push("2");
    s.push("3");
    s.push("4");
    s.push("5");
    s.push("6");
    s.push("7");
    s.push("8");
    s.push("9");

    b.setFont(BIGGER_FONT);

    buttonPanel.add(B);

    }

    }

    //--\- One ActionListener to use for all operator buttons.

    ActionListener opListener = new OpListener();

    //--\- Create panel with gridlayout to hold operator buttons.

    // Use array of button names to create buttons in a loop.

    JPanel opPanel = new JPanel(new GridLayout(5, 1));

    String[] opOrder = {"+", "-", "*", "/", "enter"};

    for (int i = 0; i < opOrder.length; i++) {

    JButton b = new JButton(opOrder[i]);

    b.addActionListener(opListener);

    s.push("+");
    s.push("-");
    s.push("*");
    s.push("/");
    s.push("enter");


    b.setFont(BIGGER_FONT);

    opPanel.add(B);

    }

    //--\- Layout the top-level panel.

    this.setLayout(new BorderLayout());

    this.add(displayField, BorderLayout.NORTH );

    this.add(buttonPanel , BorderLayout.CENTER);

    this.add(opPanel , BorderLayout.EAST );

    this.add(clearButton , BorderLayout.SOUTH );

    }//end constructor

    //================================================== ==== action_clear

    /*\* Called by Clear btn action listener and elsewhere.*/

    private void action_clear() {

    startNumber = true;

    displayField.setText("0");

    resultValue = 0;

    previousOp = "=";

    }

    // inner listener class OpListener

    /*\* Listener for all op buttons. \*/

    class OpListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    // The calculator is always in one of two states.

    // 1. A number must be entered \-\- this operator is wrong.

    // 2. An operator must be entered \-\- we're ok.

    {

    startNumber = true; // Next thing must be a number

    try {

    // Get value from display field, convert, do prev op

    // If this is the first op, \_previousOp will be =.

    String displayText = displayField.getText();

    int currentValue = Integer.parseInt(displayText);

    if (previousOp.equals("=")) {

    resultValue = currentValue;

    } else if (previousOp.equals("+")) {

    resultValue += currentValue;

    } else if (previousOp.equals("-")) {

    resultValue -= currentValue;

    } else if (previousOp.equals("*")) {

    resultValue *= currentValue;

    } else if (previousOp.equals("/")) {

    resultValue /= currentValue;

    }

    displayField.setText(""+resultValue);

    } catch (NumberFormatException ex) {

    action_clear();

    displayField.setText("Error");

    }

    //--\- set \_previousOp for the next operator.

    previousOp = e.getActionCommand();

    }//endif \_startNumber

    }//endmethod

    }//end class

    //////////////////////////////////// inner listener class ClearListener

    // Action listener for numeric keys

    class NumListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    String digit = e.getActionCommand(); // Get text from button

    if (startNumber) {

    // This is the first digit, clear field and set

    displayField.setText(digit);

    startNumber = false;

    } else {

    // Add this digit to the end of the display field

    displayField.setText(displayField.getText() + digit);

    }

    }

    }//end class

    //inner listener class ClearListener

    class ClearListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    action_clear();

    }

    }

    }
    Last edited by ali1; 11-05-2011 at 08:31 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: need stack help

    It is very hard to read your code since you're using [quote] [/quote] tags and not [code] [/code] around your code. Could you please edit your question to use the code tags?

  3. #3
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: need stack help

    First i create simple claculator.but now I am trying to change it to RPN calculator by using stack.Can some one help me((especially where to use pop method) because I don't know alot about stack.here is my codes so far.
    import java.awt.event.*;

    import javax.swing.*;

    import javax.swing.event.*;
    import java.util.Scanner;


    public class CalcGUIPanel extends JPanel
    {



    Stack s=new Stack();


    //--\- Component referenced during execution

    private JTextField displayField; // display result / input.

    //--\- Variables representing state of the calculator

    private boolean startNumber = true; // true: num key next

    private int resultValue = 0; // result so far

    private String previousOp = "="; // previous operation

    //========================================= static (class) variables

    private static final Font BIGGER_FONT =

    new Font("monspaced", Font.PLAIN, 20);

    //================================================== ==== constructor

    public CalcGUIPanel()
    {

    //--\- Display field

    displayField = new JTextField();


    //--\- Clear button

    JButton clearButton = new JButton("CLEAR");

    clearButton.setFont(BIGGER_FONT);

    clearButton.addActionListener(new ClearListener());

    //--\- One listener for all numeric keys.

    ActionListener numListener = new NumListener();

    //--\- Layout numeric keys in a grid. Generate the buttons

    // in a loop from the chars in a string.

    String buttonOrder = "789456123 0 ";

    JPanel buttonPanel = new JPanel(new GridLayout(5, 3));

    for (int i = 0; i < buttonOrder.length(); i++) {

    String keyTop = buttonOrder.substring(i, i+1);

    if (keyTop.equals(" ")) {

    buttonPanel.add(new JLabel(""));

    } else {

    JButton b = new JButton(keyTop);

    b.addActionListener(numListener);
    s.push("0");
    s.push("1");
    s.push("2");
    s.push("3");
    s.push("4");
    s.push("5");
    s.push("6");
    s.push("7");
    s.push("8");
    s.push("9");

    b.setFont(BIGGER_FONT);

    buttonPanel.add(B);

    }

    }

    //--\- One ActionListener to use for all operator buttons.

    ActionListener opListener = new OpListener();

    //--\- Create panel with gridlayout to hold operator buttons.

    // Use array of button names to create buttons in a loop.

    JPanel opPanel = new JPanel(new GridLayout(5, 1));

    String[] opOrder = {"+", "-", "*", "/", "enter"};

    for (int i = 0; i < opOrder.length; i++) {

    JButton b = new JButton(opOrder[i]);

    b.addActionListener(opListener);

    s.push("+");
    s.push("-");
    s.push("*");
    s.push("/");
    s.push("enter");


    b.setFont(BIGGER_FONT);

    opPanel.add(B);

    }

    //--\- Layout the top-level panel.

    this.setLayout(new BorderLayout());

    this.add(displayField, BorderLayout.NORTH );

    this.add(buttonPanel , BorderLayout.CENTER);

    this.add(opPanel , BorderLayout.EAST );

    this.add(clearButton , BorderLayout.SOUTH );

    }//end constructor

    //================================================== ==== action_clear

    /*\* Called by Clear btn action listener and elsewhere.*/

    private void action_clear() {

    startNumber = true;

    displayField.setText("0");

    resultValue = 0;

    previousOp = "=";

    }

    // inner listener class OpListener

    /*\* Listener for all op buttons. \*/

    class OpListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    // The calculator is always in one of two states.

    // 1. A number must be entered \-\- this operator is wrong.

    // 2. An operator must be entered \-\- we're ok.

    {

    startNumber = true; // Next thing must be a number

    try {

    // Get value from display field, convert, do prev op

    // If this is the first op, \_previousOp will be =.

    String displayText = displayField.getText();

    int currentValue = Integer.parseInt(displayText);

    if (previousOp.equals("=")) {

    resultValue = currentValue;

    } else if (previousOp.equals("+")) {

    resultValue += currentValue;

    } else if (previousOp.equals("-")) {

    resultValue -= currentValue;

    } else if (previousOp.equals("*")) {

    resultValue *= currentValue;

    } else if (previousOp.equals("/")) {

    resultValue /= currentValue;

    }

    displayField.setText(""+resultValue);

    } catch (NumberFormatException ex) {

    action_clear();

    displayField.setText("Error");

    }

    //--\- set \_previousOp for the next operator.

    previousOp = e.getActionCommand();

    }//endif \_startNumber

    }//endmethod

    }//end class

    //////////////////////////////////// inner listener class ClearListener

    // Action listener for numeric keys

    class NumListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    String digit = e.getActionCommand(); // Get text from button

    if (startNumber) {

    // This is the first digit, clear field and set

    displayField.setText(digit);

    startNumber = false;

    } else {

    // Add this digit to the end of the display field

    displayField.setText(displayField.getText() + digit);

    }

    }

    }//end class

    //inner listener class ClearListener

    class ClearListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    action_clear();

    }

    }

    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: need stack help

    Again your posted code is near impossible to read, again you'll want to use the tag [code] on top of your code block and the tag [/code] below your code block. For example which is easier to read, your posts or this?:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.Stack;
    
    public class CalcGuiPanel extends JPanel {
       Stack s = new Stack();
       // --\- Component referenced during execution
       private JTextField displayField; // display result / input.
       // --\- Variables representing state of the calculator
       private boolean startNumber = true; // true: num key next
       private int resultValue = 0; // result so far
       private String previousOp = "="; // previous operation
       // ========================================= static (class) variables
       private static final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20);
    
       // ====================================================== constructor
       public CalcGuiPanel() {
          // --\- Display field
          displayField = new JTextField();
          // --\- Clear button
          JButton clearButton = new JButton("CLEAR");
          clearButton.setFont(BIGGER_FONT);
          clearButton.addActionListener(new ClearListener());
          // --\- One listener for all numeric keys.
          ActionListener numListener = new NumListener();
          // --\- Layout numeric keys in a grid. Generate the buttons
          // in a loop from the chars in a string.
          String buttonOrder = "789456123 0 ";
          JPanel buttonPanel = new JPanel(new GridLayout(5, 3));
          for (int i = 0; i < buttonOrder.length(); i++) {
             String keyTop = buttonOrder.substring(i, i + 1);
             if (keyTop.equals(" ")) {
                buttonPanel.add(new JLabel(""));
             } else {
                JButton b = new JButton(keyTop);
                b.addActionListener(numListener);
                s.push("0");
                s.push("1");
                s.push("2");
                s.push("3");
                s.push("4");
                s.push("5");
                s.push("6");
                s.push("7");
                s.push("8");
                s.push("9");
                b.setFont(BIGGER_FONT);
                buttonPanel.add(b); //!! change B to b
             }
          }
          // --\- One ActionListener to use for all operator buttons.
          ActionListener opListener = new OpListener();
          // --\- Create panel with gridlayout to hold operator buttons.
          // Use array of button names to create buttons in a loop.
          JPanel opPanel = new JPanel(new GridLayout(5, 1));
          String[] opOrder = { "+", "-", "*", "/", "enter" };
          for (int i = 0; i < opOrder.length; i++) {
             JButton b = new JButton(opOrder[i]);
             b.addActionListener(opListener);
             s.push("+");
             s.push("-");
             s.push("*");
             s.push("/");
             s.push("enter");
             b.setFont(BIGGER_FONT);
             opPanel.add(b); //!! change B to b
          }
          // --\- Layout the top-level panel.
          this.setLayout(new BorderLayout());
          this.add(displayField, BorderLayout.NORTH);
          this.add(buttonPanel, BorderLayout.CENTER);
          this.add(opPanel, BorderLayout.EAST);
          this.add(clearButton, BorderLayout.SOUTH);
       }// end constructor
        // ====================================================== action_clear
    
       /* \* Called by Clear btn action listener and elsewhere. */
       private void action_clear() {
          startNumber = true;
          displayField.setText("0");
          resultValue = 0;
          previousOp = "=";
       }
    
       // inner listener class OpListener
       /* \* Listener for all op buttons. \ */
       class OpListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             // The calculator is always in one of two states.
             // 1. A number must be entered \-\- this operator is wrong.
             // 2. An operator must be entered \-\- we're ok.
             {
                startNumber = true; // Next thing must be a number
                try {
                   // Get value from display field, convert, do prev op
                   // If this is the first op, \_previousOp will be =.
                   String displayText = displayField.getText();
                   int currentValue = Integer.parseInt(displayText);
                   if (previousOp.equals("=")) {
                      resultValue = currentValue;
                   } else if (previousOp.equals("+")) {
                      resultValue += currentValue;
                   } else if (previousOp.equals("-")) {
                      resultValue -= currentValue;
                   } else if (previousOp.equals("*")) {
                      resultValue *= currentValue;
                   } else if (previousOp.equals("/")) {
                      resultValue /= currentValue;
                   }
                   displayField.setText("" + resultValue);
                } catch (NumberFormatException ex) {
                   action_clear();
                   displayField.setText("Error");
                }
                // --\- set \_previousOp for the next operator.
                previousOp = e.getActionCommand();
             }// endif \_startNumber
          }// endmethod
       }// end class
        // ////////////////////////////////// inner listener class ClearListener
        // Action listener for numeric keys
    
       class NumListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             String digit = e.getActionCommand(); // Get text from button
             if (startNumber) {
                // This is the first digit, clear field and set
                displayField.setText(digit);
                startNumber = false;
             } else {
                // Add this digit to the end of the display field
                displayField.setText(displayField.getText() + digit);
             }
          }
       }// end class
        // inner listener class ClearListener
    
       class ClearListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             action_clear();
          }
       }
    }
    Last edited by Fubarable; 11-05-2011 at 08:44 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: need stack help

    Next, I suggest that you work out your stack logic in a separate simpler non-GUI class. After you've gotten the bugs out in this simpler environment, only then add the code to your main program.

Similar Threads

  1. stack RPN calculaotr
    By ali1 in forum New To Java
    Replies: 3
    Last Post: 11-01-2011, 08:27 AM
  2. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 08:14 PM
  3. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 08:04 AM
  4. Stack Calculator
    By Bgreen7887 in forum New To Java
    Replies: 3
    Last Post: 01-28-2011, 11:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •