teaching machines

CS 245 Lecture 5 – Calculator continued

September 17, 2013 by . Filed under cs245, fall 2013, lectures.

Agenda

TODO

Code

CalculatorGUI.java

package lecture0912;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalculatorGUI {
  private static double a = 0.0;
//  private static double b = 0.0;
  private static String operator = "+";

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(700, 500);

    // Panel
    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.CENTER);

    panel.setLayout(new GridLayout(0, 3));

    // Label
    final JLabel numberDisplay = new JLabel();
    frame.add(numberDisplay, BorderLayout.NORTH);
    numberDisplay.setText("0");
    numberDisplay.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 36));
    numberDisplay.setHorizontalAlignment(JLabel.RIGHT);

    JButton[] buttons = new JButton[20];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton();
      panel.add(buttons[i]);
    }

    // Digits.
    for (int i = 0; i < 10; i++) {
      buttons[i].setText("" + i);
      buttons[i].addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          JButton perpetrator = (JButton) e.getSource();

          if (numberDisplay.getText().equals("0")) {
            numberDisplay.setText(perpetrator.getText());
          } else {
            numberDisplay.setText(numberDisplay.getText() + perpetrator.getText());
          }
        }
      });
    }

    class OperatorListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
        // get two operands
        // and last operator
        // combine and store result

        double numberJustEntered = Double.parseDouble(numberDisplay.getText());

        if (operator.equals("+")) {
          a = a + numberJustEntered;
        } else if (operator.equals("-")) {
          a = a - numberJustEntered;
        }

        numberDisplay.setText("0");
        operator = ((JButton) e.getSource()).getText();
        // store current operator
      }
    }
    OperatorListener operatorListener = new OperatorListener();

    // Operators.
    buttons[10].setText("+");
    buttons[10].addActionListener(operatorListener);

    buttons[11].setText("-");
    buttons[10].addActionListener(operatorListener);

    buttons[12].setText("*");
    buttons[13].setText("/");
    buttons[14].setText("=");
    buttons[14].addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int numberJustEntered = Integer.parseInt(numberDisplay.getText());

        if (operator.equals("+")) {
          a = a + numberJustEntered;
        } else if (operator.equals("-")) {
          a = a - numberJustEntered;
        }

        numberDisplay.setText(a + "");
      }
    });

    buttons[15].setText(".");
    buttons[19].setText("Clr");

    // class ClearListener implements ActionListener {
    // @Override
    // public void actionPerformed(ActionEvent e) {
    // numberDisplay.setText("0");
    // }
    // }
    // buttons[19].addActionListener(new ClearListener());

    buttons[19].addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        numberDisplay.setText("0");
      }
    });
  }
}

Haiku

subtype vs. supertype
“Give me your money!”
“You sure? It’s Canadian.”
“Aww. Nevermind then.”