PS 7
PS 7
PS 7
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 :
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 :
Algorithm/Steps:
LOGIC PART -I
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
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
if(e.getSource()==bclr){
num1=0;
num2=0;
check=0;
xd=0;
z="";
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.
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
a) If either the operator or the second number is empty, then set the button clicked as
the operator.
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;
//Declaring Objects
Calculator ()
{
//Giving Coordinates
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);
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);
new Calculator();
OUTPUT: