PS 7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Assignment No.

07
Name: Harsh Sonigara
Roll No: 2213220
Class: SY-4 Batch A
Subject: JPL

Assignment Title: create a calculator using single function add(), sub() and multi().
Aim: Write a java program to create a calculator which performs addition,
subtraction and multiplication of numbers for different types like integer, float and
complex numbers using single function add(),sub() and multi().
Pre-Requisites: C/C++ Programming
Objective: We will learn how to build a simple calculator using Java AWT. This
calculator has some simple functionality like all the basic mathematical
operations and some special addon features using Java Swing components to
create a simple calculator with only +, -, /, * operations.
Outcomes:
For creating a calculator, we can have the following different sets of input and output.
1. To Perform Addition :

When the addition expression "98+102" is typed,


it is expected that the result is displayed as "98+102=200.0".
2. To Perform Subtraction :

When the subtraction expression "200.0-58.75" is typed,


it is expected that the result is displayed as "200.0-58.75=141.25".
3. To Perform Multiplication :
When an multiplication expression "141.25*20" is typed,
it is expected that the result is displayed as "141.25*20=2825.0".
4. To Perform Division : When the denominator is non-zero

When an division expression "2825.0/5" is typed,


it is expected that the result is displayed as "2825.0/5=565.0".
5. To Perform Division : When the denominator is zero

When an division expression "565.0/0" is typed,


it is expected that the error is displayed as "565.0/0=Zero Divison Error".

Theory:
AWT(Abstract Window Toolkit) is an API that helps in building GUI (Graphical User
Interface) based java applications. GUI helps in user interactions using some graphics.
It primarily consists of a set of classes and methods that are required for creating and
managing the GUI in a simplified manner such as
buttons,windows,frame,textfield,RadioButton etc
In this article we will use Java AWT components to create a simple calculator with
only +, -, /, * operations.
methods used :

1. add(Component c) : adds component to container.


2. addActionListenerListener(ActionListener d) : add actionListener for
specified component
3. setBackground(Color c) : sets the background color of the specified
container
4. setSize(int a, int b) : sets the size of container to specified dimensions.
5. setText(String s) : sets the text of the label to s.
6. getText() : returns the text of the label.

Algorithm/Steps:
LOGIC PART -I

1. FOR NUMERIC BUTTON. if(e. getSource()==b1){ //b1 for number 1


zt=l1. ...
2. FOR AIRTHMETIC BUTTON. if(e. getSource()==badd){ //FOR
ADDITION num1=Double. ...
3. FOR EQUALS BUTTON. if(e. getSource()==bcalc){ num2=Double. ...
4. FOR CLEAR BUTTON. if(e. ...
5. FOR BACKSPACE BUTTON.

LOGIC PART -II


1.FOR NUMERIC BUTTON

if(e.getSource()==b1){ //b1 for number 1


zt=l1.getText();
z=zt+"1";// 1 will merged at the end of the previous value
l1.setText(z);
}

when any of the numeric button pressed


whatever value in label l1 will be stored in a variable zt and then concatenated with
the corresponding number and and then displayed in the label l1
for NEGATIVE and DECIMAL PTS Button we did it similarly

2.FOR AIRTHMETIC BUTTON

if(e.getSource()==badd){ //FOR ADDITION


num1=Double.parseDouble(l1.getText());
z="";
l1.setText(z);
check=1; // 1 for the adddition
}

NOW here we store the value of label l1 into a variable num1 after converting into
double type which will be technically 1st number
and then and set label l1 to null
we will just use a check variable for getting that this particular airthmetic button(here
+) was clicked so we can do this operation in our = button

3.FOR EQUALS BUTTON


if(e.getSource()==bcalc){
num2=Double.parseDouble(l1.getText());
if(check==1)
xd =num1+num2;
if(check==2)
xd =num1-num2;
if(check==3)
xd =num1*num2;
if(check==4)
xd =num1/num2;
if(check==5)
xd =num1%num2;
l1.setText(String.valueOf(xd));
}

NOW again store the value of l1 into num2 variable which will be techincally 2nd
number
and then check the value of variable check and then do corresponding operation and
after that display result in label l1

4.FOR CLEAR BUTTON

if(e.getSource()==bclr){
num1=0;
num2=0;
check=0;
xd=0;
z="";
l1.setText(z);
}

here updated all the variable we use to its Default value 0


and set label l1 to null so that we can start our new calculation afterward

5.FOR BACKSPACE BUTTON

if(e.getSource()==bback){ // FOR BACKSPACE


zt=l1.getText();
try{
z=zt.substring(0, zt.length()-1);
}catch(StringIndexOutOfBoundsException f){return;}
l1.setText(z);
}

here just updates the value in l1 by removing last digits using substring function
and handled one StringIndexOutOfBoundsException which occur when our value in
label is null and still pressing back Button
sample Output:

Program Explanation:
1. When any button on the calculator is clicked, the function actionPerformed is
called. Get the button clicked using getActionCommand.

2. If the button clicked is a digit or decimal point, then either of the following is
executed :

a) The digit is concatenated to the the second number if no operator has been
encountered.

b) Otherwise, the digit is concatenated to the first number.

3. If the button clicked is the clear button, then clear the input field.

4. If the button clicked is the equals operator, then either of the following is executed :

a) If neither the first number nor the second number is empty, then compute the result
and display it in the frame

b) Otherwise, clear the input field.


5. If any of the button {+,-,*,/} is clicked, then either of the following is executed :

a) If either the operator or the second number is empty, then set the button clicked as
the operator.

b) Otherwise, compute the result and display it in the frame.

Conclusion: Thus we have studied calculators in java with the help of AWT/Swing with
event handling and perform addition, subtraction and multiplication .

Code:
import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public 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

l1.setBounds (100, 100, 100, 20);

l2.setBounds (100, 150, 100, 20);

l3.setBounds (100, 200, 100, 20);

t1.setBounds (300, 100, 100, 20);

t2.setBounds (300, 150, 100, 20);

t3.setBounds (300, 200, 100, 20);

b1.setBounds (100, 250, 50, 20);

b2.setBounds (170, 250, 50, 20);

b3.setBounds (250, 250, 50, 20);

b4.setBounds (330, 250, 50, 20);

b5.setBounds (410, 250, 50, 20);

//Adding components to the frame

f.add(l1);

f.add(l2);

f.add(l3);

f.add(t1);

f.add(t2);

f.add(t3);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);
b4.addActionListener(this);

b5.addActionListener(this);

f.setLayout (null);

f.setVisible(true);

f.setSize (600,400);

public void actionPerformed (ActionEvent e)

int n1=Integer.parseInt(t1.getText());

int n2=Integer.parseInt(t2.getText());

if(e.getSource()==b1)

t3.setText(String.valueOf(n1+n2));

if(e.getSource()==b2)

t3.setText(String.valueOf(n1-n2));

if (e.getSource()==b3)

t3.setText(String.valueOf(n1*n2));

if(e.getSource()==b4)

t3.setText(String.valueOf(n1/n2));

if(e.getSource()==b5)

{
System.exit(0);

public static void main(String...s)

new Calculator();

OUTPUT:

You might also like