////////////////////////////// // // // CPS406 Fall 1998 // // Calculator Applet v2.0 // // javaCalc.java // // // // Matthew Narrell // // // ////////////////////////////// import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class javaCalc extends Applet{ ///////////////////////////////////////////////////////////////////////////// // // Instance Variables representing the calculator buttons. // ///////////////////////////////////////////////////////////////////////////// //Numbers private Button button0, button1, button2, button3, button4; private Button button5, button6, button7, button8, button9; //Operations private Button add, sub, mult, div, equ, clear; ///////////////////////////////////////////////////////////////////////////// // // Accumulator variable and it's accessor and mutator. // ///////////////////////////////////////////////////////////////////////////// private int accum = 0; public void setAccum(int i){ accum = i; } public int getAccum(){ return accum; } ///////////////////////////////////////////////////////////////////////////// // // Temporary storage variable of type string for the operation to be // performed. Accessor and mutator follow. // ///////////////////////////////////////////////////////////////////////////// private String op = null; public void setOp(String s){ op = s; } public String getOp(){ return op; } ///////////////////////////////////////////////////////////////////////////// // // Calculator display. // ///////////////////////////////////////////////////////////////////////////// TextField display; ///////////////////////////////////////////////////////////////////////////// // // Initialization method. // ///////////////////////////////////////////////////////////////////////////// public void init() { setLayout(new BorderLayout()); //Create the display display = new TextField(20); display.setEditable(false); add(display, BorderLayout.NORTH); button0 = new Button("0"); //Create the number buttons button1 = new Button("1"); button2 = new Button("2"); button3 = new Button("3"); button4 = new Button("4"); button5 = new Button("5"); button6 = new Button("6"); button7 = new Button("7"); button8 = new Button("8"); button9 = new Button("9"); NumberHandler number = new NumberHandler(this); button0.addActionListener(number); //Assign each "number press" button1.addActionListener(number); //an action button2.addActionListener(number); button3.addActionListener(number); button4.addActionListener(number); button5.addActionListener(number); button6.addActionListener(number); button7.addActionListener(number); button8.addActionListener(number); button9.addActionListener(number); clear = new Button("C"); //Create the "clear" button ClearHandler clearText = new ClearHandler(this); clear.addActionListener(clearText); //Assign the "clear" operation add = new Button("+"); //Create the operator buttons sub = new Button("-"); mult = new Button("*"); div = new Button("/"); OpHandler op = new OpHandler(this); add.addActionListener(op); //Assign the associated sub.addActionListener(op); //operations with the operator mult.addActionListener(op); //buttons div.addActionListener(op); equ = new Button("="); //Create the "equals" button EqualHandler equals = new EqualHandler(this); equ.addActionListener(equals); //Assign the "equals" operation /////////////////////////////////////////////////////////////////////////// // // Create the button panel and layout the buttons similar to common // calculators. // /////////////////////////////////////////////////////////////////////////// Panel buttonsPanel = new Panel(); buttonsPanel.setLayout(new GridLayout(4,1)); buttonsPanel.add(button9); buttonsPanel.add(button8); buttonsPanel.add(button7); buttonsPanel.add(add); buttonsPanel.add(button6); buttonsPanel.add(button5); buttonsPanel.add(button4); buttonsPanel.add(sub); buttonsPanel.add(button3); buttonsPanel.add(button2); buttonsPanel.add(button1); buttonsPanel.add(mult); buttonsPanel.add(clear); buttonsPanel.add(button0); buttonsPanel.add(equ); buttonsPanel.add(div); add(buttonsPanel, BorderLayout.CENTER); // Button credit = new Button("Credits"); // creditHandler cred = new creditHandler(this); // credit.addActionListener(cred); // add(credit, BorderLayout.SOUTH); } ///////////////////////////////////////////////////////////////////////////// // // Private class to handle the action performed when a number is pressed. // ///////////////////////////////////////////////////////////////////////////// class NumberHandler implements ActionListener{ javaCalc applet; public NumberHandler(javaCalc a){ applet = a; } public void actionPerformed(ActionEvent e){ //Get the current value of the textfield. String s = applet.display.getText(); //Record which number was pressed. String t = ( (Button)e.getSource() ).getLabel(); if(s.length() == 1){ if( s.charAt(0) == '0' ) applet.display.setText(t); else applet.display.setText(s + t); } else if (s.length() == 20) //If the numbers fill the textfield, popUp.overFlow(); //Pop Up the Overflow error dialog. else //Append old textfield with new number and re-display. applet.display.setText(s + t); } } ///////////////////////////////////////////////////////////////////////////// // // Private class to handle the action performed when the "clear" button is // pressed. // ///////////////////////////////////////////////////////////////////////////// class ClearHandler implements ActionListener{ javaCalc applet; public ClearHandler(javaCalc a){ applet = a; } public void actionPerformed(ActionEvent e){ //Clear the accumulator via mutator. applet.setAccum(0); //Clear the textfield. applet.display.setText(null); } } ///////////////////////////////////////////////////////////////////////////// // // Private class to handle the action performed when an operator button is // pressed. This method first records the value of the textfield, operand1, // to the accumulator, then records which operation is to be done. // ///////////////////////////////////////////////////////////////////////////// class OpHandler implements ActionListener{ javaCalc applet; public OpHandler(javaCalc a){ applet = a; } public void actionPerformed(ActionEvent e){ //Set accumulator to integer value of textfield via mutator. applet.setAccum(Integer.parseInt(applet.display.getText())); //Set operator variable to which operation was indicated. applet.setOp( ( (Button)e.getSource() ).getLabel() ); //Clear the textfield for operand2. applet.display.setText(null); } } ///////////////////////////////////////////////////////////////////////////// // // Private class to handle action performed when the "equals" button is // pressed. // ///////////////////////////////////////////////////////////////////////////// class EqualHandler implements ActionListener{ javaCalc applet; public EqualHandler(javaCalc a){ applet = a; } public void actionPerformed(ActionEvent e){ //Determine which operation is to be performed. String operator = applet.getOp(); //Prepare operand1. int op1 = applet.getAccum(); //Fetch operand2. int op2 = Integer.parseInt(applet.display.getText()); //Result of calculation. int result = 0; ///////////////////////////////////////////////////////////////////////// // // Determine which operation was specified, perform such operation on // the operands, and display the result. // ///////////////////////////////////////////////////////////////////////// if( operator == "+" ){ result = op1 + op2; applet.display.setText(Integer.toString(result)); } else if( operator == "-" ){ result = op1 - op2; applet.display.setText(Integer.toString(result)); } else if( operator == "/" ){ if(op2 == 0) //If the divisor is negative, popUp.divByZero(); //Pop Up the Division by Zero error //dialog. else{ if(op1 < op2) //If the result is < 1, Pop Up the popUp.intDiv(); //Integer Division error dialog. else if( (op1 % op2) != 0 ) //If the result yeilds a rational popUp.intDiv(); //result, Pop Up the Integer Division } //error dialog. result = op1 / op2; applet.display.setText(Integer.toString(result)); } else if( operator == "*" ){ result = op1 * op2; applet.display.setText(Integer.toString(result)); } } } } class creditHandler implements ActionListener{ javaCalc applet; public creditHandler(javaCalc a){ applet=a; } public void actionPerformed(ActionEvent e){ popUp.credits(); applet.stop(); } } /////////// // // // END // // // ///////////