Expand|Select|Wrap|Line Numbers
- public class Calculator {
- private long input = 0; // current input
- private long result = 0; // last input/result
- private String lastOperator = ""; // keeps track of the last operator entered
- /* Digit entered as integer value i
- * Updates the value of input accordingly to (input * 10) + i
- */
- public void inDigit(long i) {
- input = (input * 10) + i;
- //System.out.println("** inDigit **input " + input + " **result " + result);
- }
- /* Operator entered + - or *
- */
- public void inOperator(String op) {
- inEquals();
- // save the current input as result to get ready for next input
- input = 0;
- // remember which operator was entered
- lastOperator = op;
- }
- /* Equals operation
- * sets result to current result + - or * input (depending on
- lastOperator)
- */
- public void inEquals() {
- if (lastOperator.equals("+")) {
- result += input;
- } else if (lastOperator.equals("-")) {
- result -= input;
- } else if (lastOperator.equals("*")) {
- result *= input;
- } else {
- result = input;
- }
- // reset last operator to "nothing"
- lastOperator = "";
- }
- /* Clear operation */
- public void inClear() {
- input = 0;
- result = 0;
- lastOperator = "";
- }
- /* return the current result */
- public long getResult() {
- return result;
- }
- /* return the current input value */
- public long getCurrentInput() {
- return input;
- }
- public void clearInput(){
- input = 0;
- }
- }
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.util.Scanner;
- public class CalculatorPanel extends JPanel {
- // an array of buttons displayed on the calculator
- private JButton[] digitButtons;
- // output display for the calculator
- private JTextField display = new JTextField(10);
- private Calculator calc = new Calculator();
- /*main method - sets up JFrame*/
- public static void main(String [] args){
- JFrame frame = new JFrame("Calculator");
- frame.setContentPane(new CalculatorPanel());
- frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
- /* Constructor -- builds a GUI for a calculator */
- public CalculatorPanel() {
- /* create an array of button labels */
- String[] buttonLabels = {"1", "2", "3", "4", "5", "6",
- "7", "8", "9", "C", "0", "=", "+", "-", "*"};
- /* Create an array of buttons. */
- digitButtons = new JButton[buttonLabels.length];
- /* Create an actionListener */
- ButtonListener listener = new ButtonListener();
- /* Create a 5 x 3 grid for placement of buttons. */
- JPanel buttonGrid = new JPanel();
- buttonGrid.setLayout(new GridLayout(5, 3));
- /* Create a button with each button label, add it to buttonGrid,
- * and register the button as a listener. */
- for (int nextBut = 0; nextBut < digitButtons.length; nextBut++) {
- digitButtons[nextBut] = new JButton(buttonLabels[nextBut]);
- buttonGrid.add(digitButtons[nextBut]);
- digitButtons[nextBut].addActionListener(listener);
- }
- /* Create a message for the user*/
- JLabel instruct = new JLabel("Press a button");
- /* add the components to this JPanel*/
- setLayout(new BorderLayout());
- add(instruct, BorderLayout.NORTH);
- add(buttonGrid, BorderLayout.CENTER);
- add(display, BorderLayout.SOUTH);
- }
- /* represents a listener for button presses */
- private class ButtonListener implements ActionListener{
- /* what to do when a button has been pressed */
- public void actionPerformed(ActionEvent aE) {
- JButton whichButton = (JButton) aE.getSource();
- /* actions performed depending on which button is clicked */
- if ("+".equals(whichButton.getText())){
- calc.inOperator("+");
- display.setText("" + calc.getResult());
- }
- else if ("-".equals(whichButton.getText())) {
- calc.inOperator("-");
- display.setText("" + calc.getResult());
- }
- else if ("*".equals(whichButton.getText())) {
- calc.inOperator("*");
- display.setText("" + calc.getResult());
- }
- else if ("C".equals(whichButton.getText())) {
- calc.inClear();
- display.setText("");
- }
- else if ("=".equals(whichButton.getText())) {
- calc.inEquals();
- display.setText("" + calc.getResult());
- calc.inClear();
- }
- else {
- long t = 0;
- Scanner scan = new Scanner(whichButton.getText());
- t = scan.nextInt();
- calc.inDigit(t);
- display.setText("" + calc.getCurrentInput());
- }
- }//end actionPerformed
- }//end ButtonListener method
- }//end class
Expand|Select|Wrap|Line Numbers
- public class CalcText {
- // the calculator we'll be using
- private static Calculator c = new Calculator();
- /* Main method - performs a simple calculation on the calculator */
- public static void main(String[] args) {
- // A simple calculation, 50 - 26 =
- c.inDigit(5);
- c.inDigit(0);
- c.inOperator("-");
- c.inDigit(2);
- c.inDigit(6);
- c.inEquals();
- System.out.println( "50 - 26 = " + c.getResult());
- c.inClear();
- }
- }
basically i can operate the calculator however, how can i process one or more of the following input sequences:
3+10= -4 =
3+4=+7=
what should i add??? Cheers!