0% found this document useful (0 votes)
20 views3 pages

A

abc

Uploaded by

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

A

abc

Uploaded by

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

import java.awt.

*;
import java.awt.event.*;

public class CalculatorAWTDisplayInput extends Frame implements ActionListener {


// Components
private TextField textField;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button addButton, subButton, mulButton, divButton, eqButton, clrButton;

// Variables to store the current result and the last operator


private double result = 0;
private String lastOperator = "";
private boolean isOperatorClicked = false;

// Constructor
CalculatorAWTDisplayInput() {
// Frame setup
setTitle("Calculator");
setSize(420, 550);
setLayout(null);

// Text field
textField = new TextField();
textField.setBounds(50, 50, 300, 50);
textField.setEditable(false);
add(textField);

// Number buttons
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");

// Operator buttons
addButton = new Button("+");
subButton = new Button("-");
mulButton = new Button("*");
divButton = new Button("/");
eqButton = new Button("=");
clrButton = new Button("C");

// Add action listeners


b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
eqButton.addActionListener(this);
clrButton.addActionListener(this);

// Panel for buttons


Panel panel = new Panel();
panel.setBounds(50, 150, 300, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));

// Add buttons to panel


panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(addButton);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(subButton);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(mulButton);
panel.add(new Label()); // Empty space
panel.add(b0);
panel.add(eqButton);
panel.add(divButton);

add(panel);

// Clear button
clrButton.setBounds(50, 470, 300, 50);
add(clrButton);

// Window close handling


addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});

setVisible(true);
}

// Main method
public static void main(String[] args) {
new CalculatorAWTDisplayInput();
}

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

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


if (isOperatorClicked) {
isOperatorClicked = false;
}
textField.setText(textField.getText() + command);
} else if (command.equals("C")) {
result = 0;
lastOperator = "";
textField.setText("");
} else if (command.equals("=")) {
performOperation(Double.parseDouble(getLastOperand()));
textField.setText(textField.getText() + "=" + result);
lastOperator = "";
} else {
if (!lastOperator.isEmpty()) {
performOperation(Double.parseDouble(getLastOperand()));
} else {
result = Double.parseDouble(getLastOperand());
}
lastOperator = command;
textField.setText(textField.getText() + command);
isOperatorClicked = true;
}
}

// Get the last entered operand


private String getLastOperand() {
String currentText = textField.getText();
int lastOperatorIndex = Math.max(
Math.max(currentText.lastIndexOf("+"),
currentText.lastIndexOf("-")),
Math.max(currentText.lastIndexOf("*"),
currentText.lastIndexOf("/"))
);
return currentText.substring(lastOperatorIndex + 1);
}

// Perform the operation


private void performOperation(double operand) {
switch (lastOperator) {
case "+":
result += operand;
break;
case "-":
result -= operand;
break;
case "*":
result *= operand;
break;
case "/":
if (operand != 0) {
result /= operand;
} else {
textField.setText("Error: Divide by 0");
}
break;
}
}
}

You might also like