Calculator
Calculator
CALCULATOR IN By
o Users can interact with the application through an intuitive interface consisting of digit
buttons (0-9), an equals button, arithmetic operation buttons, a decimal point button, and
a clear button to reset the input.
o The calculator dynamically updates the display as users enter numbers and operations,
providing real-time feedback on the input and computed results.
o The calculator employs Java's event-driven programming model, where each button click
triggers an event handled through an ActionListener. The logic behind calculations is
managed using an array to store numerical inputs and a flag system to track operator
selection.
o The program ensures smooth operation by checking user input before performing
calculations, thereby preventing common errors such as consecutive operator inputs or
division by zero.Graphically, the application is structured within a JFrame window, with
components such as JLabel for display and JButton elements for user input.
o The layout is designed for ease of use, featuring clearly distinguishable buttons with large
fonts and appropriate spacing to enhance accessibility. The calculator also implements
basic error handling mechanisms to improve usability and maintain stability during
operations.
o Additionally, it lays the foundation for future enhancements, such as scientific functions,
memory operations, and a more advanced user interface.By developing this calculator,
programmers can gain hands-on experience in building GUI applications, managing user
interactions, and implementing logical processing in Java, making it an ideal project for
beginners and enthusiasts in software development.
OBJECTIVE
The primary objective of this Java program is to develop a Graphical User Interface (GUI)
Calculator using Java Swing that allows users to perform basic arithmetic operations in an
intuitive and interactive manner.
The calculator aims to provide a functional and user-friendly interface where users can input
numbers, perform calculations, and view results dynamically.
• The application is built using Java Swing, ensuring a visually structured and interactive
interface.
• The layout includes a numeric keypad (0-9), operator buttons, a decimal point button, an
equals button, and a clear button for resetting calculations.
• The display panel (JLabel) provides real-time updates based on user input, improving the
usability of the application.
• The buttons are designed with appropriate sizes and fonts to enhance accessibility.
• Each button click triggers an event, handled using ActionListener to update the display
and execute calculations.
• The program follows an event-driven approach, responding dynamically to user
interactions without requiring manual input through a console.
4. Ensure Proper Input Handling and Error Prevention
• The program uses an array (float[] a = new float[2]) to store numerical values and a flag
(x) to track selected operations.
• The = button should execute the stored operation on the two input numbers and display
the result correctly.
• After a calculation is performed, the program should reset appropriately to allow new
inputs.
o This project is a GUI-based Calculator developed using Java Swing, designed to perform
basic arithmetic operations such as addition, subtraction, multiplication, and division.
o The calculator provides an intuitive interface where users can input numbers using on-
screen buttons and execute calculations dynamically.
o The program follows an event-driven approach, where each button click triggers an
action that updates the display and processes the arithmetic operation accordingly. The
graphical interface is built using Swing components such as JFrame, JButton, and JLabel,
ensuring a smooth and visually appealing user experience.
o The calculator features a numeric keypad (0-9), an equals button, operator buttons, a
decimal point button, and a clear button, allowing users to perform calculations
seamlessly.
o One of the key aspects of this program is its efficient input handling. It ensures that users
cannot enter invalid inputs, such as multiple consecutive operators, improper decimal
usage, or division by zero, thereby preventing errors and ensuring stable performance.
o Through this project, learners can gain hands-on experience in developing interactive
GUI applications, managing user inputs, and implementing basic arithmetic logic in Java,
making it an ideal project for beginners and aspiring Java developers.
Methodology
The development of the GUI-based Calculator follows a systematic approach, incorporating Java
Swing for GUI components, event-driven programming for user interactions, and logical
processing for arithmetic operations.
The calculator's interface is created using Java Swing, ensuring an interactive and visually
structured layout. The design includes:
• A main application window (JFrame) that serves as the container for all UI components.
• A display label (JLabel) to show user inputs and results.
• Buttons (JButton) for digits (0-9), arithmetic operators (+, -, ×, ÷), a decimal point, an
equals button (=), and a clear button (C).
• Each button is properly positioned within the JFrame using the setBounds() method,
ensuring a structured layout.
Since Java Swing applications rely on user interactions, the program follows an event-driven
approach:
• Each button is assigned an ActionListener, which detects button clicks and triggers
appropriate actions.
• When a user clicks a digit button, the number is appended to the display.
• When an operator button is clicked, the first number is stored, and the display resets for
the next input.
• The equals (=) button performs the arithmetic operation based on the stored operator and
operands.
• The clear (C) button resets the display and stored values to allow fresh calculations.
• The final result is displayed dynamically using the JLabel component, allowing users to
see real-time updates. The program ensures that:
• After pressing =, the result remains on the screen until a new input is provided.
• The display resets upon pressing C, allowing fresh calculations.
Source Code
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public Calculator() {
jf = new JFrame("Calculator");
jf.getContentPane().setBackground(Color.BLACK);
jf.setLayout(null);
jf.setSize(600, 600);
jf.setLocation(500, 150);
jf.add(displayLabel);
sevenB = new JButton("7");
sevenB.setBounds(30, 130, 80, 80);
sevenB.setFont(new Font("Arial", Font.PLAIN, 40));
jf.add(sevenB);
sevenB.addActionListener(this);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sevenB) {
if (opr) {
displayLabel.setText("7");
opr = false;
} else {
displayLabel.setText(displayLabel.getText() + "7");
} else {
displayLabel.setText(displayLabel.getText() + "3");
} else {
displayLabel.setText(displayLabel.getText() + "4");
} else {
displayLabel.setText(displayLabel.getText() + "6");
} else {
displayLabel.setText(displayLabel.getText() + "0");
opr = false;
displayLabel.setText(".");
} else {
displayLabel.setText(displayLabel.getText() + ".");
}
} else if (e.getSource() == plusB) {
opr = true;
old = displayLabel.getText();
x = 1;
a[i] = Float.parseFloat(old);
i = i + 1;
} else if (x == 2) {
a[0] -= a[1];
} else if (x == 3) {
a[0] *= a[1];
} else if (x == 4) {
a[0] /= a[1];
}
i = 1;
x = 0;
displayLabel.setText(a[0] + "");
}
}
Output:
Conclusion
o The GUI-based Calculator using Java Swing efficiently performs basic arithmetic
operations with a user-friendly interface. It demonstrates event-driven programming, GUI
design, and input validation, serving as a foundation for advanced Java applications.
Future improvements could include scientific functions and enhanced UI design.