Java Proj Report
Java Proj Report
GUI Calculator
for
in
UNIVERSITY OF MUMBAI
A. Y. 2023-24
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. 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.
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.
5. Error Handling: Display an error message in case of invalid operations (e.g., divide by zero).
OUTPUT
CODE
Calculator(){
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.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
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