Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Lesp Session1 Practical

The document contains Java code for a simple calculator application using AWT. It features a graphical user interface with buttons for digits and basic operations, and handles user input through action listeners. The calculator performs addition, subtraction, multiplication, and division, and includes functionality to clear the display.
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)
5 views3 pages

Lesp Session1 Practical

The document contains Java code for a simple calculator application using AWT. It features a graphical user interface with buttons for digits and basic operations, and handles user input through action listeners. The calculator performs addition, subtraction, multiplication, and division, and includes functionality to clear the display.
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/ 3

LESP SESSION 1st Practical : Calculator

import java.awt.*;
import java.awt.event.*;

public class SimpleCalculator extends Frame implements ActionListener {

TextField display;
Button[] numberButtons;
Button addButton, subButton, mulButton, divButton, eqButton, clrButton;
double num1, num2, result;
String operator = "";

public SimpleCalculator() {

setLayout(new BorderLayout());

setBackground(Color.LIGHT_GRAY);

display = new TextField(16);


display.setEditable(false);
display.setBackground(Color.WHITE);
add(display, BorderLayout.NORTH);

Panel buttonPanel = new Panel();


buttonPanel.setLayout(new GridLayout(4, 4, 10, 10)); // 4 rows and 4 columns with
some space between

numberButtons = new Button[10];


for (int i = 0; i < 10; i++) {
numberButtons[i] = new Button(String.valueOf(i));
numberButtons[i].setBackground(Color.CYAN);
numberButtons[i].addActionListener(this);
buttonPanel.add(numberButtons[i]); // Add number buttons to the panel
}

addButton = new Button("+");


subButton = new Button("-");
mulButton = new Button("*");
divButton = new Button("/");
eqButton = new Button("=");
clrButton = new Button("C");
LESP SESSION 1st Practical : Calculator

addButton.setBackground(Color.YELLOW);
subButton.setBackground(Color.YELLOW);
mulButton.setBackground(Color.YELLOW);
divButton.setBackground(Color.YELLOW);
eqButton.setBackground(Color.GREEN);
clrButton.setBackground(Color.RED);

buttonPanel.add(addButton);
buttonPanel.add(subButton);
buttonPanel.add(mulButton);
buttonPanel.add(divButton);
buttonPanel.add(eqButton);
buttonPanel.add(clrButton);

addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
eqButton.addActionListener(this);
clrButton.addActionListener(this);

add(buttonPanel, BorderLayout.CENTER);

setSize(250, 350);
setVisible(true);
setTitle("Calculator");

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e) {


String command = e.getActionCommand();

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


display.setText(display.getText() + command);
}
LESP SESSION 1st Practical : Calculator

else if (command.equals("+") || command.equals("-") || command.equals("*") ||


command.equals("/")) {
if (!display.getText().isEmpty()) {
num1 = Double.parseDouble(display.getText());
operator = command;
display.setText("");
}
}

else if (command.equals("=")) {
if (!display.getText().isEmpty()) {
num2 = Double.parseDouble(display.getText());

if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
if (num2 != 0) {
result = num1 / num2;
} else {
display.setText("Error");
return;
}
}

display.setText(String.valueOf(result));
operator = "";
}
}

else if (command.equals("C")) {
display.setText("");
operator = "";
num1 = num2 = result = 0;
}
}

public static void main(String[] args) {


SimpleCalculator Sc = new SimpleCalculator();
}
}

You might also like