0% found this document useful (0 votes)
15 views

Minicalculator

Uploaded by

balagomathi003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Minicalculator

Uploaded by

balagomathi003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.

No: 03
MINI CALCULATOR

Aim:
To develop a java swing-based mini calculator application that allows user to perform basic arithmetic
operations and manage input .

Algorithm:

Step-1: Create a JFrame object to serve as the window for the calculator application with “BorderLayout”
to organize components.
Step-2: Initialize numeric buttons (0-9) and operator buttons (“+”,”-” etc) and organize them using
“GridLayout”.
Step-3: Add “ActionListener ” to the buttons to handle user clicks.
Step-4: Append the numeric values to the display field.
Step-5: Capture operators and operands for calculations.
Step-6: Display the result when “=” button is clicked.
Step-7: Instantiate the class in “main” method to run the application.
Program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MiniCalculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] numButtons;
private JButton[] opButtons;
private JButton clearButton, equalsButton, backButton;
private double num1, num2;
private char operator;
Font myFont = new Font("Times new roman",Font.BOLD,25);
public MiniCalculator() {
setTitle("Calculator");
setSize(420, 520);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
display = new JTextField(30);
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 30));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setBackground(Color.BLUE);
display.setForeground(Color.WHITE);
display.setBorder(BorderFactory.createEmptyBorder(60, 10, 10, 30));
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 3, 5, 5));
buttonPanel.setBackground(Color.BLUE);
numButtons = new JButton[10];
for (int i = 9; i >= 0; i--) {
numButtons[i] = new JButton(String.valueOf(i));
numButtons[i].setFont(myFont);
numButtons[i].addActionListener(this);
buttonPanel.add(numButtons[i]);
}
opButtons = new JButton[5];
String[] opSymbols = {"+", "-", "*", "/", "%"};
for (int i = 0; i < 5; i++) {
opButtons[i] = new JButton(opSymbols[i]);
opButtons[i].setFont(myFont);
opButtons[i].addActionListener(this);
buttonPanel.add(opButtons[i]);
}
clearButton = new JButton("C");
clearButton.setFont(myFont);
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
equalsButton = new JButton("=");
equalsButton.setFont(myFont);
equalsButton.addActionListener(this);
buttonPanel.add(equalsButton);
backButton = new JButton("<=");
backButton.setFont(myFont);
backButton.addActionListener(this);
buttonPanel.add(backButton);
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (Character.isDigit(command.charAt(0))) {
display.setText(display.getText() + command);
} else if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")
|| command.equals("%")) {
num1 = Double.parseDouble(display.getText());
operator = command.charAt(0);
display.setText("");
} else if (command.equals("=")) {
num2 = Double.parseDouble(display.getText());
double result = calculateResult(num1, num2, operator);
display.setText(String.valueOf(result));
} else if (command.equals("C")) {
display.setText("");
}else if (command.equals("<=")) {
String currentText = display.getText();
if (!currentText.isEmpty()) {
display.setText(currentText.substring(0, currentText.length() - 1));
}
}
}
private double calculateResult(double num1, double num2, char operator) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 == 0) {
JOptionPane.showMessageDialog(this, "Error: Division by zero");
return 0;
}
return num1 / num2;
case '%':
return num1 % num2;
default:
return 0;
}
}
public static void main(String[] args) {
new MiniCalculator();
}
}
Output:
Result:

Thus, the java swing based mini-calculator application that allows user to perform basic arithmetic
operations was executed successfully and the output was verified.

You might also like