Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*; // for Graphics
- import java.awt.event.*; // for ActionListener
- import javax.swing.*; // for JFrame
- public class MyCalculator {
- /**
- * Program: MyCalculator.java by Chris Clarke
- * Created: 03.06.2009
- * Purpose: Calculator with Memory and Square Root.
- * Modified: 29.10.2011 Memory added. Square root added.
- * 18.05.2012 Converted from Frame to JFrame
- */
- public static void main(String[] args) {
- MyCalculatorJFrame frame = new MyCalculatorJFrame();
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
- // Set to a reasonable size.
- frame.setSize(255, 230);
- frame.setVisible(true);
- }
- }
- class MyCalculatorJFrame extends JFrame implements ActionListener {
- public MyCalculatorJFrame() {
- setTitle("MyCalculator by Chris Clarke");
- // Build control panel.
- JPanel panel = new JPanel();
- btnDigit = new JButton[10];
- for (int i=0; i<10; i++) {
- btnDigit[i] = new JButton(""+i);
- btnDigit[i].addActionListener(this);
- panel.add(btnDigit[i]);
- } // end for
- btnPoint = new JButton(".");
- btnPoint.addActionListener( this);
- panel.add(btnPoint);
- btnEquals = new JButton("=");
- btnEquals.addActionListener(this);
- panel.add(btnEquals);
- tfDisplay = new JTextField(16);
- tfDisplay.setText(""+number);
- panel.add(tfDisplay);
- btnPlus = new JButton("+");
- btnPlus.addActionListener(this);
- panel.add(btnPlus);
- btnMinus = new JButton("-");
- btnMinus.addActionListener(this);
- panel.add(btnMinus);
- btnTimes = new JButton("*");
- btnTimes.addActionListener(this);
- panel.add(btnTimes);
- btnDivide = new JButton("/");
- btnDivide.addActionListener(this);
- panel.add(btnDivide);
- btnMemory = new JButton("M");
- btnMemory.addActionListener(this);
- panel.add(btnMemory);
- btnSquareRoot = new JButton("SqRt");
- btnSquareRoot.addActionListener(this);
- panel.add(btnSquareRoot);
- btnCancelEntry = new JButton("CE");
- btnCancelEntry.addActionListener(this);
- panel.add(btnCancelEntry);
- btnCancelAll = new JButton("AC");
- btnCancelAll.addActionListener(this);
- panel.add(btnCancelAll);
- add(panel, BorderLayout.CENTER);
- }
- public void performCalculation(String operator) {
- if (digit||op) secondNum = number;
- switch (operator) {
- case ("Plus"):
- number = firstNum+secondNum;
- break;
- case ("Minus"):
- number = firstNum-secondNum;
- break;
- case ("Times"):
- number = firstNum*secondNum;
- break;
- case ("Divide"):
- if (secondNum == 0) {
- tfDisplay.setText("Division by zero error");
- return;
- } else {
- number = firstNum/secondNum;
- } // end if
- } // end switch
- reset();
- // display result
- tfDisplay.setText(""+number);
- }
- public void reset() {
- // make a note of the first number
- firstNum = number;
- point = false;
- digit = false;
- }
- public void cancelEntry() {
- number = 0;
- point = false;
- digit = false;
- tfDisplay.setText(""+number);
- }
- public void cancelAll() {
- // do CancelAll stuff
- number = 0;
- operator = "";
- firstNum = 0;
- secondNum = 0;
- point = false;
- digit = false;
- tfDisplay.setText(""+number);
- }
- public void doDecimalPoint() {
- if (!point) {
- if (!digit)
- tfDisplay.setText("0.");
- else
- tfDisplay.setText(tfDisplay.getText()+".");
- point = true;
- digit = true;
- }
- }
- public void doMemoryStuff() {
- // if previous key was "=", set contents of memory to number;
- // otherwise, recall memory and display contents on-screen
- if (equalsKey) {
- mem = number;
- } else {
- number = mem;
- tfDisplay.setText(""+number);
- }
- }
- public void doDigit(ActionEvent e) {
- for (int i=0; i<=9; i++) {
- if (e.getSource() == btnDigit[i]) {
- if (!digit) {
- tfDisplay.setText("");
- digit = true;
- }
- tfDisplay.setText(tfDisplay.getText()+i);
- number= Double.parseDouble(tfDisplay.getText());
- }
- }
- }
- public void doSquareRootStuff() {
- double divisor0 = 3.8; // or any non-zero number
- double divisor1 = 0;
- for (int i = 0; i < 8; i++) {
- // (10 / M = "" + M = "" / 2 = "" M) repeat
- divisor1 = number / divisor0;
- divisor0 = (divisor0 + divisor1)/2; // the average of the 2 divisors
- }
- number = divisor1;
- tfDisplay.setText(""+number);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnPlus) {
- // do Plus stuff
- operator = "Plus";
- reset();
- op = true;
- equalsKey=false;
- } else if (e.getSource() == btnMinus) {
- // do Minus stuff
- operator = "Minus";
- reset();
- op = true;
- equalsKey=false;
- } else if (e.getSource() == btnTimes) {
- // do Times stuff
- operator = "Times";
- reset();
- op = true;
- equalsKey=false;
- } else if (e.getSource() == btnDivide) {
- // do Divide stuff
- operator = "Divide";
- reset();
- op = true;
- equalsKey=false;
- } else if (e.getSource() == btnMemory) {
- // do Memory stuff
- doMemoryStuff();
- equalsKey=false;
- } else if (e.getSource() == btnSquareRoot) {
- // do SquareRoot stuff
- doSquareRootStuff();
- equalsKey=false;
- } else if (e.getSource() == btnEquals) {
- // do Equals stuff
- performCalculation(operator);
- equalsKey=true;
- } else if (e.getSource() == btnCancelEntry) {
- // do CancelEntry stuff
- cancelEntry();
- equalsKey=false;
- } else if (e.getSource() == btnCancelAll) {
- // do CancelAll stuff
- cancelAll();
- equalsKey=false;
- } else if (e.getSource() == btnPoint) {
- // do Decimal Point stuff
- doDecimalPoint();
- op = false;
- equalsKey=false;
- } else {
- // do digit stuff
- doDigit(e);
- op = false;
- equalsKey=false;
- } // end if
- } // end actionPerformed()
- private JTextField tfDisplay;
- private JButton[] btnDigit;
- private JButton btnPoint;
- private JButton btnPlus, btnMinus, btnTimes, btnDivide;
- private JButton btnEquals, btnMemory, btnSquareRoot;
- private JButton btnCancelEntry, btnCancelAll;
- private double number=0;
- private double firstNum=0;
- private double secondNum=0;
- private double mem=0; // memory
- private String operator="";
- private boolean point=false;
- private boolean digit = false;
- private boolean op = false;
- private boolean equalsKey=false; // true when "=" button clicked
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement