source code: calc.java

import java.awt.*;
import java.awt.event.*;
class calc extends Frame implements ActionListener
{
TextField t1,t2, t3;
public calc()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
Label l1=new Label("enter a no:");
Label l2=new Label("enter b no");
Label l3=new Label("result is: ");
 t1=new TextField(15);
 t2=new TextField(15);
 t3=new TextField(15);
t3.setEditable(false);
Button b1=new Button("add");
Button b2=new Button("sub");
Button b3=new Button("mul");
Button b4=new Button("div");

add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(b1);add(b2);
add(b3);add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
System.out.println("bt pressed is "+s);
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int res=0;
if(s.equals("add"))
res=a+b;
if(s.equals("sub"))
res=a-b;
if(s.equals("mul"))
res=a*b;
if(s.equals("div"))
res=a/b;
t3.setText(""+res);
}
public static void main(String as[])
{
calc f=new calc();
f.setSize(300,300);
f.setVisible(true);
}
}

 

output: