import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
int currentValue = 0;
int operand1 = 0;
int operand2 = 0;
int result; // used to hold the calculated result
int operation; // 1 = add, 2 = subtract etc
TextField calculatorDisplay = new TextField(" Hi There ", 8);
Panel numPanel = new Panel();
Panel opsPanel = new Panel();
Panel arrPanel = new Panel();
Button butArray[] = new Button[10];
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button plus = new Button("+");
Button minus = new Button("-");
Button times = new Button("*");
Button divide = new Button("/");
Button equals = new Button("=");
Button clear = new Button("clear");
public void init()
{
numPanel.setLayout(new GridLayout(4,3,5,5));
add(calculatorDisplay);
numPanel.add(one);
numPanel.add(two);
numPanel.add(three);
numPanel.add(four);
numPanel.add(five);
numPanel.add(six);
numPanel.add(seven);
numPanel.add(eight);
numPanel.add(nine);
numPanel.add(zero);
opsPanel.setLayout(new GridLayout(2,4, 5, 5));
opsPanel.add(plus);
opsPanel.add(minus);
opsPanel.add(times);
opsPanel.add(divide);
opsPanel.add(equals);
add(numPanel);
add(opsPanel);
add(clear);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
clear.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
times.addActionListener(this);
divide.addActionListener(this);
equals.addActionListener(this);
arrPanel.setLayout(new GridLayout(4,3,5,5));
for (int index = 0; index < 10; index++)
{
butArray[index] = new Button(Integer.toString(index));
arrPanel.add(butArray[index]);
butArray[index].addActionListener(this);
}
add(arrPanel);
currentValue = 0;
}
public void actionPerformed(ActionEvent e)
{
for (int index = 0; index < 10; index++)
{
if (e.getSource() == butArray[index])
{
currentValue = currentValue * 10 + index;
calculatorDisplay.setText(Integer.toString(currentValue));
}
}
if(e.getSource() == one)
{
currentValue = currentValue * 10 + 1;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == two)
{
currentValue = currentValue * 10 + 2;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == three)
{
currentValue = currentValue * 10 + 3;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == four)
{
currentValue = currentValue * 10 + 4;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == five)
{
currentValue = currentValue * 10 + 5;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == six)
{
currentValue = currentValue * 10 + 6;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == seven)
{
currentValue = currentValue * 10 + 7;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == eight)
{
currentValue = currentValue * 10 + 8;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == nine)
{
currentValue = currentValue * 10 + 9;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if(e.getSource() == zero)
{
currentValue = currentValue * 10 + 0;
calculatorDisplay.setText(Integer.toString(currentValue));
}
else if (e.getSource() == clear)
{
calculatorDisplay.setText("0");
currentValue = 0;
operand1 = 0;
operand2 = 0;
}
else if (e.getSource() == plus)
{
operand1 = currentValue;
currentValue = 0;
calculatorDisplay.setText("0");
operation = 1;
}
else if (e.getSource() == minus)
{
operand1 = currentValue;
currentValue = 0;
calculatorDisplay.setText("0");
operation = 2;
}
else if (e.getSource() == times)
{
operand1 = currentValue;
currentValue = 0;
calculatorDisplay.setText("0");
operation = 3;
}
else if (e.getSource() == divide)
{
operand1 = currentValue;
currentValue = 0;
calculatorDisplay.setText("0");
operation = 4;
}
else if (e.getSource() == equals)
{
operand2 = currentValue;
currentValue = 0;
switch (operation)
{
case 1: result = operand1 + operand2; break;
case 2: result = operand1 - operand2; break;
case 3: result = operand1 * operand2; break;
case 4: result = operand1 / operand2; break;
}
calculatorDisplay.setText(Integer.toString(result));
operand1 = operand2 = 0;
}
}
public void paint(Graphics g)
{
setForeground(Color.blue);
setBackground(Color.yellow);
g.drawString(" " + operand1 + " " + operand2, 0, 380);
}
}
|