postfix calculator using java
you will make a basic postfix calculator.
This calculator should reside in a class called Calculator, which extends JFrame. To add listeners to buttons, you should use the anonymous class strategy we discussed in lecture. For this basic calculator, for the buttons, you should simply add ActionListeners. The calculator should look like the one below. The dimensions are 160 x 260. The text area should not be editable, it should only display information from the button presses.
Being a postfix calculator, the first operation button you click will perform the selected operation on whatever input was entered and zero (the initial value for some variable in which to save input, say, currentResult). Thus, entering 20 * 10 +, the result should be 10 ((0 * 20) + 10). Entering 20 * 10 + 30 * should result in 300. Entering 10 + 20 *, on the other hand, should return 200 ((0 + 10) * 20).
Answer:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame { private int currentResult = 0; private int numberEnteredByUser = 0; private JTextField outputArea; private JButton[] buttonArray; private JButton[] operationButtons; private void processDigit(int digitPressed){ String stringToDisplay; numberEnteredByUser = numberEnteredByUser * 10 + digitPressed; stringToDisplay = String.valueOf(numberEnteredByUser); outputArea.setText(stringToDisplay); } private void carryOutOperation(char operation){//operation is '+', '-', '*', '/' String stringToDisplay; switch (operation){ case '+': currentResult += numberEnteredByUser; break; case '-': currentResult -= numberEnteredByUser; break; case '*': currentResult *= numberEnteredByUser; break; case '/': currentResult /= numberEnteredByUser; break; default:break; } numberEnteredByUser = 0; stringToDisplay = String.valueOf(currentResult); outputArea.setText(stringToDisplay); } public Calculator(){ super("Calculator"); char operatorArray[] = {'+', '-', '*', '/'}; setLayout(new FlowLayout()); outputArea = new JTextField(10); add(outputArea); outputArea.setEditable(false); buttonArray = new JButton[10]; operationButtons = new JButton[4]; for (int i = 0; i < 10; i++){ buttonArray[i] = new JButton(String.valueOf(i)); buttonArray[i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { processDigit(Integer.parseInt(e.getActionCommand())); } } ); if (i > 0){ add(buttonArray[i]); } } buttonArray[0].setPreferredSize(new Dimension(130, 25)); add(buttonArray[0]); for (int i = 0; i < 4; i++){ operationButtons[i] = new JButton(); operationButtons[i].setPreferredSize(new Dimension(50, 25)); operationButtons[i].setText(String.valueOf(operatorArray[i])); operationButtons[i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { carryOutOperation(e.getActionCommand().charAt(0)); } } ); add(operationButtons[i]); } setSize(160, 260); setVisible(true); } public static void main(String args[]){ Calculator aFrame = new Calculator(); } }
Leave a reply