0% found this document useful (0 votes)
7 views32 pages

Java GUI Calculator Project 2024-25

Uploaded by

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

Java GUI Calculator Project 2024-25

Uploaded by

slayyaryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Tab 1

PM SHRI JAWAHAR NAVODAYA


VIDYALAYA PHULBANI

INFORMATION TECHNOLOGY
2024-25

PROJECT TOPIC:
CALCULATOR
SUBMITTED BY: GROUP 1
NAME OF THE MEMBER 1: Rajashree Mishra
CBSE Roll Number:
NAME OF MEMBER 2: Arundhati Mallick
CBSE Roll Number:
NAME OF MEMBER 3: Rachana Mallick
CBSE Roll Number:

Under the Guidance of:


SHUBHAM MISHRA, PGT (CS)
CERTIFICATE
This is to certify that the project entitled “Calculator” submitted by
Rajashree Mishra of Class XII, Roll Number: [Insert Roll Number], in
partial fulfillment of the requirements for the subject Information
Technology as per the guidelines prescribed by the Central Board of
Secondary Education (CBSE), is a bonafide work carried out by him
under my supervision and guidance.
This project is an original work and has not been submitted previously to
any other school or institution for evaluation or award.
Mr. Shubham Mishra
PGT Computer Science
Supervisor,
PM SHRI Jawahar
Navodaya Vidyalaya Phulbani
Smt. Namita Rani Panda
Principal, PM SHRI Jawahar
Navodaya Vidyalaya Phulbani

ACKNOWLEDGMENT

1
It is a great pleasure and privilege to express my heartfelt gratitude and
sincere appreciation to Mr. Shubham Mishra, my project supervisor, for
his constant support, encouragement, motivation, and invaluable guidance
throughout the completion of this project. His expertise and insights have
inspired me, and I am deeply thankful for the knowledge and skills I have
gained under his mentorship.
I would also like to extend my gratitude to Smt. Namita Rani Panda,
Principal, PM SHRI Jawahar Navodaya Vidyalaya Phulbani, for
providing the resources and a conducive environment to carry out this
project successfully.
I am grateful to my teachers, classmates, and family members for their
unwavering support, encouragement, and assistance during this project.
Their contributions have been invaluable, whether through thoughtful
advice or moral support.
Lastly, I am grateful to The Almighty for blessing me with the strength
and wisdom to complete this endeavor successfully.
With sincere regards,
1)Rajashree Mishra
2)Arundhati Mallick
3)Rachana Mallick

Class XII, Information Technology

TABLE OF CONTENT
TABLE OF CONTENT 3
ABSTRACT 3
Key Features of the Calculator: 5

2
DESIGN: 5
1. User Interface Design (UI): 5
2. Functional Modules: 6
3. Code Organization: 6
ANALYSIS: 7
1. Strengths: 7
2. Limitations: 7
3. Performance: 7
4. Suggestions for Improvement: 8
OUTPUT SNAPSHOTS 25
CONCLUSION AND FUTURE SCOPE 31

ABSTRACT
This Java project implements a graphical user interface
(GUI)-based calculator using Swing, part of the Java

3
Foundation Classes (JFC). The calculator provides basic
arithmetic operations, such as addition, subtraction,
multiplication, and division, and advanced scientific
functions, including square, square root, and reciprocal.
Designed for usability, the calculator features an input field
for displaying results, buttons for operations, and a toggle for
enabling or disabling the calculator. The intuitive interface
uses components like JFrame, JTextField, JButton, and
JRadioButton, with event handling implemented through the
ActionListener interface to process user interactions. The
calculator supports additional functionality for input
management, such as clearing inputs or deleting specific
characters. Styled with custom fonts and color-coded buttons,
the layout offers a visually appealing and user-friendly
experience. This program demonstrates the power of Java’s
Swing framework for creating interactive desktop applications
and highlights the flexibility of event-driven programming for
crafting practical, aesthetically pleasing tools.

INTRODUCTION
This Java project is a GUI-based calculator that allows users to perform
basic and advanced operations like addition, subtraction, multiplication,
division, square root, squaring a number, and finding the reciprocal.
The calculator also includes features to clear or delete inputs and turn the
calculator on or off. The application demonstrates the integration of
various Swing components like JFrame, JTextField, JButton, and
JRadioButton to create an interactive and user-friendly interface.
Furthermore, it applies event-driven programming through the
ActionListener interface to handle button clicks and other user actions.

Key Features of the Calculator:

4
1. Arithmetic Operations: Supports addition, subtraction,
multiplication, and division.
2. Scientific Functions: Includes square, square root, and reciprocal
operations.
3. On/Off Toggle: Users can enable or disable the calculator,
enhancing usability and resource management.
4. Input Management: Provides buttons for clearing input fields or
deleting individual characters.
5. User-Friendly Design: The application incorporates color-coded
buttons and layouts for intuitive interaction.
The calculator provides essential arithmetic and scientific operations, such
as addition, subtraction, multiplication, division, square root, squaring a
number, and finding the reciprocal. It also features functionality to clear
and delete inputs and enable or disable the calculator.
This project is an excellent example of how Swing can build a functional
and visually appealing desktop application. It highlights the power of
Java's GUI capabilities and its event-driven programming paradigm.

DESIGN:
1. User Interface Design (UI):

● Swing Components:
○ JFrame: The main application window.
○ JTextField: For displaying numbers and results.
○ JButtons: Represent calculator keys for numbers and operations.
○ JRadioButton: Toggles to turn the calculator ON or OFF.
● Layout:
○ Buttons and components are placed using absolute positioning
(setBounds).
○ Color coding improves visual distinction (e.g., operators, numbers,
and actions like clear/delete).

2. Functional Modules:

● Event Handling:
○ Implemented using the ActionListener interface.

5
○ Each button has a specific event response, ensuring modularity.
● Mathematical Operations:
○ Basic arithmetic (addition, subtraction, multiplication, division).
○ Scientific functions (square, square root, reciprocal).
● State Management:
○ On/Off toggle controls enable or disable calculator functionality.
○ The calculation variable stores the type of operation selected for
subsequent execution.

3. Code Organization:

● Initialization:
○ Components are initialized in the constructor.
○ GUI components and event listeners are added through separate
methods (addComponents, addActionEvent).
● Action Handlers:
○ Centralized in the actionPerformed method, categorized by button
actions.
● Helper Methods:
○ enable() and disable() to manage the active state of the calculator.
● Main Method:
○ The entry point (main) initializes the application by creating an
instance of the Calculator class.

ANALYSIS:
1. Strengths:

● Usability:
○ The intuitive layout and color-coded buttons enhance user
experience.
○ Basic and advanced features cater to a wide range of user needs.
● Modularity:
○ Clear separation of responsibilities (e.g., GUI setup, event
handling, and mathematical logic).
● Expandability:
○ The program is structured to add additional features (e.g.,
trigonometric functions) with minimal refactoring.
● Reliability:
○ Proper input validation (e.g., handling of empty fields, preventing
duplicate dots in decimal numbers).

6
● Accessibility:
○ It includes a toggle for enabling and disabling the calculator, which
is a thoughtful feature for user convenience.

2. Limitations:

● Fixed Layout:
○ Absolute positioning (setBounds) makes the design rigid and less
adaptive to window resizing or screen size changes.
● Lack of Input from Keyboard:
○ The application relies solely on mouse clicks for input, which
might limit usability for some users.
● Limited Error Handling:
○ Division by zero or invalid input scenarios may not be gracefully
handled.
● No Memory Functionality:
○ Features like memory storage or recalling values are absent, which
are common in scientific calculators.

3. Performance:

● The program is lightweight and efficient for small-scale computations.


● As the GUI is Swing-based, performance is suitable for desktop
applications but may not scale well for more complex operations or
interfaces.

4. Suggestions for Improvement:

● Adaptive Layout:
○ Use a layout manager (e.g., GridLayout, FlowLayout) instead of
absolute positioning to make the design responsive.
● Keyboard Support:
○ Add key listeners to allow input via the keyboard.
● Advanced Features:
○ Include additional functions like memory storage (M+, M-, MR) or
trigonometric operations.
● Error Handling:
○ Enhance error handling for invalid operations like division by zero
or entering non-numeric input.
● Unit Testing:
○ Implement unit tests for mathematical operations to ensure
reliability.

7
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link];
import [Link];

class Calculator implements ActionListener {

double number, answer;


int calculation;

JFrame frame;
JLabel label = new JLabel();
JTextField textField = new JTextField();
JRadioButton onRadioButton = new
JRadioButton("on");
JRadioButton offRadioButton = new
JRadioButton("off");
JButton buttonZero = new JButton("0");
JButton buttonOne = new JButton("1");
JButton buttonTwo = new JButton("2");
JButton buttonThree = new JButton("3");
JButton buttonFour = new JButton("4");
JButton buttonFive = new JButton("5");
JButton buttonSix = new JButton("6");

8
JButton buttonSeven = new JButton("7");
JButton buttonEight = new JButton("8");
JButton buttonNine = new JButton("9");
JButton buttonDot = new JButton(".");
JButton buttonClear = new JButton("C");
JButton buttonDelete = new JButton("DEL");
JButton buttonEqual = new JButton("=");
JButton buttonMul = new JButton("x");
JButton buttonDiv = new JButton("/");
JButton buttonPlus = new JButton("+");
JButton buttonMinus = new JButton("-");
JButton buttonSquare = new JButton("x\u00B2");
JButton buttonReciprocal = new JButton("1/x");
JButton buttonSqrt = new JButton("\u221A");

// Colors for the UI


Color pink = new Color(239, 71, 111);
Color yellow = new Color(255, 209, 102);
Color green = new Color(6, 214, 160);
Color pastel = new Color(7, 59, 76);

Calculator() {
prepareGUI();
addComponents();
addActionEvent();
}

// Method to prepare the GUI


public void prepareGUI() {
frame = new JFrame();
[Link]("Calculator");
[Link](300, 490);
[Link]().setLayout(null);

[Link]().setBackground(yellow);
[Link](false);

9
[Link](null);
[Link](true);

[Link](JFrame.EXIT_ON_CLOS
E);
}

// Method to add all the components like the


numbers and symbols of all the operations to the
GUI
public void addComponents() {
[Link](250, 0, 50, 50);
[Link]([Link]);
[Link](label);

[Link](10, 40, 270, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](false);

[Link]([Link]
GHT);
[Link](textField);

[Link](10, 95, 60, 40);


[Link](true);
[Link](new Font("Arial",
[Link], 14));
[Link](yellow);
[Link]([Link]);
[Link](onRadioButton);

[Link](10, 120, 60, 40);


[Link](false);
[Link](new Font("Arial",
[Link], 14));
[Link](yellow);

10
[Link]([Link]);
[Link](offRadioButton);

ButtonGroup buttonGroup = new


ButtonGroup();
[Link](onRadioButton);
[Link](offRadioButton);

[Link](10, 230, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonSeven);

[Link](80, 230, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonEight);

[Link](150, 230, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonNine);

[Link](10, 290, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonFour);

[Link](80, 290, 60, 40);

11
[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonFive);

[Link](150, 290, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonSix);

[Link](10, 350, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonOne);

[Link](80, 350, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonTwo);

[Link](150, 350, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonThree);

[Link](150, 410, 60, 40);


[Link](new Font("Arial",
[Link], 20));

12
[Link](pastel);
[Link]([Link]);
[Link](buttonDot);

[Link](10, 410, 130, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](pastel);
[Link]([Link]);
[Link](buttonZero);

[Link](220, 350, 60, 100);


[Link](new Font("Arial",
[Link], 20));
[Link](green);
[Link](buttonEqual);

[Link](220, 110, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](green);
[Link](buttonDiv);

[Link](10, 170, 60, 40);


[Link](new Font("Arial",
[Link], 18));
[Link](green);
[Link](buttonSqrt);

[Link](220, 230, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](green);
[Link](buttonMul);

[Link](220, 170, 60, 40);

13
[Link](new Font("Arial",
[Link], 20));
[Link](green);
[Link](buttonMinus);

[Link](220, 290, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](green);
[Link](buttonPlus);

[Link](80, 170, 60, 40);


[Link](new Font("Arial",
[Link], 20));
[Link](green);
[Link](buttonSquare);

[Link](150, 170, 60,


40);
[Link](new Font("Arial",
[Link], 15));
[Link](green);
[Link](buttonReciprocal);

[Link](150, 110, 60, 40);


[Link](new Font("Arial",
[Link], 12));
[Link](pink);
[Link]([Link]);
[Link](buttonDelete);

[Link](80, 110, 60, 40);


[Link](new Font("Arial",
[Link], 12));
[Link](pink);
[Link]([Link]);
[Link](buttonClear);

14
}

// Method to add all the action listeners to


the buttons
public void addActionEvent() {
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}

// Method to add all the key listeners to the


buttons and the frame
@Override
public void actionPerformed(ActionEvent e) {
Object source = [Link]();

15
// If the on radio button is selected, the
calculator is enabled
if (source == onRadioButton) {
enable();
}
// If the off radio button is selected,
the calculator is disabled
else if (source == offRadioButton) {
disable();
}
// If the clear button is pressed, the
text field is cleared
else if (source == buttonClear) {
[Link]("");
[Link]("");
}
// If the delete button is pressed, the
last character is deleted
else if (source == buttonDelete) {
int length =
[Link]().length(); // Get the length of
the text field
int number = length - 1; // Get the
number of characters to delete

if (length > 0) { // If the length is


greater than 0, delete the last character
StringBuilder back = new
StringBuilder([Link]());
[Link](number);

[Link]([Link]());

16
if ([Link]().endsWith(""))
{ // If the text field is empty, set the label to
0
[Link]("");
}
} else if (source == buttonZero) { // If
the zero button is pressed, add a zero to the text
field
if ([Link]().equals("0")) {
// If the text field is 0, do nothing
return;
} else { // If the text field is not
empty, add a zero to the text field

[Link]([Link]() + "0");
}
}
// If the one button is pressed, add a one
to the text field and so on for all the other
buttons
else if (source == buttonOne) {
[Link]([Link]()
+ "1");
} else if (source == buttonTwo) {
[Link]([Link]()
+ "2");
} else if (source == buttonThree) {
[Link]([Link]()
+ "3");
} else if (source == buttonFour) {
[Link]([Link]()
+ "4");
} else if (source == buttonFive) {
[Link]([Link]()
+ "5");
} else if (source == buttonSix) {

17
[Link]([Link]()
+ "6");
} else if (source == buttonSeven) {
[Link]([Link]()
+ "7");
} else if (source == buttonEight) {
[Link]([Link]()
+ "8");
} else if (source == buttonNine) {
[Link]([Link]()
+ "9");
} else if (source == buttonDot) {
if ([Link]().contains("."))
{ // If the text field contains a dot, do nothing
return;
} else {

[Link]([Link]() + "."); //
If the text field does not contain a dot, add a
dot
}

}
// If the plus button is pressed, add a
plus to the text field and so on for all the
buttons
else if (source == buttonPlus) {
String str = [Link]();
number =
[Link]([Link]());
[Link]("");
[Link](str + "+");
calculation = 1;
} else if (source == buttonMinus) {
String str = [Link]();

18
number =
[Link]([Link]());
[Link]("");
[Link](str + "-");
calculation = 2;
} else if (source == buttonMul) {
String str = [Link]();
number =
[Link]([Link]());
[Link]("");
[Link](str + "X");
calculation = 3;
} else if (source == buttonDiv) {
String str = [Link]();
number =
[Link]([Link]());
[Link]("");
[Link](str + "/");
calculation = 4;
} else if (source == buttonSqrt) {
number =
[Link]([Link]());
Double sqrt = [Link](number);

[Link]([Link](sqrt));

} else if (source == buttonSquare) {


String str = [Link]();
number =
[Link]([Link]());
double square = [Link](number, 2);
String string =
[Link](square);
if ([Link](".0")) {

[Link]([Link](".0", ""));

19
} else {
[Link](string);
}
[Link]("(sqr)" + str);
} else if (source == buttonReciprocal) {
number =
[Link]([Link]());
double reciprocal = 1 / number;
String string =
[Link](reciprocal);
if ([Link](".0")) {

[Link]([Link](".0", ""));
} else {
[Link](string);
}
} else if (source == buttonEqual) {
switch (calculation) {
case 1:
answer = number +
[Link]([Link]());
if
([Link](answer).endsWith(".0")) {

[Link]([Link](answer).replace(
".0", ""));
} else {

[Link]([Link](answer));
}
[Link]("");
break;
case 2:
answer = number -
[Link]([Link]());

20
if
([Link](answer).endsWith(".0")) {

[Link]([Link](answer).replace(
".0", ""));
} else {

[Link]([Link](answer));
}
[Link]("");
break;
case 3:
answer = number *
[Link]([Link]());
if
([Link](answer).endsWith(".0")) {

[Link]([Link](answer).replace(
".0", ""));
} else {

[Link]([Link](answer));
}
[Link]("");
break;
case 4:
answer = number /
[Link]([Link]());
if
([Link](answer).endsWith(".0")) {

[Link]([Link](answer).replace(
".0", ""));
} else {

[Link]([Link](answer));
}

21
[Link]("");
break;

}
}

// method to enable the calculator


public void enable() {
[Link](false);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);
[Link](true);

22
}

// method to disable the calculator


public void disable() {
[Link](true);
[Link](false);
[Link]("");
[Link](" ");
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
[Link](false);
}

public static void main(String[] args)


{
new Calculator(); // create a new
Calculator object
}

23
}

OUTPUT SNAPSHOTS
Main screen

24
Addition :

Output :

25
Subtraction :

Multiplication :

26
Division:

27
Output:

Square:

28
CONCLUSION AND FUTURE SCOPE
The above Java calculator program demonstrates the
implementation of a basic graphical user interface (GUI)
calculator using Swing components. It provides standard
arithmetic operations such as addition, subtraction, multiplication,
and division. The program also includes features such as decimal
input, negation, clearing, and deleting input values, making it
user-friendly. ActionListener facilitates interactive operations,
allowing users to perform calculations seamlessly. This project
highlights how object-oriented programming principles and Java's
rich library can be utilized to create functional desktop
applications.
The future scope of this calculator application includes enhancing
its functionality with advanced mathematical operations such as
trigonometric functions, logarithms, square roots, and exponents,
along with memory storage features like M+, M-, and MR. The
user interface can be improved with modern styling using JavaFX
or libraries like FlatLaf, offering themes and customization

29
options for a better user experience. Robust error-handling
mechanisms can be implemented to handle scenarios like division
by zero and provide descriptive error messages for invalid inputs.
To broaden accessibility, cross-platform support can be extended
to mobile devices and standalone executables, while localization
can add multi-language support for diverse users. Integration with
external tools like spreadsheets and scientific applications can
expand functionality, and an educational mode offering step-by-
step explanations could make it a valuable resource for students
learning mathematics.

30

You might also like