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

Experiment 7

Uploaded by

tanvisingh1dec
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)
26 views3 pages

Experiment 7

Uploaded by

tanvisingh1dec
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

Experiment – 7

Objective: Create a Calculator using APPLET.

APPLET:
Introduction: Java Applets were small, self-contained programs designed to run in a web browser as
part of a web page. They were primarily used to provide interactive features that could not be easily
accomplished with standard HTML and JavaScript. However, applets have mostly been deprecated and
are no longer supported by most web browsers due to security concerns and the advent of more modern
web technologies.

Overview of Java Applets:


Java Applets were written in Java and compiled into bytecode, which was then executed by the Java
Virtual Machine (JVM) embedded within the user's browser. This allowed for platform-independent
execution, one of the significant advantages of Java.
Key Features of Applets:
1. Platform Independent: Applets could run on any device with a JVM, making them widely
usable across different systems.
2. Secure: Applets ran inside a sandbox environment, restricting them from accessing certain
resources on the local computer (e.g., files and system resources) unless explicitly allowed.
3. Integration: They could interact seamlessly with web technologies and could be embedded in
HTML pages.
Explanation:
Creating a simple calculator using Java applets involves writing Java code that runs within a web
browser. However, it's important to note that many modern browsers no longer support Java applets
directly due to security concerns. For those still wanting to learn or for legacy applications.

Here, creating the Java applet code and the corresponding HTML file to embed the applet.

Step 1: Write the Java Code


write the Java applet code. Below is a simple calculator applet that can perform addition, subtraction,
multiplication, and division.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class SimpleCalculator extends Applet implements ActionListener {


TextField num1, num2, result;
Button add, sub, mul, div;

public void init() {


// Create text fields
num1 = new TextField(5);
num2 = new TextField(5);
result = new TextField(10);

// Create buttons
add = new Button("+");
sub = new Button("-");
mul = new Button("*");
div = new Button("/");

// Add components to the applet


add(num1);
add(num2);
add(add);
add(sub);
add(mul);
add(div);
add(result);

// Add action listeners for the buttons


add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


double n1 = Double.parseDouble(num1.getText());
double n2 = Double.parseDouble(num2.getText());
double res = 0.0;

if (e.getSource() == add) {
res = n1 + n2;
} else if (e.getSource() == sub) {
res = n1 - n2;
} else if (e.getSource() == mul) {
res = n1 * n2;
} else if (e.getSource() == div) {
res = n1 / n2;
}

result.setText(String.valueOf(res));
}
}
Step 2: Compile the Java Code
compile this Java code using a JDK. Save the code in a file named SimpleCalculator.java and use the
command line to compile:
javac SimpleCalculator.java
Step 3: Create the HTML File
applet is compiled, an HTML file to embed and run it in a browser that supports Java applets.
<html>
<head>
<title>Simple Calculator Applet</title>
</head>
<body>
<applet code="SimpleCalculator.class" width="300" height="300">
Your browser does not support Java Applets.
</applet>
</body>
</html>
Step 4: Run the Applet
use the command line tool appletviewer that comes with JDK:
appletviewer file.html

You might also like