Yangzhou University
Department of Software Engineering
Class: JAVA Programming
Design Topic: Windows Attachment Calculator
Submitted By
Sayed Mizanur Rahman(英雄)
Student ID: 238801235
Windows attachment calculator and implement
1. Requirements Analysis: Analyze the requirements for the calculator program:
The program should perform basic arithmetic operations: addition, subtraction,
multiplication, and division.
The user interface should be easy to use and intuitive.
The application should handle exceptions (e.g., division by zero).
2. Summary Design: Describe the overall design concept and methods of implementation:
Design Concept: Create a simple GUI-based calculator using Java Swing.
Implementation Methods: Use Java Swing for the GUI, and implement the
arithmetic operations using Java methods.
Main Modules:
o GUI Module: For user interaction.
o Arithmetic Module: For performing calculations.
o Exception Handling Module: For managing errors like division by zero.
Relationships Between Modules:
o The GUI Module interacts with the Arithmetic Module to perform
calculations based on user input.
o The Exception Handling Module ensures the application runs smoothly
without crashing.
3. Detailed Design: Detailed description of each module and their functionalities:
GUI Module:
Main Components: JFrame, JTextField, JButton
Layout: GridLayout for arranging buttons and text field
Arithmetic Module:
Operations: Addition, Subtraction, Multiplication, Division
Methods:
o add(double a, double b)
o subtract(double a, double b)
o multiply(double a, double b)
o divide(double a, double b)
Exception Handling Module:
Exceptions: ArithmeticException for division by zero
Handling Mechanism: Display error message in the text field
4. Debugging Analysis:
Debugging Tools: Java Debugger (jdb), print statements
Common Issues:
o Incorrect display of results
o Handling of edge cases like division by zero
Solutions:
o Implemented proper exception handling
o Thorough testing with different inputs
5. User Manual:
Starting the Application: Double-click the JAR file or run the Calculator class.
Using the Calculator:
o Enter numbers by clicking the number buttons.
o Use operation buttons (+, -, *, /) to perform calculations.
o Click '=' to see the result.
o Click 'C' to clear the display.
Exiting the Application: Close the window.
6. Test Results:
Test Cases:
o Addition, Subtraction, Multiplication, Division with positive and negative
numbers
o Division by zero
o Clearing the display
Results: All test cases passed successfully.
7. Appendices:
Source Code:
o Include the full source code with comments explaining each part.
Complete Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// Main class for the Calculator application
public class Calculator extends JFrame implements ActionListener {
// Declare components
private final JTextField display; // Display field for
calculator output
private final JButton[] numberButtons; // Array of number buttons (0-
9)
private final JButton addButton, subButton, mulButton, divButton,
eqButton, clrButton; // Operation buttons
private double num1, num2, result; // Variables to store numbers
and result
private char operator; // Variable to store the
operator
// Constructor to initialize the calculator components and layout
public Calculator() {
// Initialize display field
display = new JTextField();
display.setEditable(false); // Make display field non-editable
// Initialize number buttons and add action listeners
numberButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
}
// Initialize operation buttons and add action listeners
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
eqButton = new JButton("=");
clrButton = new JButton("C");
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
eqButton.addActionListener(this);
clrButton.addActionListener(this);
// Set layout for the calculator
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5));
// Add buttons to the panel
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(numberButtons[0]);
panel.add(clrButton);
panel.add(eqButton);
panel.add(divButton);
// Add components to the frame
add(display, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
// Set frame properties
setTitle("Calculator");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// Action performed method to handle button click events
@Override
public void actionPerformed(ActionEvent e) {
// Handle number button clicks
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
display.setText(display.getText() + i);
}
}
// Handle operation button clicks
if (e.getSource() == addButton) {
num1 = Double.parseDouble(display.getText());
operator = '+';
display.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(display.getText());
operator = '-';
display.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(display.getText());
operator = '*';
display.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(display.getText());
operator = '/';
display.setText("");
}
if (e.getSource() == eqButton) {
num2 = Double.parseDouble(display.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
try {
result = num1 / num2;
} catch (ArithmeticException ex) {
display.setText("Error");
return;
}
break;
}
display.setText(String.valueOf(result));
}
if (e.getSource() == clrButton) {
display.setText("");
}
}
// Main method to run the calculator application
public static void main(String[] args) {
new Calculator();
}
}
Windows attachment calculator using Java:
8. Design Experience
This project provided valuable experience in GUI development using Java
Swing. While the current implementation meets basic requirements, future
improvements could include more advanced mathematical functions and
better error handling.