Advertisement
brilliant_moves

MyCalculator.java

Feb 26th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.22 KB | None | 0 0
  1. import java.awt.*;      // for Graphics
  2. import java.awt.event.*;    // for ActionListener
  3. import javax.swing.*;       // for JFrame
  4.  
  5. public class MyCalculator {
  6.  
  7.     /**
  8.     *   Program:    MyCalculator.java by Chris Clarke
  9.     *   Created:    03.06.2009
  10.     *   Purpose:    Calculator with Memory and Square Root.
  11.     *   Modified:   29.10.2011 Memory added. Square root added.
  12.     *           18.05.2012 Converted from Frame to JFrame
  13.     */ 
  14.  
  15.     public static void main(String[] args) {
  16.         MyCalculatorJFrame frame = new MyCalculatorJFrame();
  17.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  18.         // Set to a reasonable size.
  19.         frame.setSize(255, 230);
  20.         frame.setVisible(true);
  21.     }
  22. }
  23.  
  24. class MyCalculatorJFrame extends JFrame implements ActionListener {
  25.  
  26.     public MyCalculatorJFrame() {
  27.         setTitle("MyCalculator by Chris Clarke");
  28.        
  29.         // Build control panel.
  30.         JPanel panel = new JPanel();
  31.  
  32.             btnDigit = new JButton[10];
  33.  
  34.         for (int i=0; i<10; i++) {
  35.             btnDigit[i] = new JButton(""+i);
  36.             btnDigit[i].addActionListener(this);
  37.                 panel.add(btnDigit[i]);
  38.         } // end for
  39.  
  40.         btnPoint = new JButton(".");
  41.         btnPoint.addActionListener( this);
  42.         panel.add(btnPoint);
  43.         btnEquals = new JButton("=");
  44.         btnEquals.addActionListener(this);
  45.         panel.add(btnEquals);
  46.         tfDisplay = new JTextField(16);
  47.         tfDisplay.setText(""+number);
  48.         panel.add(tfDisplay);
  49.         btnPlus = new JButton("+");
  50.         btnPlus.addActionListener(this);
  51.         panel.add(btnPlus);
  52.         btnMinus = new JButton("-");
  53.         btnMinus.addActionListener(this);
  54.         panel.add(btnMinus);
  55.         btnTimes = new JButton("*");
  56.         btnTimes.addActionListener(this);
  57.         panel.add(btnTimes);
  58.         btnDivide = new JButton("/");
  59.         btnDivide.addActionListener(this);
  60.         panel.add(btnDivide);
  61.         btnMemory = new JButton("M");
  62.         btnMemory.addActionListener(this);
  63.         panel.add(btnMemory);
  64.         btnSquareRoot = new JButton("SqRt");
  65.         btnSquareRoot.addActionListener(this);
  66.         panel.add(btnSquareRoot);
  67.         btnCancelEntry = new JButton("CE");
  68.         btnCancelEntry.addActionListener(this);
  69.         panel.add(btnCancelEntry);
  70.         btnCancelAll = new JButton("AC");
  71.         btnCancelAll.addActionListener(this);
  72.         panel.add(btnCancelAll);
  73.         add(panel, BorderLayout.CENTER);
  74.     }
  75.  
  76.     public void performCalculation(String operator) {
  77.         if (digit||op) secondNum = number;
  78.         switch (operator) {
  79.             case ("Plus"):
  80.                 number = firstNum+secondNum;
  81.                 break;
  82.             case ("Minus"):
  83.                 number = firstNum-secondNum;
  84.                 break;
  85.             case ("Times"):
  86.                 number = firstNum*secondNum;
  87.                 break;
  88.             case ("Divide"):
  89.                 if (secondNum == 0) {
  90.                     tfDisplay.setText("Division by zero error");
  91.                     return;
  92.                 } else {
  93.                     number = firstNum/secondNum;
  94.                 } // end if
  95.         } // end switch
  96.         reset();
  97.         // display result
  98.         tfDisplay.setText(""+number);
  99.     }
  100.  
  101.     public void reset() {
  102.         // make a note of the first number
  103.         firstNum = number;
  104.         point = false;
  105.         digit = false;
  106.     }
  107.  
  108.     public void cancelEntry() {
  109.         number = 0;
  110.         point = false;
  111.         digit = false;
  112.         tfDisplay.setText(""+number);
  113.     }
  114.  
  115.     public void cancelAll() {
  116.         // do CancelAll stuff
  117.         number = 0;
  118.         operator = "";
  119.         firstNum = 0;
  120.         secondNum = 0;
  121.         point = false;
  122.         digit = false;
  123.         tfDisplay.setText(""+number);
  124.     }
  125.  
  126.     public void doDecimalPoint() {
  127.         if (!point) {
  128.             if (!digit)
  129.                 tfDisplay.setText("0.");
  130.             else
  131.                 tfDisplay.setText(tfDisplay.getText()+".");
  132.             point = true;
  133.             digit = true;
  134.         }
  135.     }
  136.  
  137.     public void doMemoryStuff() {
  138.         // if previous key was "=", set contents of memory to number;
  139.         // otherwise, recall memory and display contents on-screen
  140.         if (equalsKey) {
  141.             mem = number;
  142.         } else {
  143.             number = mem;
  144.             tfDisplay.setText(""+number);
  145.         }
  146.     }
  147.  
  148.     public void doDigit(ActionEvent e) {
  149.         for (int i=0; i<=9; i++) {
  150.             if (e.getSource() == btnDigit[i]) {
  151.                 if (!digit) {
  152.                     tfDisplay.setText("");
  153.                     digit = true;
  154.                 }
  155.                 tfDisplay.setText(tfDisplay.getText()+i);
  156.                 number= Double.parseDouble(tfDisplay.getText());
  157.             }
  158.         }
  159.     }
  160.  
  161.     public void doSquareRootStuff() {
  162.         double divisor0 = 3.8;              // or any non-zero number
  163.         double divisor1 = 0;
  164.         for (int i = 0; i < 8; i++) {
  165.             //  (10 / M = "" + M = "" / 2 = "" M) repeat
  166.             divisor1 = number / divisor0;
  167.             divisor0 = (divisor0 + divisor1)/2; // the average of the 2 divisors
  168.         }
  169.         number = divisor1;
  170.         tfDisplay.setText(""+number);
  171.     }
  172.  
  173.     public void actionPerformed(ActionEvent e) {
  174.         if (e.getSource() == btnPlus) {
  175.             // do Plus stuff
  176.             operator = "Plus";
  177.             reset();
  178.             op = true;
  179.             equalsKey=false;
  180.         } else if (e.getSource() == btnMinus) {
  181.             // do Minus stuff
  182.             operator = "Minus";
  183.             reset();
  184.             op = true;
  185.             equalsKey=false;
  186.         } else if (e.getSource() == btnTimes) {
  187.             // do Times stuff
  188.             operator = "Times";
  189.             reset();
  190.             op = true;
  191.             equalsKey=false;
  192.         } else if (e.getSource() == btnDivide) {
  193.             // do Divide stuff
  194.             operator = "Divide";
  195.             reset();
  196.             op = true;
  197.             equalsKey=false;
  198.         } else if (e.getSource() == btnMemory) {
  199.             // do Memory stuff
  200.             doMemoryStuff();
  201.             equalsKey=false;
  202.         } else if (e.getSource() == btnSquareRoot) {
  203.             // do SquareRoot stuff
  204.             doSquareRootStuff();
  205.             equalsKey=false;
  206.         } else if (e.getSource() == btnEquals) {
  207.             // do Equals stuff
  208.             performCalculation(operator);
  209.             equalsKey=true;
  210.         } else if (e.getSource() == btnCancelEntry) {
  211.             // do CancelEntry stuff
  212.             cancelEntry();
  213.             equalsKey=false;
  214.         } else if (e.getSource() == btnCancelAll) {
  215.             // do CancelAll stuff
  216.             cancelAll();
  217.             equalsKey=false;
  218.         } else if (e.getSource() == btnPoint) {
  219.             // do Decimal Point stuff
  220.             doDecimalPoint();
  221.             op = false;
  222.             equalsKey=false;
  223.         } else {
  224.             // do digit stuff
  225.             doDigit(e);
  226.             op = false;
  227.             equalsKey=false;
  228.         } // end if
  229.     } // end actionPerformed()
  230.  
  231.     private JTextField  tfDisplay;
  232.     private JButton[]   btnDigit;
  233.     private JButton     btnPoint;
  234.     private JButton     btnPlus, btnMinus, btnTimes, btnDivide;
  235.     private JButton     btnEquals, btnMemory, btnSquareRoot;
  236.     private JButton     btnCancelEntry, btnCancelAll;
  237.     private double      number=0;
  238.     private double      firstNum=0;
  239.     private double      secondNum=0;
  240.     private double      mem=0;          // memory
  241.     private String      operator="";
  242.     private boolean     point=false;
  243.     private boolean     digit = false;
  244.     private boolean     op = false;
  245.     private boolean     equalsKey=false;    // true when "=" button clicked
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement