[go: up one dir, main page]

0% found this document useful (0 votes)
37 views7 pages

PRACTICAL Calculator

This document contains a Java implementation of a simple calculator using Swing for the graphical user interface. It includes functionality for basic arithmetic operations such as addition, subtraction, multiplication, and division, along with a clear and equals button. The calculator processes user input and displays results in a text field, handling errors such as division by zero.

Uploaded by

adityasalgude05
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)
37 views7 pages

PRACTICAL Calculator

This document contains a Java implementation of a simple calculator using Swing for the graphical user interface. It includes functionality for basic arithmetic operations such as addition, subtraction, multiplication, and division, along with a clear and equals button. The calculator processes user input and displays results in a text field, handling errors such as division by zero.

Uploaded by

adityasalgude05
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/ 7

PRACTICAL

CODE

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class Calculator extends JFrame implements ActionListener {

static JFrame frame;

static JTextField display;

String operand1, operator, operand2;

Calculator() {

operand1 = operator = operand2 = "";

public static void main(String[] args) {

// Create a frame

frame = new JFrame("Calculator");

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) {

System.err.println(e.getMessage());

}
Calculator calculator = new Calculator();

display = new JTextField(16);

display.setEditable(false);

JButton b0 = new JButton("0");

JButton b1 = new JButton("1");

JButton b2 = new JButton("2");

JButton b3 = new JButton("3");

JButton b4 = new JButton("4");

JButton b5 = new JButton("5");

JButton b6 = new JButton("6");

JButton b7 = new JButton("7");

JButton b8 = new JButton("8");

JButton b9 = new JButton("9");

JButton add = new JButton("+");

JButton subtract = new JButton("-");

JButton divide = new JButton("/");

JButton multiply = new JButton("*");

JButton clear = new JButton("C");

JButton decimal = new JButton(".");

JButton equals = new JButton("=");

b0.addActionListener(calculator);

b1.addActionListener(calculator);
b2.addActionListener(calculator);

b3.addActionListener(calculator);

b4.addActionListener(calculator);

b5.addActionListener(calculator);

b6.addActionListener(calculator);

b7.addActionListener(calculator);

b8.addActionListener(calculator);

b9.addActionListener(calculator);

add.addActionListener(calculator);

subtract.addActionListener(calculator);

divide.addActionListener(calculator);

multiply.addActionListener(calculator);

clear.addActionListener(calculator);

decimal.addActionListener(calculator);

equals.addActionListener(calculator);

JPanel panel = new JPanel();

panel.add(display);

panel.add(add);

panel.add(b1);

panel.add(b2);

panel.add(b3);

panel.add(subtract);

panel.add(b4);

panel.add(b5);
panel.add(b6);

panel.add(multiply);

panel.add(b7);

panel.add(b8);

panel.add(b9);

panel.add(divide);

panel.add(decimal);

panel.add(b0);

panel.add(clear);

panel.add(equals);

panel.setLayout(new GridLayout(5, 4, 5, 5));

panel.setBackground(Color.LIGHT_GRAY);

frame.add(panel);

frame.setSize(300, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

@Override

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if ((command.charAt(0) >= '0' && command.charAt(0) <= '9') || command.equals(".")) {

if (!operator.isEmpty()) {
operand2 += command;

} else {

operand1 += command;

display.setText(operand1 + operator + operand2);

else if (command.equals("C")) {

operand1 = operator = operand2 = "";

display.setText("");

else if (command.equals("=")) {

if (!operand1.isEmpty() && !operator.isEmpty() && !operand2.isEmpty()) {

double result = 0.0;

try {

double num1 = Double.parseDouble(operand1);

double num2 = Double.parseDouble(operand2);

switch (operator) {

case "+":

result = num1 + num2;

break;

case "-":

result = num1 - num2;

break;

case "*":
result = num1 * num2;

break;

case "/":

if (num2 == 0) {

display.setText("Error: Divide by 0");

operand1 = operator = operand2 = "";

return;

result = num1 / num2;

break;

display.setText(operand1 + operator + operand2 + "=" + result);

operand1 = Double.toString(result);

operator = operand2 = "";

} catch (NumberFormatException ex) {

display.setText("Error");

else {

if (!operand1.isEmpty() && operand2.isEmpty()) {

operator = command;

display.setText(operand1 + operator);

}
}

}
OUTPUT

You might also like