import java.applet.Applet; import java.awt.*; public class calclet extends Applet { // Setup our operators // OP_BASE provided to separate operators from digits. Operator value // should be OP_BASE + index of operator key in myLabels[] private static final int OP_BASE = 90; private static final int ADD = OP_BASE + 10; private static final int EQU = OP_BASE + 11; private static final int SUB = OP_BASE + 12; private static final int MUL = OP_BASE + 13; private static final int DIV = OP_BASE + 14; // setup possible calculator states... private static final int V = 0; private static final int VO = 1; private static final int VOV = 2; private static final int VOVE = 3; private static String myLabels[] = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+", "=", "-", "*", "/" }; // Setup initial calculator values and states private int value1 = 0; private int state = V; private int Op = EQU; private int displayValue = 0; TextField display; /** * calclet() * Our Constructor--just builds the interface... */ public void init() { setLayout(new BorderLayout()); Button[] calcButtons = new Button[15]; Panel displayPanel = new Panel(); Panel keyPanel = new Panel(); keyPanel.setLayout(new GridLayout(5,3)); // Add buttons to their own panel for (int i=0; i < myLabels.length; i++) { calcButtons[i] = new Button(myLabels[i]); keyPanel.add(calcButtons[i]); } // Add text fields display = new TextField(Integer.toString(displayValue),10); display.setEditable(false); displayPanel.add(display); // Add the panels add("North",displayPanel); add("Center",keyPanel); } /** * start * Show our frame */ public void start() { resize(100,200); show(); } // show the current display value public void updateDisplay() { display.setText(Integer.toString(displayValue)); } // add the next digit to the display value public void addDisplay(int i) { displayValue = (displayValue * 10) + i; updateDisplay(); } // set the display value and update the display public void setDisplay(int i) { displayValue = i; updateDisplay(); } // perform the operation requested on the saved value (value1) and // the current value (displayValue) // Then, update the display public void doOp() { switch (Op) { case EQU: break; case ADD: displayValue += value1; updateDisplay(); break; case SUB: displayValue = value1 - displayValue; updateDisplay(); break; case MUL: displayValue *= value1; updateDisplay(); break; case DIV: if (displayValue == 0) { display.setText("DIV ZER ERR"); } else { displayValue = value1 / displayValue; updateDisplay(); } break; } } /** * Find the key in the Labels list and return an int for it */ private int key(String k) { int i; for (i=0; i<15; i++) { if (myLabels[i].equals(k)) break; } return (i<10 ? Integer.parseInt(k) : i+OP_BASE); } public boolean handleEvent(Event evt) { if (evt.target instanceof Button) { int which = key(((Button)evt.target).getLabel()); switch (which) { // Check the digit keys... case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: // If we're typing in a number, keep going if ((state == V) || (state == VOV)) { addDisplay(which); } // We're typing in the second number, so save the current // value and reset the display else if (state == VO) { value1 = displayValue; setDisplay(which); state = VOV; } // We're starting over from scratch, so reset the display else { setDisplay(which); state = V; } break; // Now check for an operator case ADD: case SUB: case MUL: case DIV: // We have two values, already, so perform the stored operation // and the set the operator to the newly pressed operator if (state == VOV) { doOp(); } Op = which; state = VO; break; // Now check for the "=" key case EQU: // Do we have two values? if so, perform the operation // Otherwise, leave the display alone if (state == VOV) { doOp(); } // Set the state of the calculator to its final state (The // "=" key has been pressed, so the next operation will be // starting fresh Op = EQU; state = VOVE; break; } return true; } return false; } }