// Program Calculator.java // A basic calculator program that allows a user to // add, subtract, multiply, and divide integers ( dividing by 0 // produces an error message ). Note: when wishing to start a new // operation hit the clear button or you will end up doing // some operation on the contents of the current accumulator // ( i.e. if you added 14 + 30 = 44, you have 44 in the textfield // ... if you now try to hit * and then 3 you get 264 instead of // 132 because the accumulator adds 44 to 44 and then multiplies // by 3. You can only do one operation at a time. Do an operation // on two numbers, hit clear, and proceed again. ) import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Calculator extends Applet implements ActionListener { private String currentOperation, currentText; private int accumulator; private TextField display; private Button operator[], number[], clear; private GridBagLayout gbLayout; private GridBagConstraints gbConstraints; private String[] operatorNames = { "/", "*", "-", "=", "+" }; public void init() { // Initialize accumulator as 0 accumulator = 0; // Use a combination of border layout // and grid layout to implement the // calculator design // Start with a border layout setLayout( new BorderLayout() ); currentText = new String( "" ); currentOperation = new String( "" ); operator = new Button[5]; number = new Button[10]; display = new TextField(); display.setEditable( false ); // user must use the mouse to use the calculator clear = new Button( "Clear" ); // Make a panel that uses grid layout Panel calculatorPanel = new Panel(); // Operator buttons on the panel /, *, -, + for ( int i = 0; i < 5; i++ ) { operator[i] = new Button( operatorNames[i] ); operator[i].addActionListener( this ); } // Number buttons on the panel 0, 1, ..., 9 for ( int k = 0; k <= 9; k++ ) { number[k] = new Button( (Integer.toString( k ) ) ); number[k].addActionListener( this ); } // Panel with 4 rows and 4 columns calculatorPanel.setLayout( new GridLayout( 4, 4 ) ); // Add the buttons to the panel // Buttons 7 through 9 for ( int i = 7, x = 0; i <= 9; i++, x++ ) { calculatorPanel.add( number[i] ); } // Divide operator calculatorPanel.add( operator[0] ); // Buttons 4 through 6 for ( int i = 4, x = 0; i <= 6; i++, x++ ) { calculatorPanel.add( number[i] ); } // Multiplication operator calculatorPanel.add( operator[1] ); // Buttons 1 through 3 for ( int i = 1, x = 0; i <= 3; i++, x++ ) { calculatorPanel.add( number[i] ); } // Subtraction operator calculatorPanel.add( operator[2] ); // Button 0 calculatorPanel.add( number[0] ); // Clear textfield and accumulator button clear.addActionListener( this ); calculatorPanel.add( clear ); // Equals operator calculatorPanel.add( operator[3] ); // Addition operator calculatorPanel.add( operator[4] ); // Complete the Layout add( "North", display ); // North position add( "Center", calculatorPanel ); // Center position } public void actionPerformed( ActionEvent e ) { // The '+' button is pressed if ( e.getSource() == operator[4] ) { accumulator += Integer.parseInt( display.getText() ); currentOperation = "addition"; currentText = ""; display.setText( currentText ); } // The '-' button is pressed if ( e.getSource() == operator[2] ) { accumulator += Integer.parseInt( display.getText() ); currentOperation = "subtraction"; currentText = ""; display.setText( currentText ); } // The '*' button is pressed if ( e.getSource() == operator[1] ) { accumulator += Integer.parseInt( display.getText() ); currentOperation = "multiplication"; currentText = ""; display.setText( currentText ); } // The '\' button is pressed if ( e.getSource() == operator[0] ) { accumulator += Integer.parseInt( display.getText() ); currentOperation = "division"; currentText = ""; display.setText( currentText ); } // The '=' button is pressed if ( e.getSource() == operator[3] ) { if ( currentOperation == "addition" ) { accumulator += Integer.parseInt( display.getText() ); currentText = Integer.toString( accumulator ); } if ( currentOperation == "subtraction" ) { accumulator -= Integer.parseInt( display.getText() ); currentText = Integer.toString( accumulator ); } if ( currentOperation == "multiplication" ) { accumulator *= Integer.parseInt( display.getText() ); currentText = Integer.toString( accumulator ); } if ( currentOperation == "division" ) { if ( ( Integer.parseInt( display.getText() ) ) == 0 ) { currentText = "Can't divide by Zero. Hit Clear"; } else { accumulator /= Integer.parseInt( display.getText() ); currentText = Integer.toString( accumulator ); } } display.setText( currentText ); } // Handle the case where a 'number' is pressed for ( int i = 0; i <= 9; i++ ) { if ( e.getSource() == number[i] ) { currentText = currentText.concat( Integer.toString( i ) ); display.setText( currentText ); } } // The 'clear' button is pressed if ( e.getSource() == clear ) { currentText = ""; accumulator = 0; display.setText( currentText ); } } }