DEVELOPING A CALCULATOR
Submitted in partial fulfillment of the requirements of the degree
BACHELOR OF ENGINEERING IN COMPUTER
ENGINEERING
By
KHAN MOHAMMED HASSAAN ZAHID (231220)
ARYAN BHANDARI (231204)
Name of the Mentor
Prof. Fatima Ansari
Department of Computer Engineering
M H Saboo Siddik College of Engineering Mumbai
University of Mumbai
(AY 2023-24)
CERTIFICATE
This is to certify that the Mini Project entitled “DEVELOPING A
CALCULATOR” is a bonafide work of Khan Mohammed Hassaan Zahid
(231220) and Aryan Bhandari(231204). Submitted to the University of Mumbai
in partial fulfillment of the requirement for the award of the degree of “Bachelor
of Engineering” in “Computer Engineering” .
(Prof. Fatima Ansari)
Mentor
(Prof. ) (Prof. )
Head of Department Principal
Mini Project Approval
This Mini Project entitled “DEVELOPING A CALCULATOR” by
KhanMohammed Hassaan Zahid (231220) and Aryan Bhandari(231204) is
approved for the degree of Bachelor of Engineering in Computer Engineering.
Examiners
1………………………………………
(Internal Examiner Name & Sign)
2…………………………………………
(External Examiner name & Sign)
Date:
Place:
Contents
1. Introduction
2. Problem Statement
3. Project Objectives
4. Technology Used
5. Code and Output
6. Implementation Details
7. Code Breakdown
8. Future Enhancements
9. Conclusion
10. References
DEVELOPING A CALCULATOR
Introduction
In today’s digital age, calculators play a vital role in simplifying mathematical
computations for users of all ages. This project focuses on developing a basic
calculator application using Java’s AWT (Abstract Window Toolkit), aiming to
provide a user-friendly interface for performing fundamental arithmetic
operations such as addition, subtraction, multiplication, and division. By
creating this application, we not only gain practical experience in programming
but also explore the principles of GUI development. The project serves as an
excellent introduction to event-driven programming and object-oriented design
concepts in Java.
The calculator is designed to be intuitive, allowing users to interact with it
seamlessly. It features a clean display that shows inputs and results, as well as a
grid of buttons for easy access to numbers and operations. The application
incorporates basic error handling to manage situations like division by zero,
ensuring a robust user experience. Ultimately, this project demonstrates how
programming can be applied to create functional tools that enhance everyday
tasks, while also providing a foundation for more advanced features and
enhancements in future iterations.
Problem Statement
The objective of this project is to develop a simple calculator application using
Java's AWT framework that enables users to perform basic arithmetic
operations, including addition, subtraction, multiplication, and division. The
application should provide a clear and user-friendly graphical interface that
allows for straightforward input of numbers and operators. Additionally, it must
handle common input errors, such as division by zero, to ensure a smooth user
experience. This project aims to enhance programming skills in Java while
demonstrating practical applications of GUI development and event-driven
programming.
Project Objectives
• To develop a graphical user interface (GUI) for basic arithmetic
calculations.
• To implement essential functionalities like input handling, operation
processing, and error management.
• To provide an opportunity to practice Java programming and GUI
development.
Technology Used
• Programming Language: Java
• Libraries: AWT (Abstract Window Toolkit)
Code :
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener {
// Declare UI components
private TextField display;
private String operator = ""; // Initialize operator
private double num1, num2, result;
public Calculator() {
// Create a frame
Frame frame = new Frame("Calculator");
frame.setSize(400, 600);
frame.setLayout(new BorderLayout());
// Create the display
display = new TextField();
display.setEditable(false);
frame.add(display, BorderLayout.NORTH);
// Create the button panel
Panel panel = new Panel();
panel.setLayout(new GridLayout(4, 4));
// Button labels in the specified order
String[] buttons = {
"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
"0", "C", "=", "/"
};
// Add buttons to the panel
for (String label : buttons) {
Button button = new Button(label);
button.addActionListener(this);
panel.add(button);
}
frame.add(panel, BorderLayout.CENTER);
// Close the frame on exit
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
// Make the frame visible
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {
// If button is a number
display.setText(display.getText() + command);
} else if (command.equals("C")) {
// Clear the display
display.setText("");
operator = "";
} else if (command.equals("=")) {
// Perform the calculation
num2 = Double.parseDouble(display.getText());
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
display.setText("Error"); // Handle division by zero
return;
}
break;
}
display.setText(String.valueOf(result));
operator = "";
} else {
// If an operator button is clicked
if (!operator.isEmpty()) {
return; // Do nothing if an operator is already set
}
operator = command;
num1 = Double.parseDouble(display.getText());
display.setText("");
}
}
public static void main(String[] args) {
new Calculator();
}
}
OUTPUT :
Implementation Details
User Interface Design
• Frame: The main window of the calculator is created using the Frame
class.
• Display: A non-editable TextField is used to show the current input and
results.
• Buttons: A panel is created using the Panel class with a GridLayout to
organize buttons for digits, operations, and functions like clear and
equals.
Code Breakdown
1. Class Declaration and Initialization
public class Calculator extends Frame implements ActionListener {
// Declare UI components and variables
private TextField display;
private String operator = "";
private double num1, num2, result;
public Calculator() {
// Frame and components initialization code...
}
}
• The Calculator class extends Frame and implements ActionListener to handle button
clicks.
2. Setting Up the GUI
• The constructor initializes the frame, display, and buttons:
frame.setSize(400, 600);
frame.setLayout(new BorderLayout());
// Display and buttons setup...
3. Action Handling
The actionPerformed method processes button clicks:
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// Handle number input, operations, and calculations...
}
• Input from number buttons appends to the display.
• The C button clears the display, while the = button performs the calculation based on
the selected operator.
Example Functionality
• When a number is clicked, it appears in the display.
• Pressing an operator prepares the application for the next number input.
• The = button calculates the result based on the input numbers and selected operator.
Challenges Faced
• Ensuring proper input handling to prevent errors, such as division by zero.
• Managing the state of calculations to support continuous operations.
Future Enhancements
• Advanced Operations: Include functions like square root, percentage, and memory
operations.
• Improved UI: Transition to Swing for a more modern interface and better styling
options.
• Keyboard Support: Allow users to input numbers and operations using the keyboard
for improved usability.
Conclusion
This project successfully demonstrates the creation of a basic calculator application in Java.
Through this exercise, key programming concepts such as event handling, GUI design, and
state management were applied. The calculator serves as a foundation for further
enhancements and more complex applications.
References
• Java Documentation: Oracle Java SE Documentation
• Tutorials on AWT and GUI Development in Java.