A Simple Calculator - Home

By: Nicholas Duchon
Feb 5, 2016


Note that in this calculator, the order of operations is strictly left to right, there is no precedence of multiplication over addition, for example.

Thus, 2 + 3 * 4 gives 5 * 4 which results in 20.

This, of course, is NOT the result if we followed the normal order of operations:

2 + 3 * 4 should be 2 + 12 = 14


Code:

// Some modifications I think make the code smaller and
// easier to understand
// - using lambda expressions

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

public class CalculatorRaleyND extends JFrame {
   public static final long serialVersionUID= 1243; // ND: random number

   enum Op {NONE, ADD, SUB, MUL, DIV};
   Op op = Op.NONE;
  
   private JTextField jtfResult = new JTextField ();
   boolean flagInputA = true;
   String inputA = "";
   String inputB = "";

   public CalculatorRaleyND() {
  
      JPanel inputNclearPanel = new JPanel();
      inputNclearPanel.setLayout(new GridLayout(0,3));
  
      //          INPUT BUTTONS
      String [] vals = {"1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0"};
      for (String st: vals) {
         JButton jb = new JButton (st);
         inputNclearPanel.add (jb);
         jb.addActionListener (e -> addToNumber(st));
      } // end settup up numeric keypad
       
      JButton jbtClr = new JButton ("C");
      inputNclearPanel.add (jbtClr);
  
      //              RESULT/OUTPUT
      jtfResult.setHorizontalAlignment(JTextField.RIGHT);
      jtfResult.setEditable(false);
  
      //              OPERATOR BUTTONS
      JButton jbtAdd = new JButton ("+");
      JButton jbtSub = new JButton ("-");
      JButton jbtMul = new JButton ("*");
      JButton jbtDiv = new JButton ("/");
      JButton jbtEql = new JButton ("=");
  
      JPanel operatorBtns = new JPanel();
      operatorBtns.setLayout(new GridLayout(0, 1));
      operatorBtns.add (jbtAdd);
      operatorBtns.add (jbtSub);
      operatorBtns.add (jbtMul);
      operatorBtns.add (jbtDiv);
      operatorBtns.add (jbtEql);
  
      jbtAdd .addActionListener (e -> operate (Op.ADD) );
      jbtSub .addActionListener (e -> operate (Op.SUB) );
      jbtMul .addActionListener (e -> operate (Op.MUL) );
      jbtDiv .addActionListener (e -> operate (Op.DIV) );
      jbtEql .addActionListener (e -> operate ( ) );
      jbtClr .addActionListener (e -> clear   ( ) );
     
   
   add (jtfResult       , BorderLayout.NORTH);
      add (inputNclearPanel, BorderLayout.CENTER);
      add (operatorBtns    , BorderLayout.EAST);

      pack ();
      setLocationRelativeTo (null);
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      setVisible (true);
  
} // end constructor
  
   void addToNumber (String v) {
      if (flagInputA)
        {inputA += v; set (inputA);}
      else
        {inputB += v; set (inputB);}
  
} // end method addToNumber
  
   void set (String s) {jtfResult.setText (s);
}

   void clear () {
      flagInputA = true;
      set ("");
      op = Op.NONE;
      inputA = inputB = "";
  
} // end method clear
  
   void operate (Op o) {
      operate ();
      op = o;
      operate ();
  
} // end method operate Op
  
   void operate () {
      if (inputA.equals("")) return;
      flagInputA = false;
      if (inputB.equals("")) return;
      double a = Double.parseDouble (inputA);
      double b = Double.parseDouble (inputB);
      double r = 0;
      switch (op) {
         case ADD: r = a + b; break;
         case SUB: r = a - b; break;
         case MUL: r = a * b; break;
         case DIV: r = a / b; break;
      } // end switch op
      inputA = "" + r;
      inputB = "";
      set (inputA);
  
} // end method operate

   public static void main(String[] args) {
      CalculatorRaleyND calc = new CalculatorRaleyND();
  
} // end main

} // end class CalculatorRaleyND



(End)