// This is a simple calculator Author Galina Goz Kriviakov, January 2002
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class calculator extends Applet implements ActionListener, KeyListener
{
// _____Objects declaration_____
private Panel calcPanel=new Panel();
private Button button_1=new Button("1");
private Button button_2=new Button("2");
private Button button_3=new Button("3");
private Button button_4=new Button("4");
private Button button_5=new Button("5");
private Button button_6=new Button("6");
private Button button_7=new Button("7");
private Button button_8=new Button("8");
private Button button_9=new Button("9");
private Button button_0=new Button("0");
private Button button_Add=new Button("+");
private Button button_Substract=new Button("-");
private Button button_Multiply=new Button("*");
private Button button_Divide=new Button("/");
private Button button_Clear=new Button("CE/C");
private Button button_Equal=new Button("=");
private Button button_Decimal=new Button(".");
private Button button_Negative=new Button("+/-");
// _____Label to show calculation _____
private Label result_Text=new Label();
// _____Label to border result_Text, doesn't have own functional purpose _____
private Label resultBorder=new Label();
private StringBuffer numbersBuffer=new StringBuffer ();
// _____Declaration of buttons fonts _____
private Font numberFont = new Font ("TimesRoman", Font.BOLD, 20);
private Font operationFont = new Font ("TimesRoman", Font.BOLD, 36);
// _____Variables for calculation results _____
private double secondNumber=0.0;
private double result=0.0;
// _____Variables for holding operation symbol_____
private String operation =new String();
// _____Label to show posibles errors _____
private Label errorLabel = new Label ();
// _____Label to border panel, doesn't have own functional purpose _____
private Label rightBorder=new Label ();
private Label bottomBorder=new Label ();
// ______________________________________________________
// _____Initialization _____
public void init()
{
setLayout(null);
// _____Objects background-foreground settings _____
rightBorder.setBackground (Color.gray );
bottomBorder.setBackground (Color.gray);
button_0.setBackground( Color.white );
button_1.setBackground( Color.white );
button_2.setBackground( Color.white );
button_3.setBackground( Color.white );
button_4.setBackground( Color.white );
button_5.setBackground( Color.white );
button_6.setBackground( Color.white );
button_7.setBackground( Color.white );
button_8.setBackground( Color.white );
button_9.setBackground( Color.white );
button_Decimal.setBackground( Color.white );
button_Decimal.setForeground (Color.black);
button_Negative.setBackground( Color.white );
button_Negative.setForeground (Color.black);
button_Clear.setBackground( Color.red );
button_Clear.setForeground (Color.white);
button_Divide.setForeground (Color.white);
button_Multiply.setForeground (Color.white);
button_Substract.setForeground (Color.white);
button_Equal.setForeground (Color.black);
button_Add.setForeground (Color.white);
errorLabel.setForeground (Color.red);
result_Text.setBackground (Color.white);
resultBorder.setBackground (Color.black);
// _____Objects font settings (using NumberFont, OperationFont) _____
button_0.setFont(numberFont);
button_1.setFont(numberFont);
button_2.setFont(numberFont);
button_3.setFont(numberFont);
button_4.setFont(numberFont);
button_5.setFont(numberFont);
button_6.setFont(numberFont);
button_7.setFont(numberFont);
button_8.setFont(numberFont);
button_9.setFont(numberFont);
button_Clear.setFont(numberFont);
button_Divide.setFont(numberFont);
button_Multiply.setFont(numberFont);
button_Substract.setFont(numberFont);
button_Decimal.setFont(numberFont);
button_Negative.setFont(numberFont);
button_Equal.setFont(numberFont);
button_Add.setFont(numberFont);
errorLabel.setFont(numberFont);
result_Text.setFont (numberFont);
// _____Objects size & place settings _____
calcPanel.setSize (370, 370);
rightBorder.setBounds (355, 0, 15, 370);
bottomBorder.setBounds (0, 355, 370, 15);
button_Clear.setBounds (35, 35, 60, 40);
button_Equal.setBounds (275, 35, 60, 40);
result_Text.setBounds (115, 35, 140, 40);
resultBorder.setBounds (115, 35, 142, 42);
button_7.setBounds (35, 95, 60, 40);
button_8.setBounds (115, 95, 60, 40);
button_9.setBounds (195, 95, 60, 40);
button_Divide.setBounds (275, 95, 60, 40);
button_4.setBounds (35, 155, 60, 40);
button_5.setBounds (115, 155, 60, 40);
button_6.setBounds (195, 155, 60, 40);
button_Multiply.setBounds (275, 155, 60, 40);
button_1.setBounds (35, 215, 60, 40);
button_2.setBounds (115, 215, 60, 40);
button_3.setBounds (195, 215, 60, 40);
button_Substract.setBounds (275, 215, 60, 40);
button_0.setBounds (35, 275, 60, 40);
button_Decimal.setBounds (115, 275, 60, 40);
button_Negative.setBounds (195, 275, 60, 40);
button_Add.setBounds (275, 275, 60, 40);
errorLabel.setBounds (85, 325, 200, 20);
// _____Addition objects to an applet _____
add (rightBorder);
add (bottomBorder);
add (button_Clear);
add (button_Equal);
add (result_Text);
add (resultBorder);
add (button_7);
add (button_8);
add (button_9);
add (button_Divide);
add (button_4);
add (button_5);
add (button_6);
add (button_Multiply);
add (button_1);
add (button_2);
add (button_3);
add (button_Substract);
add (button_0);
add (button_Decimal);
add (button_Negative);
add (button_Add);
add (errorLabel);
add (calcPanel);
// _____Registration of ActionListener _____
button_0.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
button_9.addActionListener(this);
button_Clear.addActionListener(this);
button_Divide.addActionListener(this);
button_Multiply.addActionListener(this);
button_Substract.addActionListener(this);
button_Decimal.addActionListener(this);
button_Equal.addActionListener(this);
button_Add.addActionListener(this);
button_Negative.addActionListener(this);
// _____Registration of KeyListener _____
calcPanel.addKeyListener(this);
button_0.addKeyListener (this);
button_1.addKeyListener (this);
button_2.addKeyListener (this);
button_3.addKeyListener (this);
button_4.addKeyListener (this);
button_5.addKeyListener (this);
button_6.addKeyListener (this);
button_7.addKeyListener (this);
button_8.addKeyListener (this);
button_9.addKeyListener (this);
button_Clear.addKeyListener (this);
button_Divide.addKeyListener (this);
button_Multiply.addKeyListener (this);
button_Substract.addKeyListener (this);
button_Decimal.addKeyListener (this);
button_Equal.addKeyListener (this);
button_Add.addKeyListener (this);
button_Negative.addKeyListener (this);
operation="";
// _____Setting focus to the Panel _____
calcPanel.requestFocus();
}
// ____________________________
// _____Mouse input _____
public void actionPerformed (ActionEvent ae)
{
Button buttonClicked=(Button)(ae.getSource());
String name=buttonClicked.getLabel ();
calcPanel.requestFocus();
decision(name);
}
// ____________________________
// _____Keyboard input _____
public void keyReleased (KeyEvent ke){}
public void keyTyped (KeyEvent ke){}
public void keyPressed (KeyEvent ke)
{
String name="";
char keyT=ke.getKeyChar ();
if (ke.getKeyCode()==10)
{
name="=";
}
else
{
if (ke.getKeyCode()==27)
{
name="CE/C";
}
else
{
switch (keyT)
{
case '1':name="1"; break;
case '2':name="2"; break;
case '3':name="3"; break;
case '4':name="4"; break;
case '5':name="5"; break;
case '6':name="6"; break;
case '7':name="7"; break;
case '8':name="8"; break;
case '9':name="9"; break;
case '0':name="0"; break;
case '/':name="/"; break;
case '+':name="+"; break;
case '-':name="-"; break;
case '*':name="*"; break;
case '.':name="."; break;
case '=':name="="; break;
default: {break;}
}
}
}
if (name!="")
decision(name);
}
// ____________________________
// _____Function processes key strokes _____
public void decision(String buttonName)
{
// _____Clear errorLabel _____
errorLabel.setText ("");
if (buttonName=="+/-")
{
if (numbersBuffer.charAt(0)!='-')
{
numbersBuffer.insert (0, '-');
result_Text.setText (numbersBuffer.toString ());
}
else
{
StringBuffer temp= new StringBuffer ();
temp.append (numbersBuffer.toString ().substring (1));
numbersBuffer=temp;
result_Text.setText (numbersBuffer.toString ());
}
}
else if (buttonName=="1" || buttonName=="2" || buttonName=="3"
|| buttonName=="4" || buttonName=="5"
|| buttonName=="6" || buttonName=="7"
|| buttonName=="8" || buttonName=="9"
|| buttonName=="0")
{
numbersBuffer.append(buttonName);
result_Text.setText(numbersBuffer.toString());
}
else if (buttonName=="." &&(numbersBuffer.toString()).indexOf (".")==(-1))
{
// If "." was clicked once - adds decimal point else ingnores point
//(numbersBuffer.toString()).indexOf (".")==(-1) - converts numbersBuffer
// to String() and checks this string for decimal point
numbersBuffer.append(buttonName);
result_Text.setText(numbersBuffer.toString());
}
else if (buttonName=="CE/C")
{
clearText();
}
else if (buttonName=="+" || buttonName=="-" || buttonName=="/"
|| buttonName=="*" || buttonName=="=" )
{
calculate(buttonName);
}
}
public void clearText()
{
result=0.0;
numbersBuffer.setLength (0);
operation="";
result_Text.setText("");
}
public void calculate(String bName)
{
//To convert string to double
Double tempNum=Double.valueOf (result_Text.getText ());
double tempNumber=tempNum.doubleValue() ;
if (operation=="")
{
result=tempNumber;
operation=bName;
}
else
{
secondNumber=tempNumber;
if (operation=="+") {result=(Math.floor( (result+secondNumber)*100 + 0.5 ) / 100.0 );}
else if (operation=="-"){result=(Math.floor( (result-secondNumber)*100 + 0.5 ) / 100.0 );}
else if (operation=="*"){result=(Math.floor( (result*secondNumber)*100 + 0.5 ) / 100.0 );}
else if (operation=="/")
{
if (secondNumber!=0){result=(Math.floor( (result/secondNumber)*100 + 0.5 ) / 100.0 );}
else { errorLabel.setText ("Error! Division by 0");}
}
changeOperation(bName);
}
secondNumber=0.0;
//To clear textBuffer
numbersBuffer.setLength (0);
//Change the textBox
result_Text.setText(String.valueOf (result ));
calcPanel.getCursor();
}
private void changeOperation( String opName )
{
char opNameChar=opName.charAt (0);
switch (opNameChar)
{
case '+': { operation="+"; break;}
case '-': { operation="-"; break;}
case '*': { operation="*"; break;}
case '/': { operation="/"; break;}
case '=': { operation=""; break;}
}
}
}