0% found this document useful (0 votes)
4 views

Java Proj Report

Uploaded by

tsaeed23extc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Proj Report

Uploaded by

tsaeed23extc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

A Report on

GUI Calculator
for

Java Project Third Year, (TY Sem-V)

in

Electronics & Telecommunication Engineering


by

1. A-549 Atharv Shirkar


2. A-551 Saeed Thakur
3. A-553 Aditya Thorat
4. A-556 Dipak Yarole

Under the guidance of

Prof. Yogesh Kene

UNIVERSITY OF MUMBAI

A. Y. 2023-24

Mahatma Education Society’s


Pillai College of Engineering
(Autonomous)
Accredited A+ by NAAC
Dr. K. M. Vasudevan Pillai Campus,
Plot No. 10, Sector-16, New Panvel - 410206
INTRODUCTION

A simple calculator application developed using Java's GUI toolkit, Swing. It performs basic
arithmetic operations like addition, subtraction, multiplication, and division. This project is a user-
friendly calculator application developed using Java’s Swing toolkit, designed to provide a smooth
and interactive experience for performing arithmetic operations. Swing, being a lightweight GUI
toolkit, enables the creation of responsive desktop applications, and this calculator leverages its
components to deliver a familiar interface with basic features. Users can perform fundamental
mathematical operations such as addition, subtraction, multiplication, and division, making it
suitable for quick and everyday calculations. The calculator’s layout is designed for simplicity,
ensuring that users of all levels, from beginners to advanced, can interact with it comfortably. It
features essential elements such as buttons for numbers, operators, and a display field to show the
results of computations in real-time. Each operation is executed accurately, providing immediate
feedback to the user.

PROBLEM STATEMENT
In today’s digital era, calculators are essential tools for performing quick and accurate
mathematical operations. While most people rely on either physical calculators or mobile apps,
having a custom-built calculator offers the opportunity to learn about software development and
GUI design. The challenge is to develop a simple, user-friendly calculator using Java’s Swing
library that provides basic arithmetic operations such as addition, subtraction, multiplication, and
division. This project aims to demonstrate the creation of a functioning desktop calculator with an
interactive interface, serving both as a tool for users and as an educational exercise in
programming.

OBJECTIVES

1. To design and implement a basic calculator application using Java Swing.


2. To provide functionality for basic arithmetic operations (addition, subtraction, multiplication,
division).
3. To create a user-friendly graphical interface that is intuitive for users.
4. To learn and apply Java’s GUI components, event listeners, and layout managers.
5. To ensure error handling for scenarios like division by zero and invalid input.
METHODOLOGY

1. Understanding Swing Library: Familiarizing with Java Swing components such as JFrame,
JButton, JTextField, and layout managers (GridLayout, BorderLayout).

2. Designing the User Interface: Creating a layout for the calculator that includes buttons for
digits, operations, and a display field for showing the result.

3. Event Handling: Implementing event listeners to respond to user inputs, such as button
clicks, and performing the corresponding arithmetic operation.

4. Arithmetic Logic Implementation: Developing methods to process inputs, perform


calculations, and display results.

5. Error Handling: Implementing checks for invalid operations (e.g., division by zero) and
input validation.

6. Testing: Iteratively testing the calculator for correct functionality across different input
combinations.

7. Optional Extensions: Keep the code modular to allow future additions like scientific
functions or theming options.

ALGORITHM

1. Initialize Components: Create the main frame and layout for the calculator interface.

2. Add Buttons and Display Field: Place buttons for digits (0-9), operations (+, -, *, /), and a text
field to show the results.

3. Set up Event Listeners:


a. On digit button press, append the corresponding number to the display.
b. On operation button press, store the current number and operation.
c. On "=" button press, perform the selected operation between the stored and current
numbers.
4. Perform Calculation: Based on the operation, execute addition, subtraction, multiplication, or
division. Update the display with the result.

5. Error Handling: Display an error message in case of invalid operations (e.g., divide by zero).

6. Clear Functionality: Provide a "C" button to reset the calculator’s state.


SUMMARY
This project focused on developing a basic calculator application using Java’s Swing framework.
The application offers a simple graphical user interface where users can input numbers, select
arithmetic operations, and view the results. The core learning areas included GUI design, event
handling, and basic arithmetic logic implementation in Java. A fully functional calculator was
developed, capable of performing addition, subtraction, multiplication, and division, with error
handling for invalid inputs.

OUTPUT
CODE

// Creating a Calculator using java.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JTextField textfield;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton,subButton,mulButton,divButton;
JButton decButton, equButton, delButton, clrButton, negButton;
JPanel panel;
Font myFont = new Font("Ink Free",Font.BOLD,30);
double num1=0,num2=0,result=0;
char operator;

Calculator(){

frame = new JFrame("Calculator");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);

textfield = new JTextField();


textfield.setBounds(50, 25, 300, 50);
textfield.setFont(myFont);
textfield.setEditable(false);

addButton = new JButton("+");


subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Del");
clrButton = new JButton("Clr");
negButton = new JButton("(-)");

functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;

for(int i =0;i<9;i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}

for(int i =0;i<10;i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}

negButton.setBounds(50,430,100,50);
delButton.setBounds(150,430,100,50);
clrButton.setBounds(250,430,100,50);

panel = new JPanel();


panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4,4,10,10));

panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);

frame.add(panel);
frame.add(negButton);
frame.add(delButton);
frame.add(clrButton);
frame.add(textfield);
frame.setVisible(true);
}
public static void main(String[] args) {
Calculator calc = new Calculator();
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<10;i++) {
if(e.getSource() == numberButtons[i]) {

textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}
if(e.getSource()==decButton) {
textfield.setText(textfield.getText().concat("."));
}
if(e.getSource()==addButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='+';
textfield.setText("");
}
if(e.getSource()==subButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='-';
textfield.setText("");
}
if(e.getSource()==mulButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='*';
textfield.setText("");
}
if(e.getSource()==divButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='/';
textfield.setText("");
}
if(e.getSource()==equButton) {
num2=Double.parseDouble(textfield.getText());

switch(operator) {
case'+':
result=num1+num2;
break;
case'-':
result=num1-num2;
break;
case'*':
result=num1*num2;
break;
case'/':
result=num1/num2;
break;
}
textfield.setText(String.valueOf(result));
num1=result;
}
if(e.getSource()==clrButton) {
textfield.setText("");
}
if(e.getSource()==delButton) {
String string = textfield.getText();
textfield.setText("");
for(int i=0;i<string.length()-1;i++) {

textfield.setText(textfield.getText()+string.charAt(i));
}
}
if(e.getSource()==negButton) {
double temp = Double.parseDouble(textfield.getText());
temp*=-1;
textfield.setText(String.valueOf(temp));
}

}
APPLICATIONS

1. Educational Tool: Serves as a learning platform for beginners in Java GUI


development and event handling.
2. Personal Use: Useful for quick arithmetic calculations on a desktop.
3. Basis for Future Projects: Provides a foundation for building more complex
applications, such as scientific calculators or financial tools.
4. Demonstration of GUI Concepts: Acts as a simple yet effective example of how to
develop interactive desktop applications using Swing.

CONCLUSION
The development of a simple calculator application using Java Swing successfully met the project
objectives. Through this process, the concepts of GUI programming, event-driven programming, and
layout management were reinforced. The final application is user-friendly, performs the required
arithmetic operations, and handles invalid inputs appropriately. This project provided practical
experience in Java Swing, preparing the foundation for more complex GUI-based applications in the
future.

REFERENCES
1. Herbert Schildt, “Java: The Complete Reference,” Oracle Press.
2. Oracle Documentation: Java Swing API
3. Java Tutorials: Creating a GUI with Swing
4. GeeksforGeeks: Java Swing Tutorial

You might also like