Q.
Java program using Applets that displays a simple interactive graphical
interface. This program allows users to enter two numbers and performs
basic arithmetic operations on them.
import [Link].*;
import [Link].*;
import [Link];
public class CalculatorApplet extends Applet implements ActionListener {
TextField num1, num2, result;
Button add, subtract, multiply, divide;
public void init() {
setLayout(new FlowLayout());
Label label1 = new Label("Enter first number:");
add(label1);
num1 = new TextField(10);
add(num1);
Label label2 = new Label("Enter second number:");
add(label2);
num2 = new TextField(10);
add(num2);
add = new Button("+");
subtract = new Button("-");
multiply = new Button("*");
divide = new Button("/");
add(add);
add(subtract);
add(multiply);
add(divide);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
Label label3 = new Label("Result:");
add(label3);
result = new TextField(10);
[Link](false);
add(result);
public void actionPerformed(ActionEvent e) {
try {
double n1 = [Link]([Link]());
double n2 = [Link]([Link]());
double res = 0;
if ([Link]() == add) {
res = n1 + n2;
} else if ([Link]() == subtract) {
res = n1 - n2;
} else if ([Link]() == multiply) {
res = n1 * n2;
} else if ([Link]() == divide) {
if (n2 != 0) {
res = n1 / n2;
} else {
[Link]("Error: Division by zero");
return;
[Link]([Link](res));
} catch (NumberFormatException ex) {
[Link]("Invalid input");