0% found this document useful (0 votes)
9 views2 pages

Java Lab Report

The document presents a Java program that creates an interactive calculator applet using AWT. Users can input two numbers and perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The program handles invalid inputs and division by zero with appropriate error messages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Java Lab Report

The document presents a Java program that creates an interactive calculator applet using AWT. Users can input two numbers and perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The program handles invalid inputs and division by zero with appropriate error messages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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");

You might also like