//============================================================ //Program: Calculator.java //helper program: Op_Type.java //Author: Daniel Broschart 6904 //Date: March 15, 1999 //Course: CS 161 //Description: A Java applet calculator with +,-,*,/, memory // operations, clear display, and factorial calculation // through iterative and recursive calculations. // This calculator allows you to change your mind about // which operation you wanted to perform, i.e. You can // click + but then click *, and the * operation will // be calculated when you hit the = key. // //Feature: If you enter a number and then choose an operation, // and then hit =, the number in the display will // perform the operation on itself, i.e. 9 * 9. // // //Note: Set tab spacing to 4 for this file. // I tried not to use tabs, but I think the IDE // might have inserted some anyway. //============================================================ import java.applet.Applet; import java.awt.*; public class Calculator extends Applet { final static int max_digits = 12; // max number of digits allowed //============================================== // Instantiation of buttons and variables. //============================================== private boolean decimal_set = false; // flag for decimal point private boolean op_set = false; // flag to know an operation was done private boolean dec_tag = false; private Op_Type operation = new Op_Type(); // defines operation for calculating private factorial r_factorial = new factorial(); private int num_digits = 0; // check for digits entered private float total; // total of calculation private float memory; // calculator memory private StringBuffer num_entered; // holds entered number until // it is converted to a double private GridBagLayout key_pad; // Area for keys private GridBagConstraints key_bounds; // bounds of GridBag private TextField display; // number area private Button num_keys[] = new Button[10]; // digits private Button op_keys[] = new Button[4]; // +, - , *, and / private Button mem_keys[] = new Button[4]; // memory keys and clear private Button decimal_key = new Button("."); private Button sign_key = new Button("(+|-)"); // negation key private Button equal_key = new Button("="); // Iterative factorial key private Button factorial_i_key = new Button("! iteratively"); // Recursive factorial key private Button factorial_r_key = new Button("! recursively"); private void init_buttons() { int i; for(i=0; i < 10; i++) { num_keys[i] = new Button(Integer.toString(i)); num_keys[i].setBackground(Color.white); }// set number keys i = 0; op_keys[i] = new Button("+"); // Plus Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("MS"); // Store Memory mem_keys[i].setBackground(Color.white); i++; op_keys[i] = new Button("-"); // Minus Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("M+"); // Add to Memory mem_keys[i].setBackground(Color.white); i++; op_keys[i] = new Button("X"); // Multiplication Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("MR"); // Recall Memory mem_keys[i].setBackground(Color.white); i++; op_keys[i] = new Button("/"); // Divide Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("Clear"); // Clear Memory mem_keys[i].setBackground(Color.white); i++; } private void addComponent(Component comp, GridBagLayout gbl, GridBagConstraints gbc, int row, int column, int width, int height) { // set gridx and gridy gbc.gridx = column; gbc.gridy = row; // set gridwidth and gridheight gbc.gridwidth = width; gbc.gridheight = height; // gbc.insets(3,3,3,3); // set internal padding to 3 x 3 gbl.setConstraints(comp, gbc); add(comp); } public void init () { int i; key_pad = new GridBagLayout(); // key_pad.setBackground(Color.blue); setLayout(key_pad); // set to applet setBackground(Color.black); key_bounds = new GridBagConstraints(); display = new TextField("0", 30); // set display to 30 chars display.setEditable(false); // don't allow people to alter display display.setBackground(Color.black); display.setForeground(Color.green); addComponent(display, key_pad, key_bounds, 0, 0, 4, 1); num_entered = new StringBuffer(""); init_buttons(); key_bounds.fill = GridBagConstraints.BOTH; for(i = 0; i < 4; i++)// Display memory keys { addComponent(mem_keys[i], key_pad, key_bounds, 1, i, 1, 1); } for(i = 7; i < 10; i++)// Display keys 7 to 9 { addComponent(num_keys[i], key_pad, key_bounds, 3, i-7, 1, 1); } for(i = 4; i < 7; i++)// Display keys 4 to 6 { addComponent(num_keys[i], key_pad, key_bounds, 4, i-4, 1, 1); } for(i = 1; i < 4; i++)// Display keys 1 to 3 { addComponent(num_keys[i], key_pad, key_bounds, 5, i-1, 1, 1); } int j = 6; for(i = 0; i < 4; i++)// Display operation keys { addComponent(op_keys[i], key_pad, key_bounds, j--, 3, 1, 1); } // Display 0 key addComponent(num_keys[0], key_pad, key_bounds, 6, 0, 1, 1); decimal_key.setBackground(Color.white); // Display decimal key addComponent(decimal_key, key_pad, key_bounds, 6, 1, 1, 1); sign_key.setBackground(Color.white); // Display +/- key addComponent(sign_key, key_pad, key_bounds, 6, 2, 1, 1); // Display factorial keys factorial_i_key.setBackground(Color.red); factorial_r_key.setBackground(Color.red); addComponent(factorial_i_key, key_pad, key_bounds, 7, 0, 3, 1); addComponent(factorial_r_key, key_pad, key_bounds, 7, 3, 1, 1); equal_key.setBackground(Color.green); // Display = key addComponent(equal_key, key_pad, key_bounds, 8, 0, 4, 1); } public boolean action(Event evt, Object obj) { int i; Float temp_float; if(evt.target instanceof Button) { // Check if not past digit limit if ( (num_digits < max_digits+1) && (!dec_tag) ) { if ( (!dec_tag) && (num_digits == max_digits) ) { dec_tag = true; } for (i = 0; i < 10; i ++) { if(evt.target == num_keys[i]) { if (op_set) { num_entered = new StringBuffer(""); op_set = false; } num_entered.insert(num_digits, num_keys[i].getLabel()); num_digits++; display.setText(num_entered.toString()); return true; } if ( (evt.target == decimal_key) && (!decimal_set)) { if (op_set) { num_entered = new StringBuffer(""); op_set = false; } num_entered.insert(num_digits, decimal_key.getLabel()); decimal_set = true; num_digits++; display.setText(num_entered.toString()); return true; } } // This is the end of the for loop. }// The end of the check if not past digit limit if (evt.target == sign_key) { // conversion procedures temp_float = new Float(num_entered.toString());// I used these float neg_float = temp_float.floatValue(); // to just keep neg_float *= -1; // multiplying by -1. temp_float = new Float(neg_float); // Too bad Floats can't do // primative actions. // set to negated number num_entered = new StringBuffer(temp_float.toString()); display.setText(num_entered.toString()); num_digits = max_digits+2; // don't allow to add numbers // num_entered = new StringBuffer(""); //num_digits = 0; dec_tag = false; return true; } if (evt.target == factorial_i_key) { temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); for(i = (int) total - 1; i > 0; i--) { total = total * i; } num_entered = new StringBuffer(Float.toString(total)); display.setText(num_entered.toString()); num_digits = 0; dec_tag = false; decimal_set = false; total = 0; op_set = true; } if (evt.target == factorial_r_key) { temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); total = r_factorial.factorial(total); num_entered = new StringBuffer(Float.toString(total)); display.setText(num_entered.toString()); total = 0; op_set = true; num_digits = 0; dec_tag = false; decimal_set = false; } if (evt.target == equal_key) // equal key operations { switch(operation.Which_Op()) { case 0: // add temp_float = new Float(num_entered.toString()); total += temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; case 1: // subtract temp_float = new Float(num_entered.toString()); total -= temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; case 2: // multiply temp_float = new Float(num_entered.toString()); total *= temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; case 3: // divide temp_float = new Float(num_entered.toString()); if(temp_float.floatValue() != 0) { total /= temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); } else { display.setText("Error. Division by 0"); num_entered = new StringBuffer(""); num_digits = 0; total = 0; dec_tag = false; return true; } break; default: temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; } display.setText(num_entered.toString()); op_set = true; num_digits = 0; dec_tag = false; decimal_set = false; return true; }// End of equal key operations if. for (i = 0; i < 4; i++) // calculator operations { if (evt.target == op_keys[i]) { switch (i) { case 0: // add operation.ADD_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; case 1: // subtract operation.SUB_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; case 2: // multiply operation.MULT_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; case 3: // divide operation.DIV_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; default: } num_entered = new StringBuffer(Float.toString(total)); display.setText(String.valueOf(num_entered)); decimal_set = false; op_set = true; num_digits = 0; dec_tag = false; return true; } if (evt.target == mem_keys[i]) { switch (i) { case 0: // Store Memory num_digits = max_digits+1; temp_float = new Float(num_entered.toString()); memory = temp_float.floatValue(); break; case 1: // Memory + num_digits = max_digits+1; temp_float = new Float(num_entered.toString()); memory += temp_float.floatValue(); break; case 2: // Memory Recall num_digits = max_digits+1; num_entered = new StringBuffer(String.valueOf(memory)); display.setText(Float.toString(memory)); break; case 3: // Clear num_digits = 0; total = 0; num_entered = new StringBuffer("0"); // clear display display.setText(num_entered.toString()); num_entered = new StringBuffer(""); // clear string break; default: break; } decimal_set = false; op_set = true; num_digits = 0; dec_tag = false; return true; } } // End of calculator operations for loop } // End of largest if: if(evt.target instanceof Button) return false; } } // End of Calculator Applet Class, END OF PROGRAM