Java Project
Java Project
VISVESVARAYA TECHNOLOGICAL
UNIVERSITY BELAGAVI-590018
JAVA MINI PROJECT SYNOPSIS ON
IN
Name:dmfnjdvfdkvmkdmvd
USN:fmcdkfvmdkvmkdmvd
Prof. KIRAN M P
Assistant professor, Dept. of CSE
GEC, Hassan.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
2024-2025
1Page
Government Engineering College
Java Assessment 2024-25
Hassan
Abstract
This Java-based mini calculator application is a simple
implementation of a GUI-based calculator, similar to those found on
mobile devices. Developed using the Swing library, the calculator
provides an intuitive and user-friendly interface. It supports basic
arithmetic operations such as addition, subtraction, multiplication,
and division, along with essential features like clearing the input.
The application operates through a grid layout consisting of buttons
for digits (0-9), arithmetic operators (+, -, *, /), a clear button (C), and
an equals button (=). User inputs are handled through button clicks,
and operations are processed based on the selected operator. Error
handling is included to manage invalid inputs or operations such as
division by zero, displaying appropriate error messages on the
calculator's screen.
This program demonstrates foundational concepts of event-driven
programming, GUI design, and exception handling in Java, making it
suitable as a beginner-level project for learning Java programming.
2Page
Government Engineering College
Java Assessment 2024-25
Hassan
Introduction
This document provides a detailed report on a Java-based calculator program implemented
using Swing for the graphical user interface (GUI). The program supports basic arithmetic
operations and simulates a calculator with a visually appealing interface.
1. Program Overview
The calculator program is designed to provide the following functionality:
Basic Arithmetic Operations: Addition, subtraction, multiplication, and division.
Clear Operation: Reset the calculator to its initial state.
Memory Features: Placeholder buttons for memory operations (e.g., MC, MR, M+,
M-), though functionality for these features is not implemented.
Visual Appeal: An aesthetically pleasing GUI with color-coded buttons and an easy-
to-read display.
3Page
Government Engineering College
Java Assessment 2024-25
Hassan
4. Strengths
1. Readable GUI: The use of color-coded buttons and a large, readable text field
enhances usability.
2. Error Handling: Provides feedback for invalid inputs and division by zero.
3. Scalable Design: Modular implementation allows for easy addition of new features,
such as advanced functions or memory operations.
4Page
Government Engineering College
Java Assessment 2024-25
Hassan
6. Recommendations
Implement memory functionality for enhanced usability.
Refactor the code to separate concerns more effectively, such as moving arithmetic
logic to a dedicated class.
Introduce unit tests to verify correctness of arithmetic operations.
Consider making the calculator responsive to resizing by implementing
GridBagLayout or similar layouts.
Code :
1 import javax.swing.*;
2 import java.awt.*;
5Page
Government Engineering College
Java Assessment 2024-25
Hassan
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5
6 public class Calculator {
7 private JFrame frame;
8 private JTextField textField;
9 private String currentExpression = "";
10 private double result = 0;
11 private String operator = "";
12 private boolean isOperatorPressed = false;
13
14 public Calculator() {
15 frame = new JFrame(" Java Calculator ");
16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17 frame.setSize(400, 600);
18 frame.setLayout(new BorderLayout());
19
20 // Configure text field with increased height
21 textField = new JTextField("0");
22 textField.setHorizontalAlignment(JTextField.RIGHT);
23 textField.setEditable(false);
24 textField.setFont(new Font("Arial", Font.BOLD, 36)); // Increase font size for better
25 visibility
textField.setBackground(Color.BLACK);
26
textField.setForeground(Color.WHITE);
27
textField.setPreferredSize(new Dimension(400, 80)); // Set custom height for the
28
text field
29
6Page
Government Engineering College
Java Assessment 2024-25
Hassan
30 frame.add(textField, BorderLayout.NORTH);
31
32 // Configure button panel
33 JPanel panel = new JPanel();
34 panel.setLayout(new GridLayout(5, 4, 10, 10));
35 panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
36
37 String[] buttons = {
38 "MC", "MR", "M+", "M-",
39 "7", "8", "9", "/",
40 "4", "5", "6", "*",
41 "1", "2", "3", "-",
42 "C", "0", "=", "+"
43 };
44
45 for (String text : buttons) {
46 JButton button = new JButton(text);
47 button.setFont(new Font("Arial", Font.BOLD, 20));
48 if ("0123456789".contains(text)) {
49 button.setBackground(Color.LIGHT_GRAY);
50 } else if ("C=+-*/".contains(text)) {
51 button.setBackground(Color.ORANGE);
52 button.setForeground(Color.WHITE);
53 } else {
54 button.setBackground(Color.DARK_GRAY);
55 button.setForeground(Color.WHITE);
56 }
7Page
Government Engineering College
Java Assessment 2024-25
Hassan
57 button.setFocusPainted(false);
58 button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
59 button.addActionListener(new ButtonClickListener());
60 panel.add(button);
61 }
62
63 frame.add(panel, BorderLayout.CENTER);
64 frame.setVisible(true);
65 }
66
67 private class ButtonClickListener implements ActionListener {
68 @Override
69 public void actionPerformed(ActionEvent e) {
70 String command = e.getActionCommand();
71
72 try {
73 if ("0123456789".contains(command)) {
74 if (isOperatorPressed) {
75 textField.setText(command);
76 isOperatorPressed = false;
77 } else {
78 textField.setText(textField.getText().equals("0") ? command :
79 textField.getText() + command);
}
80
} else if (command.equals("C")) {
81
textField.setText("0");
82
currentExpression = "";
83
8Page
Government Engineering College
Java Assessment 2024-25
Hassan
84 result = 0;
85 operator = "";
86 } else if ("+-*/".contains(command)) {
87 if (!operator.isEmpty()) {
88 calculate(Double.parseDouble(textField.getText()));
89 } else {
90 result = Double.parseDouble(textField.getText());
91 }
92 operator = command;
93 isOperatorPressed = true;
94 currentExpression += textField.getText() + " " + operator + " ";
95 textField.setText(currentExpression);
96 } else if (command.equals("=")) {
97 if (!operator.isEmpty()) {
98 calculate(Double.parseDouble(textField.getText()));
99 currentExpression = "";
100 operator = "";
101 textField.setText(String.valueOf(result));
102 }
103 } else if ("MC MR M+ M-".contains(command)) {
104 textField.setText("Memory feature not implemented.");
105 }
106 } catch (NumberFormatException ex) {
107 textField.setText("Error");
108 }
109 }
110
9Page
Government Engineering College
Java Assessment 2024-25
Hassan
10 P a g e
Government Engineering College
Java Assessment 2024-25
Hassan
138 }
139
140 public static void main(String[] args) {
SwingUtilities.invokeLater(Calculator::new);
}
}
Code Explanation:
1. Class Definition: Calculator
This class defines the calculator application. It initializes the components and controls
their interaction.
Variables:
o frame: A JFrame object that represents the main window of the calculator.
o textField: A JTextField where the current calculation or result is displayed.
o currentExpression: A string that stores the ongoing mathematical expression
as the user inputs numbers and operators.
o result: A double that stores the current result of the calculation.
o operator: A string that holds the current arithmetic operator (e.g., "+", "-",
"*", "/").
o isOperatorPressed: A boolean flag that tracks whether an operator has been
pressed, ensuring correct input formatting.
2. Constructor: Calculator()
JFrame Setup:
o A JFrame is created with the title "Java Calculator".
o The frame's default close operation is set to EXIT_ON_CLOSE, so the program
will terminate when the window is closed.
o The frame size is set to 400x600 pixels and uses BorderLayout.
Text Field Setup:
o The text field (textField) is used to display the result or the current
expression.
11 P a g e
Government Engineering College
Java Assessment 2024-25
Hassan
o The text is aligned to the right, and the font size is increased to improve
visibility.
o The background is set to black, and the text color is white for contrast.
o The textField is not editable, so the user cannot manually type into it.
o The preferred size of the textField is set to 400x80 pixels.
Button Panel Setup:
o A JPanel with a GridLayout is used to arrange the buttons in a 5x4 grid.
o There is some padding around the buttons, achieved by setting an empty
border.
o An array of button labels (buttons) is defined, and each button is created and
added to the panel.
o Buttons for numbers (0-9) and basic operators (+, -, *, /) are created and
assigned specific backgrounds:
Number buttons are light gray.
Operator buttons are orange with white text for visibility.
Other memory-related buttons (e.g., "MC", "MR", "M+", "M-") are
dark gray with white text.
o An ActionListener (ButtonClickListener) is added to each button to handle
user interactions.
Visibility:
o The frame is set visible with frame.setVisible(true).
3. Inner Class: ButtonClickListener
This class implements the ActionListener interface to handle button clicks.
Method: actionPerformed(ActionEvent e)
o This method is called when a button is clicked. It processes the button's label
(e.getActionCommand()) and updates the display accordingly:
o Number buttons (0-9):
If an operator has been pressed, the text field is cleared, and the
clicked number is displayed.
12 P a g e
Government Engineering College
Java Assessment 2024-25
Hassan
13 P a g e
Government Engineering College
Java Assessment 2024-25
Hassan
How It looks?
14 P a g e
Government Engineering College
Java Assessment 2024-25
Hassan
Conclusion
The program demonstrates a solid foundation for a functional calculator with a user-friendly
GUI. While it covers basic operations effectively, there is room for enhancements in
functionality, user feedback, and modularity. With further development, this calculator can
serve as a robust tool for arithmetic computations.
15 P a g e
Government Engineering College
Java Assessment 2024-25
Hassan
16 P a g e