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.util.Stack;
import java.awt.*;

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);

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();

}

}

}

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();

}

}

}

Edited 5 Years Ago by ali11: to put code tags.

It would be nice if your code is enclosed by code tags. Its not at all readable.

sorry i forgot> I just fix it.

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