Calculater.java import java.awt.*; import java.awt.event.*; import java.awt.Toolkit; import java.applet.*; import java.lang.Object; import java.lang.*; /** * Define our applet class. It extends the Applet object */ public class Calculator extends Applet{ // // define and create the objects that make up the interface // BorderLayout borderLayout = new BorderLayout(10,10); // 10 pixels of padding private TextField accumulator = new TextField("",25); private KeyPad keyPad = new KeyPad(this); private float answer = 0; // holds intermediate answers private float accumulatorValue = 0; // holds value of accumulator after conversion private char lastOperation = ' '; // remembers last operation key private boolean startNewNumber = false; // tells the addDigit routine to start a new number /** * Constructor the applet */ public Calculator() { } /** * The following methods initialize the applet and are * invoked automatically. */ public void init(){ // // first set the applets layout manager // setLayout(borderLayout); // // add items to the layout // add("North",accumulator); // text box at top to enter operands and display answer add("Center",keyPad); // keypad, this is a grid panel } /** * Add a digit to the window */ public void addDigit(String c) { String text; if(startNewNumber) // this is true after a operation button is pressed. text = ""; // it allows the answer to be displayed until the else // first digit of the next operand is entered text = accumulator.getText(); text += c; // add the character pressed accumulator.setText(text); // change value of accumulator startNewNumber = false; // clear this to allow building a number } /** * convert input text to a number */ public boolean convertInput() { String text; text = accumulator.getText(); // get what is in the text box try { accumulatorValue = (Float.valueOf(text).floatValue());} // convert string to a float catch(NumberFormatException e) // if its a bad number do something here { accumulatorValue = 0; answer = 0; accumulator.setText("Error"); return false; // let the caller know a bad value was entered } return true; } /** * process display a result */ public void equal() { if(convertInput()){ // convert accumulator to a float switch (lastOperation){ // modify answer variable based on last operation case '+': answer+= accumulatorValue; break; case '-': answer-= accumulatorValue; break; case '*': answer = answer*accumulatorValue; break; case '/': answer = answer /accumulatorValue; break; default: answer = accumulatorValue; break; } accumulator.setText(String.valueOf(answer)); // set accumulator to show answer lastOperation = ' '; } startNewNumber = true; // tell addDigit to clear on the next digit } /** * proces a + - * / operation */ public void operation(char c) { equal(); // display the result lastOperation = c; // store the operation for later } /** * process a clear */ public void clear() { accumulator.setText(""); answer = 0; } /** * process a negate */ public void negate() { String text; text = accumulator.getText(); if(text.charAt(0) == '-') { text = text.substring(1); accumulator.setText(text); } else { text = "-" + accumulator.getText(); accumulator.setText(text); } } /** * process a backspace */ public void backSpace() { String text; int length; if(startNewNumber) { accumulator.setText(""); startNewNumber = false; return; } text = accumulator.getText(); // get contents length = text.length(); // determine the number of characters if(length != 0) // do nothing if its empty accumulator.setText(text.substring(0,length-1)); // remove last character } } ------------------------------------------------------------------------ KeyPad.java import java.awt.*; import java.awt.event.*; // // keypad is a panel object that has a grid of buttons // public class KeyPad extends Panel implements ActionListener { GridLayout gridLayout = new GridLayout(4,5); // create the gridlayout with 20 buttons Calculator calculator; // place to store the calculator object /** * constructor fo rthe keypad */ public KeyPad(Calculator theCalculator) { calculator = theCalculator; // remember calculator object so we can send it messages int i, nComponents; // // add all the keypad buttons // add(new Button("7")); add(new Button("8")); add(new Button("9")); add(new Button("/")); add(new Button("*")); add(new Button("4")); add(new Button("5")); add(new Button("6")); add(new Button("+")); add(new Button("-")); add(new Button("1")); add(new Button("2")); add(new Button("3")); add(new Button(".")); add(new Button("=")); add(new Button("0")); add(new Button("+/-")); add(new Button(".")); add(new Button("C")); add(new Button("<-")); this.setLayout(gridLayout); // // add an actionListener for each button // nComponents = getComponentCount(); for(i=0; i < nComponents; i++) ((Button)getComponent(i)).addActionListener(this); } /** * this method is called whenever a button is * pressed. Figure out what was pressed then do it */ public void actionPerformed(ActionEvent e){ // // first make sure that it was a button that sent this // event // if(e.getSource() instanceof Button){ // // Now determine which button. Ask it for its label // String label = ((Button)e.getSource()).getLabel(); switch(label.charAt(0)) { case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : case '.' : calculator.addDigit(label); // tell calculator a digit was pressed break; case '+' : if(label.compareTo("+/-") == 0) calculator.negate(); else calculator.operation('+'); break; case '*' : case '/' : case '-' : calculator.operation(label.charAt(0)); break; case '=' : calculator.equal(); break; case 'C' : calculator.clear(); break; case '<' : calculator.backSpace(); break; } }// end if (e.getSource } }