JAVA

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

Subject name and code: JAVA(22412) Academic Year: 2022-23

Couse Name: IF4I Semester: Fourth

A STUDY ON

Prepare A Calculator Design Using An Applet


MICRO PROJECT REPORT
Submitted in December 2023 by the group of 3 students
Sr. Roll No. Full Name of Students Enrollment Seat No.
No. ( sem-4) No. (semester-3)
1 DSD02 PINIL BABURAV CHAKAR 2209350316
2 DSD14 ROHIT RAJENDRA SONAWANE 2209350328
3 DSD10 SURAJ GURUNATH RAUT 2209350324

Under the Guidance of


PROF. MR. MAYUR KHARIK
In
Diploma Board of Technical Education,
ISO 9001:2008 (ISO/IEC-27001:201
SHIVAJIRAO S. JONDHALE POLYTECHNIC, ASANGOAN
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION,
MUMBAI

CERTIFICATES
AS
This is to certify that Mr. PINIL BABURAV CHAKAR
Roll No: DSD02 of Fourth Semester of Information Technology Programme in
Engineering & Technology at Shivajirao. S Jondhale Polytechnic Asangaon
(EAST) ShahapurS421601 has completed the Micro Project Satisfactorily in
Subject: JAVA
In the academic Year 2023 as prescribed curriculum of I Scheme.

Place: ASANGOAN Enrollment No: 2209350316


Date: / /2023 Exam Seat No:

Project Guide Heat of Department Principal

Seal of
institute
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION,
MUMBAI

CERTIFICATES

This is to certify that Mr. ROHIT RAJENDRA SONAWANE


Roll No: DSD14 of Fourth Semester of Information Technology Programme in
Engineering & Technology at Shivajirao. S Jondhale Polytechnic Asangaon
(EAST) Shahapur421601 has completed the Micro Project Satisfactorily in
Subject: JAVA
In the academic Year 2023 as prescribed curriculum of I Scheme.

Place: ASANGOAN Enrollment No: 2209350328


Date: / /2023 Exam Seat No:

Project Guide Heat of Department Principal

Seal of
institute
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION,
MUMBAI

CERTIFICATES

This is to certify that Mr. SURAJ GURUNATH RAUT


Roll No: DSD10 of Fourth Semester of Information Technology Programme in
Engineering & Technology at Shivajirao. S Jondhale Polytechnic Asangaon
(EAST) Shahapur421601 has completed the Micro Project Satisfactorily in
Subject: JAVA
In the academic Year 2023 as prescribed curriculum of I Scheme.

Place: ASANGOAN Enrollment No: 2209350324


Date: / /2023 Exam Seat No:

Project Guide Heat of Department Principal

Seal of
institute
-:Introduction:-
Problem Description :
We have to write a program in Java such that it creates a calculator which
allows basic operations of addition, subtraction, multiplication and division.

Expected Input and Output


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 :
Note: Join free Sanfoundry classes at Telegram or Youtube advertisement
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
Take Java Programming Tests Now!
When an division expression "565.0/0" is typed,
it is expected that the error is displayed as "565.0/0=Zero Divison Error".
Problem Solution
1. Create a text field to accept the expression and display the output also.
2. Create buttons for digits and a decimal point.
3. Create a button to clear the complete expression.
4. Create the buttons for operations, that is for addition, subtraction,
multiplication and division and an equals button to compute the result.
5. Add ActionListener to all the buttons.
6. Compute the result and display in the text field.

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.
Source Code :

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Calculator extends Applet implements ActionListener

String cmd[] = {"+", "-", "*", "/", "=", "C"};

int pv = 0;

String op = "";

Button b[] = new Button[16];

TextField t1 = new TextField(10);

public void init()

setLayout(new BorderLayout());

add(t1, "North");

t1.setText("0");
Panel p = new Panel();

p.setLayout(new GridLayout(4, 4));

for (int i = 0; i < 16; i++)

if (i < 10)

b[i] = new Button(String.valueOf(i));

else

b[i] = new Button(cmd[i % 10]);

b[i].setFont(new Font("Arial", Font.BOLD, 25));

p.add(b[i]);

add(p, "Center");

b[i].addActionListener(this);

public void actionPerformed(ActionEvent ae)

{
int res = 0;

String cap = ae.getActionCommand();

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

if (cap.equals("C"))

t1.setText("0");

pv = 0; 5

cv = 0;

res = 0;

op = "";

else if (cap.equals("="))

res = 0;

if (op == "+")

res = pv + cv;
else if (op == "-")

res = pv - cv;

else if (op == "*")

res = pv * cv; 6

else if (op == "/")

res = pv / cv;

t1.setText(String.valueOf(res));

else if (cap.equals("+") || cap.equals("-") || cap.equals("*") || cap.equals("/"))

pv = cv;

op = cap;

t1.setText("0");

else

{7
int v = cv * 10 + Integer.parseInt(cap);

t1.setText(String.valueOf(v));

/*

<applet code="Calculator.class" width=401 height=391>

</applet>

*/ 8

Output :
thank you !!!

You might also like