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

OOP IN JAVA PROJECT

Final project for GUI in OOP

Uploaded by

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

OOP IN JAVA PROJECT

Final project for GUI in OOP

Uploaded by

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

OOP IN JAVA PROJECT

Ali Mehmood Bukhari


MTN-SP-000140
BSSE-2nd
OOP in Java
Calculator
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Main Calculator class


public class Calculator {
public static void main(String[] args) {
// Create an instance of the CalculatorFrame
SwingUtilities.invokeLater(CalculatorFrame::new);
}
}

// GUI frame for the calculator


class CalculatorFrame extends JFrame implements
ActionListener {
private final JTextField inputField;
private double num1, num2, result;
private char operator;
private boolean isOperatorPressed;

public CalculatorFrame() {
// Set up the frame
setTitle("Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

// Create the input field


inputField = new JTextField();
inputField.setFont(new Font("Arial", Font.BOLD, 24));
inputField.setHorizontalAlignment(JTextField.RIGHT);
inputField.setEditable(false);
add(inputField, BorderLayout.NORTH);

// Create buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 10, 10));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.setFont(new Font("Arial", Font.BOLD, 20));
button.addActionListener(this);
buttonPanel.add(button);
}

add(buttonPanel, BorderLayout.CENTER);

// Display the frame


setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

try {
if (Character.isDigit(command.charAt(0))) {
if (isOperatorPressed) {
inputField.setText(""); // Clear for the second
operand
isOperatorPressed = false;
}
inputField.setText(inputField.getText() + command);
} else if (command.equals("C")) {
// Clear the input field and reset variables
inputField.setText("");
num1 = num2 = result = 0;
operator = '\0';
isOperatorPressed = false;
} else if (command.equals("=")) {
// Perform the calculation
if (operator != '\0' && !
inputField.getText().isEmpty()) {
num2 = Double.parseDouble(inputField.getText());
calculateResult();
inputField.setText(String.valueOf(result));
operator = '\0'; // Reset operator
}
} else {
// Save the operator and the first number
if (!inputField.getText().isEmpty()) {
num1 = Double.parseDouble(inputField.getText());
operator = command.charAt(0);
isOperatorPressed = true;
}
}
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
}

private void calculateResult() {


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 throw new ArithmeticException("Division by
zero");
break;
default:
inputField.setText("Error");
}
}
}

You might also like