

/*----------------------------------------------------------------------+
|      Title:	Calculator.java                                         |
|               A four-function calculator                              |
|                                                                       |
|      Author:	your name here                                          |
|           Clark University                                            |
|                                                                       |
|      Date:    April, 2002.                                            |
|                                                                       |
|      The are 17 buttons on this basic calculator: the ten digit       |
|      buttons, the four arithmetic operations, a +/- button for        |
|      negation, an entry button labelled =, a clear button             |
|      labelled C, and a decimal point button.                          |
+----------------------------------------------------------------------*/

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

public class Calculator0 extends JApplet implements ActionListener {

  // currentValue stores the number currently being displayed
  private int currentValue = 0;

  // previousValue stores the number that was displayed before
  private int previousValue = 0;

  // waitingForDigit is true when the user hasn't entered any
  // digits.  It's false after the first digit has been entered
  private boolean waitingForDigit = true;

  // the last operation has to be remembered while the user enters
  // the next operand. If it's '=', there's no operation to remember
  private char lastOperation = '=';

  // division by 0 can lead to a computational error, and the
  // calculator displays "Error" until the user presses the clear
  // button
  private boolean computationError = false;

  // The output field is where the currentValue is displayed
  private JTextField outputField;

  // A constant array for the buttons on the calculator
  private final String[] button =
    {"C"," "," ","/",
     "7","8","9","*",
     "4","5","6","+",
     "1","2","3","-",
     "0"," ","+/-","="};

  public void init() {
    // First layout the whole applet frame.  The outputField will
    // go at the top; the buttons in a buttonPanel at the bottom.
    Container contentPane = getContentPane();
    contentPane.setBackground(Color.lightGray);
    BorderLayout bLayout = new BorderLayout();
    bLayout.setHgap(20);
    bLayout.setVgap(20);
    contentPane.setLayout(bLayout);
    contentPane.add(new Label(""),BorderLayout.EAST);
    contentPane.add(new Label(""),BorderLayout.WEST);
    contentPane.add(new Label(""),BorderLayout.SOUTH);

    // A large font will be used for the outputField and
    // for the buttons
    Font font = new Font("Serif",Font.PLAIN,16);

    // Set up the outputField and add it to the contentPane
    outputField = new JTextField("0", 12);
    outputField.setBackground(Color.white);
    outputField.setFont(font);
    displayCurrentValue();
    // Put the outputField in a panel to add space around it
    JPanel topPanel = new JPanel();
    topPanel.add(outputField,BorderLayout.CENTER);
    topPanel.add(new Label(""),BorderLayout.WEST);
    topPanel.add(new Label(""),BorderLayout.EAST);
    contentPane.add(topPanel,BorderLayout.NORTH);

    // Prepare the buttonPanel
    JPanel buttonPanel = new JPanel();
    buttonPanel.setBackground(Color.lightGray);
    GridLayout gLayout = new GridLayout(5,4);
    gLayout.setHgap(20);
    gLayout.setVgap(20);
    buttonPanel.setLayout(gLayout);

    // Add all the buttons to the buttonPanel
    for (int i=0; i<button.length; ++i) {
      JButton theButton = new JButton(button[i]);
      theButton.addActionListener(this);
      theButton.setFont(font);
      theButton.setBackground(Color.white);
      buttonPanel.add(theButton);
    }

    // Finally, add the buttonPanel to the contentPane
    contentPane.add(buttonPanel);
  }//init

  public void actionPerformed(ActionEvent e)
  {
    String label = e.getActionCommand(); // The button label
    char first = label.charAt(0); // the first char of the label

    if (first == 'C') { // Clear button
      // When C is pressed while numbers are being entered
      // just clear the current value, but when pressed
      // after a computation, then everything should be reset
      // to an initial state
      if (waitingForDigit) { // Perform an "all clear"
        // $$$ put something here to reset the calculator
	// to an initial state !!!

      } // In any case, clear the display and error flag
      currentValue = 0;
      waitingForDigit = true;
      computationError = false;
    } else if (label.equals("+/-")) { // Negate button
      currentValue = -currentValue;
      waitingForDigit = true;
    } else if (first == ' ') { // Blank button
      ; // do nothing
    } else if ('0'<=first && first <='9') { // It's a digit
      int digit = first - '0'; // Convert to an int
      if (waitingForDigit) {
        currentValue = digit;
        waitingForDigit = false;
      } else
        // $$$ put something here so that numbers can be
        // entered properly !!!
        ;
    } else { // it's one of +,-,*,/, or =
      computeLastOperation();
      lastOperation = first;
      previousValue = currentValue;
      waitingForDigit = true;
    } //if/else
    displayCurrentValue();
  }//actionPerformed

  private void displayCurrentValue() {
    if (computationError)
      outputField.setText("  Error  ");
    else
      outputField.setText(Integer.toString(currentValue));
  }

  private void computeLastOperation() {
    switch (lastOperation) {
      case '=': // no last operation
        break;
      case '+':
        // $$$ put something here so that the + key works!!!

        break;
      case '-':
        // $$$ put something here so that the - key works!!!

        break;
      case '*':
        // $$$ put something here so that the * key works!!!

        break;
      case '/':
        if (currentValue==0)
          computationError = true;
        else
          currentValue = previousValue / currentValue;
        break;
    }//switch
  }//computeLastOperation
}//Calculator applet

/*----------------------------------------------------------------------+
|      This skeleton for the Calculator.java applet was prepared        |
|      by David Joyce, Dept. Math. & Comp. Sci., Clark University       |
|      April, 2002.                                                     |
+----------------------------------------------------------------------*/

