import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Stack; public class ExpressionEval extends Applet implements ActionListener { private Button[][] buttons; // array of Button objects. private Label result = new Label(); // Label for result private Label infix = new Label(); // Label for expression private Label nextChar = new Label(); // Label for each char of expression private String infixStr = ""; // expression being evaluated protected Label operandLabel = new Label(); // Label for operand stack protected Label operatorLabel = new Label(); // Label for operator stack Calculator calc = new Calculator(); // Calculator object public void init() { Label instruct = new Label("Press = to end expression"); add(instruct); Panel map = new Panel(); // a panel in our applet // characters to be used as button labels. char [][] opMap = { {'0', '1', '2'}, {'3', '4', '5'}, {'6', '7', '8'}, {'9', ' ', '='}, {'*', '/', '+'}, {'-', '(', ')'} }; // Create an array of buttons of the proper dimensions. buttons = new Button[opMap.length][opMap[0].length]; // Create the 6 x 3 grid of buttons map.setLayout(new GridLayout(buttons.length, buttons[0].length)); // Create and label each button, add it to applet, // and register applet as listener. for (int r = 0; r < buttons.length; r++) for (int c = 0; c < buttons[r].length; c++) { // create labelled button - take label from array opMap buttons[r][c] = new Button("" + opMap[r][c]); map.add(buttons[r][c]); // add to panel buttons[r][c].addActionListener(this); } add(map); nextChar.setText("________________________________________"); add(nextChar); infix.setText("________________________________________"); add(infix); operandLabel.setText("___________________________________________"); add(operandLabel); operatorLabel.setText("___________________________________________"); add(operatorLabel); result.setText("___________________________________________"); add(result); // add reset button } // init public void clear() { } public void actionPerformed(ActionEvent e) { Button b = (Button)e.getSource(); // Gets the button pressed, if (b == reset) { return; } char token = (b.getLabel()).charAt(0); // token is label character infixStr = infixStr + token; // Append it to expression nextChar.setText("Character is " + token); infix.setText("Expression so far: " + infixStr); // Process the token if (token >= '0' && token <= '9') calc.processDigit(token); else { String message = calc.processOperator(token, this); result.setText(message); } } }