Source of CalculatorGUIAppAWT.java


  1: //CalculatorGUIApp.java
  2: //Emulates a simple calculator.

  4: import java.awt.*;
  5: import java.awt.event.*;

  7: class Calculator
  8: extends Frame
  9: implements ActionListener
 10: {
 11:     private TextField display = new TextField(10);
 12:     private Panel keypad = new Panel();
 13:     private Button[] button = new Button[15];
 14:     private StringBuffer registerA = new StringBuffer();
 15:     private StringBuffer registerB = new StringBuffer();
 16:     private char operator;
 17:     private boolean firstNumberAlreadyInput = false;

 19:     static String[] keyLabels = {" 0 "," 1 "," 2 ",
 20:                                  " 3 "," 4 "," 5 ",
 21:                                  " 6 "," 7 "," 8 ",
 22:                                  " 9 "," + "," - ",
 23:                                  " = "," * "," / "};
 24:     //Constructor
 25:     public Calculator(String s)
 26:     {
 27:         super(s);
 28:         setBackground(Color.yellow);

 30:         //Set up display
 31:         setLayout(new FlowLayout(FlowLayout.CENTER));
 32:         display.setEditable(false);
 33:         add(display);

 35:         //Set up keypad
 36:         keypad.setLayout(new GridLayout(5,3));
 37:         for (int index=0; index!=15; index++)
 38:         {
 39:             button[index] = new Button(keyLabels[index]);
 40:             button[index].setBackground(Color.white);
 41:             button[index].addActionListener(this);
 42:             keypad.add(button[index]);
 43:         }
 44:         add(keypad);

 46:         addWindowListener
 47:         (
 48:             new WindowAdapter()
 49:             {
 50:                 public void windowClosing(WindowEvent e)
 51:                 {
 52:                     System.exit(0);
 53:                 }
 54:             }
 55:         );
 56:     }

 58: 
 59:     //Method to respond to whichever key has been pressed
 60:     public void actionPerformed(ActionEvent event)
 61:     {
 62:         final int positionOfEqualsKey = 12;
 63:         Object source = event.getActionCommand();

 65:         //Test for digit in the range 0-9
 66:         for (int digit = 0; digit != 10; digit++)
 67:         {
 68:             if (source.equals(keyLabels[digit]))
 69:             {
 70:                 if (firstNumberAlreadyInput)
 71:                 {
 72:                     registerB.append(String.valueOf(digit));
 73:                     display.setText(registerB.toString());
 74:                     return;
 75:                 }
 76:                 else
 77:                 {
 78:                     registerA.append(String.valueOf(digit));
 79:                     display.setText(registerA.toString());
 80:                     return;
 81:                 }
 82:             }
 83:         }

 85:         //Test for an operator
 86:         for (int positionOfOperator  = 10;
 87:                  positionOfOperator != 15;
 88:                  positionOfOperator++)
 89:         {
 90:             //Test for +, -, * or /
 91:             if (source.equals(keyLabels[positionOfOperator]) && 
 92:                (positionOfOperator != positionOfEqualsKey))
 93:             {
 94:                 operator = keyLabels[positionOfOperator].charAt(1);
 95:                 firstNumberAlreadyInput = true;
 96:                 return;
 97:             }
 98:         }

100:         //Test for =
101:         if (source.equals(" = "))
102:         {
103:             display.setText(doCalculation());
104:             registerA.setLength(0);
105:             registerB.setLength(0);        
106:             firstNumberAlreadyInput = false;
107:         }
108:     }

110: 
111:     private String doCalculation()
112:     {
113:         final char beep = '\u0007';
114:         try
115:         {
116:             int m = new Integer(registerA.toString()).intValue();
117:             int n = new Integer(registerB.toString()).intValue();
118:         
119:             switch (operator)
120:             {
121:                 case '+' : return String.valueOf(m+n);
122:                 case '-' : return String.valueOf(m-n);
123:                 case '*' : return String.valueOf(m*n);
124:                 case '/' : return String.valueOf(m/n);
125:                 default  : return "   E R R O R";
126:             }
127:         }
128:         catch(Exception e)
129:         {
130:             System.out.print(beep);
131:             return "   E R R O R";
132:         }
133:     }
134: }


137: class CalculatorGUIAppAWT
138: {
139:     public static void main(String[] args)
140:     {
141:         Calculator myCalculator = new Calculator("Calculator");
142:         myCalculator.setSize(150, 200);
143:         myCalculator.setVisible(true);
144:     }
145: }