import java.applet.Applet; import java.awt.*; import java.awt.event.*; //A simple calculator, containing a +, -, /, *, Clear and Equal button. It is an exercise in event handling and if then loops public class SimpleCalc extends Applet implements ActionListener { //Declare all the variables and buttons for the form Label Prompt1, Prompt2; TextField value1, value2; Button addButton; Button subButton; Button divButton; Button multButton; Button clrButton; Button equButton; double num1, num2, result, temp1, temp2; int addFlag = 0, subFlag = 0 , multFlag = 0, divFlag = 0, clrFlag = 0, errFlag = 0; // for checking when a button is presses String s1, s2; public void init() { // add the buttons and Prompts to the init() addButton = new Button("+"); add (addButton); subButton = new Button("-"); add (subButton); divButton = new Button("/"); add (divButton); multButton = new Button("*"); add (multButton); clrButton = new Button("Clear"); add (clrButton); equButton = new Button("="); add (equButton); Prompt1 = new Label("Type your first value here"); add (Prompt1); value1 = new TextField(10); add (value1); Prompt2 = new Label("Type your second value here"); add (Prompt2); value2 = new TextField(10); add (value2); addButton.addActionListener(this); subButton.addActionListener(this); divButton.addActionListener(this); multButton.addActionListener(this); clrButton.addActionListener(this); equButton.addActionListener(this); } public void actionPerformed (ActionEvent event) { // if the button has been pressed, then its flag is set to 1, and in the next method.... if (event.getSource() == addButton){ addFlag = 1; } else if (event.getSource() == subButton){ subFlag = 1; } else if (event.getSource() == divButton){ divFlag = 1; } else if (event.getSource() == multButton){ multFlag = 1; } else if (event.getSource() == clrButton){ value1.setText(""); value2.setText(""); clrFlag = 1; repaint(); } else if (event.getSource() == equButton){ //check to see if input is a number, not a string. If it is a string, then set errFlag to 1 try { num1 = Integer.parseInt(value1.getText()); num2 = Integer.parseInt(value2.getText()); } catch (NumberFormatException e) { errFlag = 1; } repaint(); //process the button handling section when pressed } } public void paint (Graphics g) // we check the value of the various flags, and then do the arithmetic { if (errFlag == 1) { //if errFlag is 1 then the user fucked up somewhere g.drawString("Please make sure values are numbers only", 25, 100); errFlag = 0; } else if (addFlag == 1){ result = num1 + num2; g.drawString("Answer : " + result, 25, 100); result = 0; }else if (subFlag == 1){ result = num1 - num2; g.drawString("Answer : " + result, 25, 100); result = 0; }else if (divFlag == 1){ result = num1 / num2; g.drawString("Answer : " + result, 25, 100); result = 0; }else if (multFlag == 1){ result = num1 * num2; g.drawString("Answer : " + result, 25, 100); result = 0; }else if (clrFlag == 1){ result = 0; num1 = 0; num2 = 0; addFlag = 0; subFlag = 0; divFlag = 0; multFlag = 0; clrFlag = 0; g.drawString("Answer : " + result, 25, 100); } result = 0; num1 = 0; num2 = 0; addFlag = 0; subFlag = 0; divFlag = 0; multFlag = 0; clrFlag = 0; } }