// 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 bigCalculator 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("+/-");
private Button button_Square=new Button("X^2");
private Button button_Cube=new Button("X^3");
private Button button_Power=new Button("X^Y");
private Button button_Reverse=new Button("1/X");
private Button button_Percentage=new Button("%");
private Button button_SqRoot=new Button("sqrt");
private Button button_Log=new Button("log");
private Button button_Factorial=new Button("N!");
private Button button_Pi=new Button("Pi");
private Button button_Sin=new Button("sin");
private Button button_Cos=new Button("cos");
private Button button_Tan=new Button("tan");
// _____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, 26);
// _____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_Negative.setBackground( Color.white );
button_Clear.setBackground( Color.red );
button_Clear.setForeground (Color.white);
button_Equal.setForeground (Color.black);
button_Divide.setForeground (Color.white);
button_Multiply.setForeground (Color.white);
button_Substract.setForeground (Color.white);
button_Add.setForeground (Color.white);
button_Square.setForeground (Color.white);
button_Cube.setForeground (Color.white);
button_Power.setForeground (Color.white);
button_Reverse.setForeground (Color.white);
button_Percentage.setForeground (Color.white);
button_SqRoot.setForeground (Color.white);
button_Log.setForeground (Color.white);
button_Factorial.setForeground (Color.white);
button_Pi.setForeground (Color.white);
button_Sin.setForeground (Color.white);
button_Cos.setForeground (Color.white);
button_Tan.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_Decimal.setFont(operationFont);
button_Negative.setFont(operationFont);
button_Clear.setFont(numberFont);
button_Equal.setFont(operationFont);
button_Divide.setFont(operationFont);
button_Multiply.setFont(operationFont);
button_Substract.setFont(operationFont);
button_Add.setFont(operationFont);
button_Square.setFont(numberFont);
button_Cube.setFont(numberFont);
button_Power.setFont(numberFont);
button_Reverse.setFont(numberFont);
button_Percentage.setFont(numberFont);
button_SqRoot.setFont(numberFont);
button_Log.setFont(numberFont);
button_Factorial.setFont(numberFont);
button_Pi.setFont(numberFont);
button_Sin.setFont(numberFont);
button_Cos.setFont(numberFont);
button_Tan.setFont(numberFont);
errorLabel.setFont(numberFont);
result_Text.setFont (numberFont);
// _____Objects size & place settings _____
calcPanel.setSize (370, 690);
rightBorder.setBounds (675, 0, 15, 370);
bottomBorder.setBounds (0, 355, 690, 15);
button_Clear.setBounds (515, 35, 60, 40);
button_Equal.setBounds (595, 35, 60, 40);
result_Text.setBounds (35, 35, 460, 40);
resultBorder.setBounds (35, 35, 462, 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 (355, 95, 60, 40);
button_Reverse.setBounds (435, 95, 60, 40);
button_Percentage.setBounds (515, 95, 60, 40);
button_Pi.setBounds (595, 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 (355, 155, 60, 40);
button_Square.setBounds (435, 155, 60, 40);
button_SqRoot.setBounds (515, 155, 60, 40);
button_Sin.setBounds (595, 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 (355, 215, 60, 40);
button_Cube.setBounds (435, 215, 60, 40);
button_Log.setBounds (515, 215, 60, 40);
button_Cos.setBounds (595, 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 (355, 275, 60, 40);
button_Power.setBounds (435, 275, 60, 40);
button_Factorial.setBounds (515, 275, 60, 40);
button_Tan.setBounds (595, 275, 60, 40);
errorLabel.setBounds (100, 325, 490, 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_Reverse);
add (button_Percentage);
add(button_Pi);
add (button_4);
add (button_5);
add (button_6);
add (button_Multiply);
add (button_Square);
add (button_SqRoot);
add (button_Sin);
add (button_1);
add (button_2);
add (button_3);
add (button_Substract);
add (button_Cube);
add (button_Log);
add (button_Cos);
add (button_0);
add (button_Decimal);
add (button_Negative);
add (button_Add);
add (button_Power);
add (button_Factorial);
add (button_Tan);
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);
button_Square.addActionListener(this);
button_Cube.addActionListener(this);
button_Power.addActionListener(this);
button_Reverse.addActionListener(this);
button_Percentage.addActionListener(this);
button_Pi.addActionListener(this);
button_SqRoot.addActionListener(this);
button_Sin.addActionListener(this);
button_Log.addActionListener(this);
button_Cos.addActionListener(this);
button_Factorial.addActionListener(this);
button_Tan.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);
button_Square.addKeyListener (this);
button_Cube.addKeyListener (this);
button_Power.addKeyListener (this);
button_Reverse.addKeyListener (this);
button_Percentage.addKeyListener (this);
button_Pi.addKeyListener (this);
button_SqRoot.addKeyListener (this);
button_Sin.addKeyListener (this);
button_Log.addKeyListener (this);
button_Cos.addKeyListener (this);
button_Factorial.addKeyListener (this);
button_Tan.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=="Pi")
result_Text.setText (String.valueOf (Math.PI));
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=="=" || buttonName=="1/X"
|| buttonName=="X^2" || buttonName=="X^3"
|| buttonName=="X^Y" || buttonName=="%"
|| buttonName=="sqrt" || buttonName=="rootN"
|| buttonName== "N!" || buttonName=="log"
|| buttonName=="sin" || buttonName== "cos"
|| buttonName=="tan")
{
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=="" || bName=="X^2" || bName=="X^3"
|| bName=="1/X" || bName=="sqrt" || bName=="sin"
|| bName=="cos" || bName=="tan" || bName=="N!"
|| bName=="log")
{
result=tempNumber;
operation=bName;
if (bName=="X^2"){square();}
else if (bName=="X^3"){cube();}
else if (bName=="1/X"){reverse();}
else if (bName=="sqrt"){squareRoot();}
else if (bName=="sin"){calcSin();}
else if (bName=="cos"){calcCos();}
else if (bName=="tan"){calcTan();}
else if (bName=="N!") {calcFact();}
else if (bName=="log") {calcLog();}
}
else
{
secondNumber=tempNumber;
if (operation=="+") {add();}
else if (operation=="-"){substract();}
else if (operation=="*"){multiply();}
else if (operation=="/"){divide();}
else if (operation=="X^Y"){power();}
else if (operation=="%"){percentage();}
changeOperation(bName);
}
}
secondNumber=0.0;
//To clear textBuffer
numbersBuffer.setLength (0);
//Change the textBox
result_Text.setText(String.valueOf ((Math.floor( result*100 + 0.5 ) / 100.0 )));
calcPanel.getCursor();
}
private void add(){ result+=secondNumber;}
private void substract(){result-=secondNumber;}
private void multiply(){result*=secondNumber;}
private void divide()
{
if (secondNumber!=0){result/=secondNumber;}
else { errorLabel.setText ("Error! Division by 0");}
}
private void reverse(){result=1/result;}
private void square(){result*=result;}
private void cube(){result=result*result*result;}
private void power(){result=Math.pow (result, secondNumber);}
private void squareRoot(){result=Math.sqrt (result);}
private void calcSin(){result=Math.sin (result*Math.PI/180 );}
private void calcCos(){result=Math.cos (result*Math.PI/180 );}
private void calcTan(){result=Math.tan (result*Math.PI/180 );}
private void calcFact()
{
int temp=(int)result;
if (temp<34)
{
int base=1;
for (int i=1; i<=temp; i++)
{
base*=i;
}
result=(double)base;
}
else errorLabel.setText ("Number exceeded a permissible value");
}
private void percentage(){result=result/100*secondNumber;}
private void calcLog(){ result=Math.log (result);}
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;}
case 'X': { operation="X^Y"; break;}
case '%': { operation="%"; break;}
}
}
}