Calculator using Applet in Java


Source Code:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code=”calculator” width=300 height=300>
</applet>*/
public class calculator extends Applet implements ActionListener
{
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char op;
public void init()
{
t1=new TextField(50);
GridLayout gl=new GridLayout(5,4);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(“”+i);
}
add=new Button(“+”);
sub=new Button(“-“);
mul=new Button(“*”);
div=new Button(“/”);
mod=new Button(“%”);
clear=new Button(“clear”);
EQ=new Button(“=”);
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);

}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals(“+”))
{
v1=Integer.parseInt(t1.getText());
op=’+’;
t1.setText(“”);
}
else if(str.equals(“-“))
{
v1=Integer.parseInt(t1.getText());
op=’-‘;
t1.setText(“”);
}
else if(str.equals(“*”))
{
v1=Integer.parseInt(t1.getText());
op=’*’;
t1.setText(“”);
}
else if(str.equals(“/”))
{
v1=Integer.parseInt(t1.getText());
op=’/’;
t1.setText(“”);
}
else if(str.equals(“%”))
{
v1=Integer.parseInt(t1.getText());
op=’%’;
t1.setText(“”);
}
if(str.equals(“=”))
{
v2=Integer.parseInt(t1.getText());
if(op==’+’)

result=v1+v2;
else if(op==’-‘)
result=v1-v2;
else if(op==’*’)
result=v1*v2;
else if(op==’/’)
result=v1/v2;
else if(op==’%’)
result=v1%v2;
t1.setText(“”+result);
}
if(str.equals(“clear”))
{
t1.setText(“”);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s