import java.applet.*; import java.awt.*; public class KeyPad1 extends Panel { TextField display; // Readout Panel keys; // Key Pad int maxLength = 20; // Max length of Readout String output = "0"; // String to be displayed in // the readout boolean decimal = false; // Has the user inserted a decimal // into the display, yet? (Only // one allowed in a valid number) float result=0.0f; // Result of calc. String function=""; // Function currently being performed boolean newNumber = true; // Ready to start a new number? boolean finished = false; // Finished with current calc. ? // (When "=" key pressed) boolean memory = false; // Is there a number in memory? float memoryValue = 0.0f; // Value being held in memory public KeyPad1() { setLayout( new BorderLayout() ); setFont( new Font("Helvetica", Font.PLAIN, 12) ); setBackground(Color.lightGray); display = new TextField(maxLength+1); display.setEditable(false); display.setFont( new Font("Helvetica", Font.PLAIN, 12) ); display.setBackground(Color.white); keys = new Panel(); keys.setLayout( new GridLayout(5,5) ); keys.setFont( new Font("Helvetica", Font.PLAIN, 12) ); keys.setBackground(Color.lightGray); keys.add( new Button("+/-") ); keys.add( new Button("") ); keys.add( new Button("") ); keys.add( new Button("") ); keys.add( new Button("AC") ); keys.add( new Button("M+") ); keys.add( new Button("7") ); keys.add( new Button("8") ); keys.add( new Button("9") ); keys.add( new Button("/") ); keys.add( new Button("M-") ); keys.add( new Button("4") ); keys.add( new Button("5") ); keys.add( new Button("6") ); keys.add( new Button("x") ); keys.add( new Button("MR") ); keys.add( new Button("1") ); keys.add( new Button("2") ); keys.add( new Button("3") ); keys.add( new Button("-") ); keys.add( new Button("MC") ); keys.add( new Button("0") ); keys.add( new Button(".") ); keys.add( new Button("=") ); keys.add( new Button("+") ); add("North", display); // Readout add("Center", new Label("")); // Blank Space add("South", keys); // Number Pad display.resize( display.preferredSize() ); keys.resize( keys.preferredSize() ); updateDisplay(); // Call my display function show(); } // End init() public void updateDisplay() { String output_right = ""; // Right Justify the Readout for(int i=1; i<=(maxLength-output.length()); i++) { if ((i==1) && (memory)) output_right = output_right + "M"; else output_right = output_right + "_"; } output_right = output_right + output; display.setText(output_right); } // End updateDisplay public void appendDigit(String new_d) { if (output == "0") // If numbers are added, we don't output = ""; // want to maintain the leading 0 if (output.length() < maxLength) { if (newNumber) // If starting a new number { output = new_d; // Make this the first digit newNumber = false; // (then it is no longer new) } else output = output + new_d; // Else just add another updateDisplay(); // Print display, right justified } if (finished || function == "") // If the last calculation was finished, then by { // entering a new digit, we have started a new // calculation result = Float.valueOf(output).floatValue(); finished = false; function = ""; } } // End appendDigit() public void key_equals() { evaluate(); function = ""; // Reset for a new number newNumber = true; decimal = false; finished = true; } // End = public void evaluate() { // Perform current function if (function == "plus") result += Float.valueOf(output).floatValue(); else if (function == "minus") result -= Float.valueOf(output).floatValue(); else if (function == "times") result *= Float.valueOf(output).floatValue(); else if (function == "div") result /= Float.valueOf(output).floatValue(); else result = Float.valueOf(output).floatValue(); // If "=" pressed, setting finished to "true," and we if (finished == true) // then receive another function key press, we should finished = false; // continue working our last result // And reset for a new number output = Float.toString(result); updateDisplay(); newNumber = true; decimal = false; } // End evaluate() public boolean handleEvent(Event evt) { if (evt.arg == "AC") { result = 0; output = Float.toString(result); function = ""; // Reset for a new number/function newNumber = true; decimal = false; finished = true; updateDisplay(); } if (evt.arg == "+/-") { // Flip sign of displayed number float val = Float.valueOf(output).floatValue(); val *= -1; output = Float.toString(val); updateDisplay(); } if (evt.arg == "1") appendDigit("1"); if (evt.arg == "2") appendDigit("2"); if (evt.arg == "3") appendDigit("3"); if (evt.arg == "4") appendDigit("4"); if (evt.arg == "5") appendDigit("5"); if (evt.arg == "6") appendDigit("6"); if (evt.arg == "7") appendDigit("7"); if (evt.arg == "8") appendDigit("8"); if (evt.arg == "9") appendDigit("9"); if (evt.arg == "0") if (output.length() != 0) appendDigit("0"); if (evt.arg == ".") if (output.length() < maxLength) if (!decimal) { decimal = true; if (output.length() == 0 || newNumber) { output = "0."; newNumber = false; } else output = output + "."; updateDisplay(); // Print display, right justified } if (evt.arg == "=") // Evaluate and print results key_equals(); if (evt.arg == "+") { evaluate(); // Clean up last calculation function = "plus"; // and set up for new one } if (evt.arg == "-") { evaluate(); // Clean up last calculation function = "minus"; // and set up new one } if (evt.arg == "x") { evaluate(); // Clean up last calculation function = "times"; // and set up new one } if (evt.arg == "/") { evaluate(); // Clean up last calculation function = "div"; // and set up new one } if (evt.arg == "M+") { memory = true; // Flag our memory value key_equals(); // Update result as if "=" has been pressed // and store the result in memory memoryValue += Float.valueOf(output).floatValue(); updateDisplay(); } if (evt.arg == "M-") { memory = true; // Flag our memory value key_equals(); // Update result as if "=" has been pressed // and store the result in memory memoryValue -= Float.valueOf(output).floatValue(); updateDisplay(); } if (evt.arg == "MR") { if (memory) { output = Float.toString(memoryValue); updateDisplay(); } } if (evt.arg == "MC") { memory = false; memoryValue = 0.0f; updateDisplay(); } if (output == "") // If no numbers are added, we can output = "0"; // replace the lone 0 return false; } // End handleEvent } // Applet