programme:- to build a claculator application that sum,multiply,divide,subtract.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//<applet code="calci2" height="300" width="300"></applet>
public class calci2 extends Applet implements ActionListener
{
Label l1=new Label("enter 1st number");
TextField t1=new TextField(20);
Label l2=new Label("enter 2nd number");
TextField t2=new TextField(20);
Label l3=new Label("Result");
TextField t3=new TextField(20);
Button b1=new Button("Sum");
Button b2=new Button("Subtract");
Button b3=new Button("Multiply");
Button b4=new Button("Division");
{
Component add1=add(l1);
Component add2=add(t1);
Component add3=add(l2);
Component add4=add(t2);
Component add5=add(l3);
Component add6=add(t3);
Component add7=add(b1);
Component add8=add(b2);
Component add9=add(b3);
Component add10=add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
//public void init()
//{
//setBackground(Color.YELLOW);
//}
public void actionPerformed(ActionEvent e)
{
int s1,s2,s3,s4,s5,s6;
if(e.getSource()==b1)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s3=s1+s2;
t3.setText(Integer.toString(s3));
setBackground(Color.YELLOW);
}
if(e.getSource()==b2)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s4=s1-s2;
t3.setText(Integer.toString(s4));
}
if(e.getSource()==b3)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s5=s1*s2;
t3.setText(Integer.toString(s5));
}
if(e.getSource()==b4)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s6=s1/s2;
t3.setText(Integer.toString(s6));
}
}
}
Run it by saving by calci2.java
1) javac calci2.java
2) appletviewer calci2.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//<applet code="calci2" height="300" width="300"></applet>
public class calci2 extends Applet implements ActionListener
{
Label l1=new Label("enter 1st number");
TextField t1=new TextField(20);
Label l2=new Label("enter 2nd number");
TextField t2=new TextField(20);
Label l3=new Label("Result");
TextField t3=new TextField(20);
Button b1=new Button("Sum");
Button b2=new Button("Subtract");
Button b3=new Button("Multiply");
Button b4=new Button("Division");
{
Component add1=add(l1);
Component add2=add(t1);
Component add3=add(l2);
Component add4=add(t2);
Component add5=add(l3);
Component add6=add(t3);
Component add7=add(b1);
Component add8=add(b2);
Component add9=add(b3);
Component add10=add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
//public void init()
//{
//setBackground(Color.YELLOW);
//}
public void actionPerformed(ActionEvent e)
{
int s1,s2,s3,s4,s5,s6;
if(e.getSource()==b1)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s3=s1+s2;
t3.setText(Integer.toString(s3));
setBackground(Color.YELLOW);
}
if(e.getSource()==b2)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s4=s1-s2;
t3.setText(Integer.toString(s4));
}
if(e.getSource()==b3)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s5=s1*s2;
t3.setText(Integer.toString(s5));
}
if(e.getSource()==b4)
{
s1=Integer.parseInt(t1.getText());
s2=Integer.parseInt(t2.getText());
s6=s1/s2;
t3.setText(Integer.toString(s6));
}
}
}
Run it by saving by calci2.java
1) javac calci2.java
2) appletviewer calci2.java
No comments:
Post a Comment