BASIC CALCULATOR
calculation using buttons
/*
<applet code=calculate width=300 height=300>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class calculate extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button Add,sub,mul,div;
public void init()
{
setBackground(Color.gray);
t1=new TextField(15);
t2=new TextField(15);
t3=new TextField(15);
Add=new Button(" ADD ");
sub=new Button(" SUB ");
mul=new Button(" MUL ");
div=new Button(" DIV ");
add(t1);
add(t2);
add(Add);
add(sub);
add(mul);
add(div);
add(t3);
Add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int a,b,c=0;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
if(ae.getSource()==Add)
{
c=a+b;
//t3.setText(Integer.toString(c));
}
if(ae.getSource()==sub)
{
c=a-b;
//t3.setText(Integer.toString(c));
}
if(ae.getSource()==mul)
{
c=a*b;
//t3.setText(Integer.toString(c));
}
if(ae.getSource()==div)
{
c=a/b;
//t3.setText(Integer.toString(c));
}
t3.setText(Integer.toString(c));
}
}
/*
<applet code=calculate width=300 height=300>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class calculate extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button Add,sub,mul,div;
public void init()
{
setBackground(Color.gray);
t1=new TextField(15);
t2=new TextField(15);
t3=new TextField(15);
Add=new Button(" ADD ");
sub=new Button(" SUB ");
mul=new Button(" MUL ");
div=new Button(" DIV ");
add(t1);
add(t2);
add(Add);
add(sub);
add(mul);
add(div);
add(t3);
Add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int a,b,c=0;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
if(ae.getSource()==Add)
{
c=a+b;
//t3.setText(Integer.toString(c));
}
if(ae.getSource()==sub)
{
c=a-b;
//t3.setText(Integer.toString(c));
}
if(ae.getSource()==mul)
{
c=a*b;
//t3.setText(Integer.toString(c));
}
if(ae.getSource()==div)
{
c=a/b;
//t3.setText(Integer.toString(c));
}
t3.setText(Integer.toString(c));
}
}
Comments
Post a Comment