0% found this document useful (0 votes)
6 views6 pages

Expt 14

Experiment design. Met pdf

Uploaded by

gtn8061
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Expt 14

Experiment design. Met pdf

Uploaded by

gtn8061
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Expt.

No: 14 Simple Calculator in Java

Aim:
To Write a program to create a simple calculator to perform addition,
subtraction, multiplication and division using button, label and text field.

Program:
import java.awt.*;
import java.awt.event.*;
class Calculator implements ActionListener
{
String s1,s2,s3,s4,s5; // s1 is the first n s2 is the second n
int c,num1,num2,result;
//Declaring Objects
Frame f=new Frame(); // creating a Frame
TextField screen=new TextField(); // creating a TextField
Button b0 = new Button("0"); // creating a Button
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button badd =new Button("+");
Button bsub =new Button("-");
Button bmul =new Button("*");
Button bdiv =new Button("/");
Button beq =new Button("=");
Button bclear = new Button("Clear");
Calculator()
{
//setting Coordinates in a frame
screen.setBounds(10,50,200,30);
bclear.setBounds(10,80,200,20);
b7.setBounds(10,100,50,50);
b8.setBounds(60,100,50,50);
b9.setBounds(110,100,50,50);
bmul.setBounds(160,100,50,50);
b4.setBounds(10,150,50,50);
b5.setBounds(60,150,50,50);
b6.setBounds(110,150,50,50);
bdiv.setBounds(160,150,50,50);
b1.setBounds(10,200,50,50);
b2.setBounds(60,200,50,50);
b3.setBounds(110,200,50,50);
bsub.setBounds(160,200,50,50);
b0.setBounds(10,250,50,50);
beq.setBounds(60,250,100,50);
badd.setBounds(160,250,50,50);
//Adding components to the frame
f.add(screen);
f.add(b0);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(badd);
f.add(bsub);
f.add(bmul);
f.add(bdiv);
f.add(beq);
f.add(bclear);
//add action listener to this(button) component.
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
beq.addActionListener(this);
bclear.addActionListener(this);
f.setTitle("My Calculator"); // setting the title of frame
f.setSize(500,500); // frame size 500 width and 500 height
f.setLayout(null); // no layout
f.setVisible(true); // to make a frame visible
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
s3 = screen.getText(); //screen number=5
s4 = "0"; //pressed button=0
s5 = s3+s4; //number=50
screen.setText(s5); //display 50 in the screen
}
if(e.getSource()==b1)
{
s3 = screen.getText();
s4 = "1";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b2)
{
s3 = screen.getText();
s4 = "2";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b3)
{
s3 = screen.getText();
s4 = "3";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b4)
{
s3 = screen.getText();
s4 = "4";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b5)
{
s3 = screen.getText();
s4 = "5";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b6)
{
s3 = screen.getText();
s4 = "6";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b7)
{
s3 = screen.getText();
s4 = "7";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b8)
{
s3 = screen.getText();
s4 = "8";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==b9)
{
s3 = screen.getText();
s4 = "9";
s5 = s3+s4;
screen.setText(s5);
}
if(e.getSource()==badd)
{
s1 = screen.getText();
screen.setText("");
c=1;
}
if(e.getSource()==bsub)
{
s1 = screen.getText();
screen.setText("");
c=2;
}

if(e.getSource()==bmul)
{
s1 = screen.getText();
screen.setText("");
c=3;
}
if(e.getSource()==bdiv)
{
s1 = screen.getText();
screen.setText("");
c=4;
}

if(e.getSource()==beq)
{
s2 = screen.getText();
num1=Integer.parseInt(s1); //convert string into int
num2=Integer.parseInt(s2);
if(c==1)
{
result= num1+num2;
screen.setText(String.valueOf(result));
}
if(c==2)
{
result= num1-num2;
screen.setText(String.valueOf(result));
}
if(c==3)
{
result= num1*num2;
screen.setText(String.valueOf(result));
}
if(c==4)
{
if(num2!=0)
{
result= num1/num2;
screen.setText(String.valueOf(result));
}
else
screen.setText("Undefined");
}
}

if(e.getSource()==bclear)
{
screen.setText("");
}
}
public static void main(String[] args)
{
Calculator calc=new Calculator();
}
}

Result:
Thus the Java Program to create a simple calculator to perform addition,
subtraction, multiplication and division using button, label and text field is
written and executed successfully.

You might also like