Creating Scientific Calculator Using Applet in Java
Creating Scientific Calculator Using Applet in Java
Java [closed]
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class publiccalculator extends Applet implements ActionListener{
lblcasio.setFont(myCasio);
lblfx.setFont(myFX);
lblnatural.setFont(myNatural);
add(lblcasio);
add(lblfx);
add(lblnatural);
add(txtfield1);
add(abs);
add(X2);
add(X3);
add(log);
add(sqrt);
add(sin);
add(cos);
add(tan);
add(pie);
add(E);
add(seven);
add(eight);
add(nine);
add(DEL);
add(AC);
add(four);
add(five);
add(six);
add(multiply);
add(subtract);
add(one);
add(two);
add(three);
add(plus);
add(dive);
add(zero);
add(equals);
add(dot);
add(round);
lblcasio.setBounds(60,0,100,20);
lblfx.setBounds(350,0,150,20);
lblnatural.setBounds(160,25,200,20);
txtfield1.setBounds(100,50,240,20);
abs.setBounds(100,75,35,35);
X2.setBounds(150,75,35,35);
X3.setBounds(200,75,35,35);
log.setBounds(250,75,35,35);
sqrt.setBounds(300,75,35,35);
sin.setBounds(100,120,35,35);
cos.setBounds(150,120,35,35);
tan.setBounds(200,120,35,35);
pie.setBounds(250,120,35,35);
E.setBounds(300,120,35,35);
seven.setBounds(100,165,35,35);
eight.setBounds(150,165,35,35);
nine.setBounds(200,165,35,35);
AC.setBounds(250,165,85,35);
four.setBounds(100,210,35,35);
five.setBounds(150,210,35,35);
six.setBounds(200,210,35,35);
multiply.setBounds(250,210,35,35);
subtract.setBounds(300,210,35,35);
one.setBounds(100,255,35,35);
two.setBounds(150,255,35,35);
three.setBounds(200,255,35,35);
plus.setBounds(250,255,35,35);
dive.setBounds(300,255,35,35);
zero.setBounds(100,300,35,35);
equals.setBounds(150,300,85,35);
dot.setBounds(250,300,35,35);
round.setBounds(300,300,35,35);
equals.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
plus.addActionListener(this);
dive.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
multiply.addActionListener(this);
subtract.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
sin.addActionListener(this);
cos.addActionListener(this);
tan.addActionListener(this);
pie.addActionListener(this);
E.addActionListener(this);
abs.addActionListener(this);
X2.addActionListener(this);
X3.addActionListener(this);
log.addActionListener(this);
sqrt.addActionListener(this);
zero.addActionListener(this);
dot.addActionListener(this);
round.addActionListener(this);
txtfield1.setEnabled(false);
}
B[10]=new Button("+");
B[11]=new Button("-");
B[12]=new Button("*");
B[13]=new Button("/");
B[14]=new Button("=");
B[15]=new Button("sqrt");
B[16]=new Button("B");
B[17]=new Button("CE");
B[18]=new Button("C");
B[19]=new Button("MS");
B[20]=new Button("MR");
add(tf);
for(int i=0;i<=9;i++)
{
B[i].addActionListener(this);
add(B[i]);
}
add(B[10]);
add(B[11]);
add(B[12]);
add(B[13]);
add(B[14]);
add(B[15]);
add(B[16]);
add(B[17]);
add(B[18]);
add(B[19]);
add(B[20]);
B[10].addActionListener(this);
B[11].addActionListener(this);
B[12].addActionListener(this);
B[13].addActionListener(this);
B[14].addActionListener(this);
B[15].addActionListener(this);
B[16].addActionListener(this);
B[17].addActionListener(this);
B[18].addActionListener(this);
B[19].addActionListener(this);
B[20].addActionListener(this);
}
else if(str=="-")
{
number1=Integer.valueOf(tf.getText());
tf.setText("");
choice=2;
temp="";
}
else if(str=="*")
{
number1=Integer.valueOf(tf.getText());
tf.setText("");
choice=3;
temp="";
}
else if(str=="/")
{
number1=Integer.valueOf(tf.getText());
tf.setText("");
choice=4;
temp="";
}
else if(str=="sqrt")
{
number1=Integer.valueOf(tf.getText());
tf.setText(String.valueOf(Math.sqrt(number1)));
}
else if(str=="B")
{
temp2=tf.getText();
temp=temp2.substring(0,temp2.length()-1);
tf.setText(temp2.substring(0,temp2.length()-1));
number2=Integer.valueOf(tf.getText());
}
else if(str=="CE")
{
temp2=null;
tf.setText("");
temp="";
}
else if(str=="C")
{
temp="";
number1=0;
number2=0;
choice=0;
tf.setText("");
}
else if(str=="MS")
{
temp2=tf.getText();
}
else if(str=="MR")
{
tf.setText(temp2);
}
else if(str=="=")
{
switch(choice)
{
case 1:
answer=number1+number2;
tf.setText(String.valueOf(answer));
break;
case 2:
answer=number1-number2;
tf.setText(String.valueOf(answer));
break;
case 3:
answer=number1*number2;
tf.setText(String.valueOf(answer));
break;
case 4:
answer=number1/number2;
tf.setText(String.valueOf(answer));
break;
}
}
else
{
temp=temp+str;
tf.setText(temp);
number2=Integer.valueOf(temp);
}
}
}
7.5K views
View 10 upvotes
Upvote
10