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

A

abc

Uploaded by

antimachauhan888
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)
33 views3 pages

A

abc

Uploaded by

antimachauhan888
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.*;

class cals extends Frame implements ActionListener {


// TextField for display
TextField display;

// Stores operands and operator


String num1 = "", num2 = "", operator = "";

// Constructor to set up the UI


cals() {
// Set up the frame
setTitle("AWT Calculator");
setSize(300, 400);
setLayout(new BorderLayout());

// Display field
display = new TextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 30));

// Wrap the text field in a panel to make it span the full length
Panel displayPanel = new Panel();
displayPanel.setLayout(new BorderLayout());
displayPanel.add(display, BorderLayout.CENTER);

// Add the display panel to the frame


add(displayPanel, BorderLayout.NORTH);

// Panel for buttons


Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));

// Add buttons to the panel


String[] buttons = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"C", "0", "=", "/"
};

for (String label : buttons) {


Button button = new Button(label);
button.setFont(new Font("Arial", Font.BOLD, 16));
button.addActionListener(this);
buttonPanel.add(button);
}

// Add button panel to the frame


add(buttonPanel, BorderLayout.CENTER);

// Add window closing event


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

// Handle button clicks


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

// If a number or dot is clicked


if (command.charAt(0) >= '0' && command.charAt(0) <= '9' ||
command.equals(".")) {
// Append to the current operand
if (operator.isEmpty()) {
num1 += command;
display.setText(num1);
} else {
num2 += command;
display.setText(num1 + " " + operator + " " + num2);
}
}
// If an operator is clicked
else if (command.equals("+") || command.equals("-") || command.equals("*")
|| command.equals("/")) {
if (!num1.isEmpty()) {
operator = command;
display.setText(num1 + " " + operator);
}
}
// If equals button is clicked
else if (command.equals("=")) {
if (!num1.isEmpty() && !num2.isEmpty() && !operator.isEmpty()) {
double result = 0;
double operand1 = Double.parseDouble(num1);
double operand2 = Double.parseDouble(num2);

// Perform operation
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
if (operand2 != 0) {
result = operand1 / operand2;
} else {
display.setText("Error: Divide by 0");
return;
}
break;
}

// Display result and reset state


display.setText(String.valueOf(result));
num1 = String.valueOf(result);
num2 = operator = "";
}
}
// If clear button is clicked
else if (command.equals("C")) {
num1 = num2 = operator = "";
display.setText("");
}
}

// Main method to run the calculator


public static void main(String[] args) {
new cals();
}
}

You might also like