import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AdderApplet extends Applet
implements ActionListener
{
public void init()
{
setLayout(new BorderLayout());
Panel textPanel = new Panel();
textPanel.setBackground(Color.gray);
textPanel.setLayout(new FlowLayout());
inputOutputField = new TextField("0", 20);
inputOutputField.setBackground(Color.purple);
textPanel.add(inputOutputField);
add(textPanel, "North");
Panel platformPanel = new Panel();
platformPanel.setLayout(new GridLayout(4, 5));
Panel button1Panel = new Panel();
button1Panel.setBackground(Color.black);
Button sevenButton = new Button(" 7 ");
sevenButton.addActionListener(this);
button1Panel.add(sevenButton);
Button eightButton = new Button(" 8 ");
eightButton.addActionListener(this);
button1Panel.add(eightButton);
Button nineButton = new Button(" 9 ");
nineButton.addActionListener(this);
button1Panel.add(nineButton);
Button divideButton = new Button(" / ");
divideButton.addActionListener(this);
button1Panel.add(divideButton);
platformPanel.add(button1Panel);
Panel button2Panel = new Panel();
button2Panel.setBackground(Color.black);
Button fourButton = new Button(" 4 ");
fourButton.addActionListener(this);
button2Panel.add(fourButton);
Button fiveButton = new Button(" 5 ");
fiveButton.addActionListener(this);
button2Panel.add(fiveButton);
Button sixButton = new Button(" 6 ");
sixButton.addActionListener(this);
button2Panel.add(sixButton);
Button multiplyButton = new Button(" * ");
multiplyButton.addActionListener(this);
button2Panel.add(multiplyButton);
platformPanel.add(button2Panel);
Panel button3Panel = new Panel();
button3Panel.setBackground(Color.black);
Button oneButton = new Button(" 1 ");
oneButton.addActionListener(this);
button3Panel.add(oneButton);
Button twoButton = new Button(" 2 ");
twoButton.addActionListener(this);
button3Panel.add(twoButton);
Button threeButton = new Button(" 3 ");
threeButton.addActionListener(this);
button3Panel.add(threeButton);
Button minusButton = new Button(" - ");
minusButton.addActionListener(this);
button3Panel.add(minusButton);
platformPanel.add(button3Panel);
Panel button4Panel = new Panel();
button4Panel.setBackground(Color.black);
Button zeroButton = new Button(" 0 ");
zeroButton.addActionListener(this);
button4Panel.add(zeroButton);
Button dotButton = new Button(" . ");
dotButton.addActionListener(this);
button4Panel.add(dotButton);
Button addButton = new Button(" + ");
addButton.addActionListener(this);
button4Panel.add(addButton);
Button equalsButton = new Button(" = ");
equalsButton.addActionListener(this);
button4Panel.add(equalsButton);
platformPanel.add(button4Panel);
add(platformPanel, "Center");
Panel clearPanel = new Panel();
clearPanel.setBackground(Color.blue);
Button clearButton = new Button(" CLEAR ");
clearButton.addActionListener(this);
clearPanel.add(clearButton);
add(clearPanel, "South");
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(" 7 "))
{
numString = numString + "7";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 8 "))
{
numString = numString + "8";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 9 "))
{
numString = numString + "9";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 4 "))
{
numString = numString + "4";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 5 "))
{
numString = numString + "5";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 6 "))
{
numString = numString + "6";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 1 "))
{
numString = numString + "1";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 2 "))
{
numString = numString + "2";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" 3 "))
{
numString = numString + "3";
inputOutputField.setText(numString);
flag = "y";
} else
if(e.getActionCommand().equals(" - "))
{
equalBuffer = "";
calcIt();
buttonBuffer = "-";
} else
if(e.getActionCommand().equals(" / "))
{
equalBuffer = "";
calcIt();
buttonBuffer = "/";
} else
if(e.getActionCommand().equals(" * "))
{
equalBuffer = "";
calcIt();
buttonBuffer = "*";
} else
if(e.getActionCommand().equals(" 0 "))
{
numString = numString + "0";
inputOutputField.setText(numString);
} else
if(e.getActionCommand().equals(" . "))
{
numString = numString + ".";
inputOutputField.setText(numString);
} else
if(e.getActionCommand().equals(" + "))
{
equalBuffer = "";
calcIt();
buttonBuffer = "+";
} else
if(e.getActionCommand().equals(" = "))
{
calcIt();
buttonBuffer = buttonBuffer;
equalBuffer = "=";
} else
if(e.getActionCommand().equals(" CLEAR "))
{
numString = "";
inputOutputField.setText("0");
buttonBuffer = "";
numBuffer = 0.0D;
equalBuffer = "";
equalString = "";
} else
{
inputOutputField.setText("Error in adder code.");
}
repaint();
}
private void doTheMath()
{
if(buttonBuffer == "+")
{
if(numString == "")
numString = "0";
numBuffer = numBuffer + stringToDouble(numString);
inputOutputField.setText(Double.toString(numBuffer));
} else
if(buttonBuffer == "-")
{
if(numString == "")
numString = "0";
numBuffer = numBuffer - stringToDouble(numString);
inputOutputField.setText(Double.toString(numBuffer));
} else
if(buttonBuffer == "*")
{
if(numString == "")
numString = "1";
numBuffer = numBuffer * stringToDouble(numString);
inputOutputField.setText(Double.toString(numBuffer));
} else
if(buttonBuffer == "/")
{
if(numString == "")
numString = "1";
numBuffer = numBuffer / stringToDouble(numString);
inputOutputField.setText(Double.toString(numBuffer));
}
}
private void doEqualMath()
{
if(buttonBuffer == "+")
{
numBuffer = numBuffer + stringToDouble(equalString);
inputOutputField.setText(Double.toString(numBuffer));
} else
if(buttonBuffer == "-")
{
numBuffer = numBuffer - stringToDouble(equalString);
inputOutputField.setText(Double.toString(numBuffer));
} else
if(buttonBuffer == "*")
{
numBuffer = numBuffer * stringToDouble(equalString);
inputOutputField.setText(Double.toString(numBuffer));
} else
if(buttonBuffer == "/")
{
numBuffer = numBuffer / stringToDouble(equalString);
inputOutputField.setText(Double.toString(numBuffer));
}
}
private void calcIt()
{
numString
= inputOutputField
.getText(); if(flag == "y")
{
equalString
= inputOutputField
.getText(); doTheMath();
numBuffer
= stringToDouble
(inputOutputField
.getText()); numString = "";
}
if(flag == "n")
{
if(equalBuffer == "=")
doEqualMath();
numBuffer
= stringToDouble
(inputOutputField
.getText()); if(equalBuffer != "=")
buttonBuffer = "";
numString = "";
}
flag = "n";
}
private static double stringToDouble
(String stringObject
) {
return Double
.valueOf
(stringObject
.trim()).doubleValue
(); }
public AdderApplet()
{
buttonBuffer = "";
equalBuffer = "";
numBuffer = 0.0D;
numString = "";
equalString = "";
flag = "n";
}
private TextField inputOutputField;
private String buttonBuffer;
private String equalBuffer;
private double numBuffer;
private String numString;
private String equalString;
private String flag;
}