USING AWT MAIN FUNCTION:-
import [Link].*;
import [Link].*;
class Calculator implements ActionListener
//Declaring Objects
Frame f=new Frame();
Label l1=new Label("First Number");
Label l2=new Label("Second Number");
Label l3=new Label("Result");
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mul");
Button b4=new Button("Div");
Button b5=new Button("Cancel");
Calculator()
//Giving Coordinates
[Link](50,100,100,20);
[Link](50,140,100,20);
[Link](50,180,100,20);
[Link](200,100,100,20);
[Link](200,140,100,20);
[Link](200,180,100,20);
[Link](50,250,50,20);
[Link](110,250,50,20);
[Link](170,250,50,20);
[Link](230,250,50,20);
[Link](290,250,50,20);
//Adding components to the frame
[Link](l1);
[Link](l2);
[Link](l3);
[Link](t1);
[Link](t2);
[Link](t3);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](null);
[Link](true);
[Link](400,350);
public void actionPerformed(ActionEvent e)
int n1=[Link]([Link]());
int n2=[Link]([Link]());
if([Link]()==b1)
[Link]([Link](n1+n2));
if([Link]()==b2)
[Link]([Link](n1-n2));
if([Link]()==b3)
[Link]([Link](n1*n2));
if([Link]()==b4)
{
[Link]([Link](n1/n2));
if([Link]()==b5)
[Link](0);
public static void main(String...s)
new Calculator();
}
2) USING APPLET:-
import [Link].*;
import [Link].*;
import [Link].*;
public class calc extends Applet implements ActionListener
Label l1,l2;
TextField t1,t2,t3;
Button addition,subtraction,multiplication,division;
public void init()
l1=new Label("enter first no");
add(l1);
l2=new Label("enter second no");
add(l2);
t1=new TextField(10);
add(t1);
t2=new TextField(10);
add(t2);
t3=new TextField(10);
add(t3);
addition=new Button("+");
add(addition);
[Link](this);
subtraction=new Button("-");
add(subtraction);
[Link](this);
multiplication=new Button("*");
add(multiplication);
[Link](this);
division=new Button("/");
add(division);
[Link](this);
public void actionPerformed(ActionEvent ae)
if([Link]()==addition)
int sum=[Link]([Link]()) + [Link]([Link]());
[Link]([Link](sum));
if([Link]()==subtraction)
{
int sub=[Link]([Link]()) + [Link]([Link]());
[Link]([Link](sub));
if([Link]()==multiplication)
int mul=[Link]([Link]()) + [Link]([Link]());
[Link]([Link](mul));
if([Link]()==division)
int div=[Link]([Link]()) + [Link]([Link]());
[Link]([Link](div));
/*
<applet code="calc" width=200 height=200>
</applet>
*/
3) USING SWING:-
import [Link].*;
import [Link].*;
import [Link].*;
class calculator extends JFrame implements ActionListener {
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
// store oprerator and operands
String s0, s1, s2;
// default constrcutor
calculator()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
[Link]([Link]());
}
catch (Exception e) {
[Link]([Link]());
}
// create a object of class
calculator c = new calculator();
// create a textfield
l = new JTextField(16);
// set the textfield to non editable
[Link](false);
// create number buttons and some operators
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
// create number buttons
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
// equals button
beq1 = new JButton("=");
// create operator buttons
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
// add action listeners
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
// add elements to panel
[Link](l);
[Link](ba);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](bs);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](bm);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bd);
[Link](be);
[Link](b0);
[Link](beq);
[Link](beq1);
// set Background of panel
[Link]([Link]);
// add panel to frame
[Link](p);
[Link](200, 220);
[Link]();
}
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
// if the value is a number
if (([Link](0) >= '0' && [Link](0) <= '9') || [Link](0) == '.') {
// if operand is present then add to second no
if ()
s2 = s2 + s;
else
s0 = s0 + s;
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == '=') {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
te = ([Link](s0) / [Link](s2));
else
te = ([Link](s0) * [Link](s2));
// set the value of text
[Link](s0 + s1 + s2 + "=" + te);
// convert it to string
s0 = [Link](te);
s1 = s2 = "";
}
else {
// if there was no operand
if ([Link]("") || [Link](""))
s1 = s;
// else evaluate
else {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
te = ([Link](s0) / [Link](s2));
else
te = ([Link](s0) * [Link](s2));
// convert it to string
s0 = [Link](te);
// place the operator
s1 = s;
// make the operand blank
s2 = "";
}
// set the value of text
[Link](s0 + s1 + s2);
}
}
}