Department of Information Technology
DEPARTMENT OF INFORMATION
TECHNOLOGY
Practical File
B. Tech (IT) 3rd Year – 5thSemester
Subject: Java Lab
BTIT506-18
CHANDIGARH ENGINEERING COLLEGE
(LANDRAN)
Faculty: - Submitted by: -
Ms. Jaskiran Kaur Pankaj Yadav
Pankaj Yadav | 2102210 Page 1
Department of Information Technology
INDEX
Sr. Page Date Remarks
Name of the Experiment
No. No.
1
10
11
12
13
14
15
16
17
18
19
20
Pankaj Yadav | 2102210 Page 2
Department of Information Technology
Experiment No: 1
Task: Write a program in Java to show implementation of classes.
Theory: In Java, the most searching program is of employee details. An employee is an entity that
can have several attributes like id, name, and department, etc. In order to create a java employee
details program, we need to create a class for the employee entity and create properties of the
employees.
Source code:
import [Link];
public class Employee {
int empid;
String name;
float salary;
public void getInput() {
Scanner in = new Scanner([Link]);
[Link]("Enter the empid :: ");
empid = [Link]();
[Link]("Enter the name :: ");
name = [Link]();
[Link]("Enter the salary :: ");
salary = [Link]();
public void display() {
[Link]("Employee id = " + empid);
[Link]("Employee name = " + name);
[Link]("Employee salary = " + salary);
public static void main(String[] args) { Employee
e[] = new Employee[5];
Pankaj Yadav | 2102210 Page 3
Department of Information Technology
for (int i=0; i<1; i++) {
e[i] = new Employee();
e[i].getInput();
Output:
Pankaj Yadav | 2102210 Page 4
Department of Information Technology
Experiment No. 2
Task: WAP in Java to show implementation of inheritance.
Inheritance: In Java, Inheritance is an important pillar of OOP(Object-Oriented
Programming). It is the mechanism in Java by which one class is allowed to inherit the
features(fields and methods) of another class. In Java, Inheritance means creating new classes
based on existing ones. A class that inherits from another class can reuse the methods and
fields of that class. In addition, you can add new fields and methods to your current class as
well.
Java Inheritance Types
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Inheritance : In single inheritance, subclasses inherit the features of one superclass.
In the image below, class A serves as a base class for the derived class B.
Source Code:
import [Link].*;
// Parent class
class A {
public void print_Bat(){
[Link]("Bat");
}
}
class B extends A {
public void print_For() { [Link]("For"); }
}
// Driver class
public class Main {
// Main function
public static void main(String[] args){
B g = new B();
Pankaj Yadav | 2102210 Page 5
Department of Information Technology
g.print_Bat();
g.print_For();
g.print_Bat();
}}
Output:
2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base
class, and as well as the derived class also acts as the base class for other classes. In the
below image, class A serves as a base class for the derived class B, which in turn serves as a
base class for the derived class C. In Java, a class cannot directly access the grandparent’s
members.
Source Code:
import [Link].*;
// Parent class
class A {
public void print_Bat(){
[Link]("Bat");
}
}
class B extends A {
public void print_for() { [Link]("For"); }
}
class C extends B{
public void print_Rat(){ [Link]("Rat");}
}
// Driver class
public class Main {
// Main function
public static void main(String[] args){
C g = new C();
g.print_Rat();
g.print_for();
g.print_Bat();
}}
Output:
Pankaj Yadav | 2102210 Page 6
Department of Information Technology
3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base
class) for more than one subclass. In the below image, class A serves as a base class for the
derived classes B, C, and D.
Source Code:
import [Link].*;
// Parent class
class A {
public void print_Bat(){
[Link]("Bat");
}
}
class B extends A {
public void print_For() { [Link]("For"); }
}
class C extends A{
public void print_Rat(){ [Link]("Rat");}
}
class D extends A{
public void print_Hat(){ [Link]("Hat");}
}
// Driver class
public class Main {
// Main function
public static void main(String[] args){
B g = new B();
g.print_Bat();
g.print_For();
g.print_Bat();
Pankaj Yadav | 2102210 Page 7
Department of Information Technology
C h= new C();
h.print_Rat();
h.print_Bat();
h.print_Rat();
D i = new D();
i.print_Hat();
i.print_Bat();
i.print_Hat();
}}
Output:
Pankaj Yadav | 2102210 Page 8
Department of Information Technology
Experiment No. 3
Task: WAP in Java to show implementation of threads.
Thread: Thread can be referred to as a lightweight process. Thread uses fewer resources to
create and exist in the process; thread shares process resources. The main thread of Java is the
thread that is started when the program starts. The slave thread is created as a result of the
main thread. This is the last thread to complete execution.
A thread can programmatically be created by:
• Implementing the [Link] interface.
• Extending the [Link] class.
Implementation of threads:
1. By implementing Runnable interface
Source Code:
import [Link].*;
class CGC implements Runnable {
public static void main(String args[])
// create an object of Runnable target
CGC cgc = new CGC();
// pass the runnable reference to Thread
Thread t = new Thread(cgc, "cgc");
// start the thread
[Link]();
// get the name of the thread
[Link]([Link]());
Pankaj Yadav | 2102210 Page 9
Department of Information Technology
@Override public void run()
[Link]("Inside run method");
Output:
2. By using Thread Class
Source Code:
import [Link].*;
class CGC extends Thread {
public void run()
{
[Link]("Welcome to Chandigarh Group of Colleges.");
}
public static void main(String[] args)
{
CGC g = new CGC(); // creating thread
[Link](); // starting thread
}
}
Output:
Pankaj Yadav | 2102210 Page 10
Department of Information Technology
Experiment No. 4
Title: Write a program in Java to use exception handling mechanism
• Using Try Catch
• Using Throw
• Using Throws
• Using Finally
Theory: The Exception Handling in Java is one of the powerful mechanism to handle the runtime
errors so that the normal flow of the application can be maintained.
Exception Handling in Java is one of the effective means to handle runtime errors so that the
regular flow of the application can be preserved. Java Exception Handling is a mechanism to
handle runtime errors such as Class Not Found Exception, IO Exception, SQL Exception, Remote
Exception, etc.
Types of Java Exceptions: There are mainly two types of exceptions: checked and unchecked. An
error is considered as the unchecked exception. However, according to Oracle, there are three types
of exceptions namely:
1. Checked Exception
2. Unchecked Exception
3. Error
Pankaj Yadav | 2102210 Page 11
Department of Information Technology
1) Checked Exception
The classes that directly inherit the Throw able class except Runtime Exception and Error are known
as checked exceptions. For example, IO Exception, SQL Exception, etc. Checked exceptions are
checked at compile-time.
2) Unchecked Exception
The classes that inherit the Runtime Exception are known as unchecked exceptions. For example,
Arithmetic Exception, Null Pointer Exception, Array Index Out Of Bounds Exception, etc. Unchecked
exceptions are not checked at compile-time, but they are checked at runtime.
3) Error
Error is irrecoverable. Some example of errors are Out Of Memory Error, Virtual Machine Error,
Assertion Error etc.
Using Try Catch : Source
code: public class
JavaExceptionExample{ public static void
main(String args[]){ try{
//code that may raise exception int
data=100/0;
}catch(ArithmeticException e){[Link](e);}
//rest code of the program
[Link]("lisharajeshwarisingh");
[Link]("2102190");
[Link]("rest of the code");
Output:
Pankaj Yadav | 2102210 Page 12
Department of Information Technology
Using Throw: Source code:
public class TestThrow1 {
// Function to check if a person is eligible to vote or not
public static void validate(int age) {
if (age < 18) {
// Throw an exception if not eligible to vote
throw new ArithmeticException("Person is not eligible to vote");
} else {
[Link]("Person is eligible to vote!");
}
}
// Main method
public static void main(String args[]) {
try {
// Calling the function
validate(44);
[Link]("Person is eligible to vote!");
} catch (ArithmeticException e) {
[Link]("Exception: " + [Link]());
}
[Link]("2102189");
[Link]("Lakshyadeep");
}
} Output:
Pankaj Yadav | 2102210 Page 13
Department of Information Technology
Using Throws: Source code:
// Java program to demonstrate working of throws class
class ThrowsExecp {
// This method throws an exception to be handled by
caller or caller of caller and so on.
static void fun() throws IllegalAccessException {
[Link]("2102189");
[Link]("Lakshyadeep");
[Link]("Inside fun().");
throw new IllegalAccessException("demo");
}
// This is a caller function
public static void main(String args[]) {
try {
fun();
} catch (IllegalAccessException e) {
[Link]("Caught in main.");
}Output:
Pankaj Yadav | 2102210 Page 14
Department of Information Technology
Using Finally:
Source code:
c class TestFinallyBlock {
public static void main(String args[]) {
try {
// Code that does not throw any
exception
int data = 25 / 5;
[Link](data);
} catch (ArithmeticException e) { //
Changed to ArithmeticException
[Link](e);
} finally {
[Link]("2102189");
[Link]("Lakshyadeep");
[Link]("finally block is
always executed");
[Link]("rest of the
code");
}Output:
Pankaj Yadav | 2102210 Page 15
Department of Information Technology
Experiment No: 05
Title: Write a program in Java to implement multiple Inheritance by using
Interface.
Theory: Multiple Inheritance is a feature of an object-oriented concept, where a class can
inherit properties of more than one parent class. The problem occurs when methods with
the same signature exist in both the superclasses and subclass. On calling the method, the
compiler cannot determine which class method to be called and even on calling which class
method gets the priority.
Source code:
interface Walkable {
void walk();
}
interface Swimmable {
void swim();
}
// Implement the interfaces in a class
class Duck implements Walkable, Swimmable {
public void walk()
Pankaj Yadav | 2102210 Page 16
Department of Information Technology
{
[Link]("Duck is walking.");
}
public void swim()
{
[Link]("Duck is swimming.");
}
}
// Use the class to call the methods from the interfaces
class Main {
public static void main(String[] args)
{
Duck duck = new Duck();
[Link]();
[Link]();
}
}
Output:
Pankaj Yadav | 2102210 Page 17
Department of Information Technology
Experiment No: 06
Title: Write a program in Java to use exception handling mechanism
• to handle NegativeArraySizeException
• to handle NullPointerException
• to handle Arithmetic Exception
Theory:
Handle NegativeArraySizeException
Theory: NegativeArraySizeException: It is a runtime exception in Java that occurs when
an application attempts to create an array with a negative size 1. This exception is
unchecked, which means it does not need to be declared in the throws clause of a method
or constructor.
To handle this exception, you can surround the piece of code that can throw
a NegativeArraySizeException in a try-catch block and catch the exception in the catch
clause. You can then take further action as necessary for handling the exception and
ensuring that the program execution does not stop 1. Here’s an example of how to handle
it in code:
Source code:
public class NegativeArraySizeExceptionExample {
public static void main(String [] args) {
try {
int [] array = new int [ -5 ];
} catch (NegativeArraySizeException nase) {
[Link](); //handle the exception
}
[Link]("Continuing execution...");
}
}
Output:
Pankaj Yadav | 2102210 Page 18
Department of Information Technology
Handle NullPointerException
Theory: NullPointerException is a RuntimeException. In Java, a special null value can
be assigned to an object reference. NullPointerException is thrown when program attempts
to use an object reference that has the null value.
These can be:
• Invoking a method from a null object.
• Accessing or modifying a null object’s field.
• Taking the length of null, as if it were an array.
• Accessing or modifying the slots of null object, as if it were an array.
• Throwing null, as if it were a Throwable value.
• When you try to synchronize over a null object.
Source code:
public class HandleNullPointer {
public static void main(String[] args) {
try{
String tihString = null;
[Link]("length of String = "+ [Link]());
}catch(NullPointerException e){
[Link]("Performing operation on null reference");
}
[Link]("Task after exception handling");
}
Output:
Handle Arithmetic Exception
Pankaj Yadav | 2102210 Page 19
Department of Information Technology
Theory: The Exception Handling is one of the most powerful mechanisms to handle the
runtime errors so that the normal flow of the application can be maintained. In Java,
exception is an abnormal condition. Java programming language defines various exceptions.
In this section, we will discuss the one of the prominent exceptions that
is ArithmeticException in Java.
Source code:
public class ArithmeticException
void division(int a,int b)
int c=a/b;
[Link]("Division of a number is successful");
[Link]("Output of division: "+c);
public static void main(String[] args)
ArithmeticException ex=new ArithmeticException();
[Link](10,5);
Output:
Pankaj Yadav | 2102210 Page 20
Department of Information Technology
PRACTICAL NO. 7
Aim : WAP in Java to show Implementation of Packages.
Theory :
Package in JavaIn Java, a package is a way to organize related classes and
interfaces into a single unit. Packages provide a hierarchical structure for
managing and grouping classes, and they help avoid naming conflicts between
classes. Packages are used for:
• Preventing naming conflicts. For example there can be two classes with
name Employee in two packages, [Link] and
[Link]
• Making searching/locating and usage of classes, interfaces, enumerations
and annotations easier
• Providing controlled access: protected and default have package level
access control. A protected member is accessible by classes in the same
package and its subclasses. A default member (without any access
specifier) is accessible by classes in the same package only.
• Packages can be considered as data encapsulation (or data-hiding).
Built-in Packages
These packages consist of a large number of classes which are a part of Java
[Link] of the commonly used built-in packages are:
1) [Link]: Contains language support classes(e.g classed which defines
primitive data types, math operations). This package is automatically
imported.
2) [Link]: Contains classed for supporting input / output operations.
3) [Link]: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4) [Link]: Contains classes for creating Applets.
Pankaj Yadav | 2102210 Page 21
Department of Information Technology
5) [Link]: Contain classes for implementing the components for graphical
user interfaces (like button , ;menus etc).
6) [Link]: Contain classes for supporting networking operations.
User-defined packages
These are the packages that are defined by the user.
Program Code
Output
Pankaj Yadav | 2102210 Page 22
Department of Information Technology
PRACTICAL NO. 8
Aim : Write a program in Java to make use of different type of loop
statement.
Theory:
For loop:
Initialization: It is the initial condition which is executed once when the loop starts.
Here, wecan initialize the variable, or we can use an already initialized variable. It is
an optional condition.
Condition: It is the second condition which is executed each time to test the condition
of theloop. It continues execution until the condition is false. It must return boolean
value either true or false. It is an optional condition.
Increment/Decrement: It increments or decrements the variable value. It is an
optional condition.
Statement: The statement of the loop is executed each time until the second condition
is false.
Pankaj Yadav | 2102210 Page 23
Department of Information Technology
Source code:
public class PyramidExample {
public static void main(String[] args) {
for(inti=1;i<=5;i++){for(int j=1;j<=i;j++){
[Link]("*");
}
[Link]();//new line
}
}
Output:
Pankaj Yadav | 2102210 Page 24
Department of Information Technology
PRACTICAL NO. 9
Aim : Write a program in Java to show Garbage Collection.
Theory: Java garbage collection is an automatic process. Automatic garbage collection is
the process of looking at heap memory, identifying which objects are in use and which are
not, and deleting the unused objects. An in-use object, or a referenced object, means that
some part of your program still maintains a pointer to that object. An unused or unreferenced
object is no longer referenced by any part of your program. So the memory used by an
unreferenced object can be reclaimed. The programmer does not need to mark objects to be
deleted explicitly. The garbage collection implementation lives in the JVM.
Method of Garbage Collection:
• By nulling the reference
• By assigning a reference to another
• By anonymous object etc.
1) By nulling a reference:
2) By assigning a reference to another:
3)
4) By anonymous object:
5)
Source code:
public class TestGarbage1{
public void finalize(){[Link]("object is garbage collected");}
public static void main(String args[]){
Pankaj Yadav | 2102210 Page 25
Department of Information Technology
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
[Link]();
}
}
Output:
Pankaj Yadav | 2102210 Page 26
Department of Information Technology
PRACTICAL NO. 10
Aim : Write a program in Java to demonstrate the concept of method
overloading.
Theory: If a class has multiple methods having same name but different in
parameters, it is known as Method [Link] we have to perform only one
operation, having same name of the methods increases the readability of the
program.
Suppose you have to perform addition of the given numbers but there can be
any number of arguments, if you write the method such as a(int,int) for two
parameters, and b(int,int,int) for three parameters then it may be difficult for
you as well as other programmers to understand the behavior of the method
because its name differs.
Advantages of Method Overloading
• Method overloading improves the Readability and reusability of the program.
• Method overloading reduces the complexity of the program.
• Using method overloading, programmers can perform a task efficiently and effectively.
• Using method overloading, it is possible to access methods performing related functions
• with slightly different arguments and types.
• Objects of a class can also be initialized in different ways using the constructors.
Source code:
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
{
return (x + y);
Pankaj Yadav | 2102210 Page 27
Department of Information Technology
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Output:
Pankaj Yadav | 2102210 Page 28
Department of Information Technology
PRACTICAL NO. 11
Aim : Write a program in Java to implement the concept of method
overriding.
Theory: Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes or parent
classes. When a method in a subclass has the same name, the same parameters or signature,and
the same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
4) Usage of Java Method Overriding
• Method overriding is used to provide the specific implementation of a method
whichis already provided by its superclass.
• Method overriding is used for runtime polymorphism
5) Rules for Java Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
Pankaj Yadav | 2102210 Page 29
Department of Information Technology
Source code:
class
Vehicle{
void run()
{
[Link]("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
[Link]("Bike is running safely");
}
}
class main{
public static void main(String args[])
{
[Link]("2102190");
[Link]("lisha rajeshwari
singh");Bike2 obj = new Bike2();
[Link]();
}
}
Output:
Pankaj Yadav | 2102210 Page 30
Department of Information Technology
PRACTICAL NO. 12
Aim : Write a program in Java to pass object as a parameter .
Theory: Objects, like primitive types, can be passed as parameters to methods in Java.
When passing an object as a parameter to a method, a reference to the object is passed
ratherthan a copy of the object itself. This means that any modifications made to the object
withinthe method will have an impact on the original object.
Basically, a parameter cannot be changed by the function, but the function can ask the
parameter to change itself via calling some method within it.
• While creating a variable of a class type, we only create a reference to an object.
Thus, when we pass this reference to a method, the parameter that receives it will
refer to the same object as that referred to by the argument.
• This effectively means that objects act as if they are passed to methods by use of
call- by-reference.
• Changes to the object inside the method do reflect the object used as an argument.
Source code:
public class MyClass {
privateint attribute1;
private String attribute2;
private double attribute3;
// Constructor
publicMyClass(int attribute1, String attribute2, double attribute3) {
this.attribute1 = attribute1;
this.attribute2 =
attribute2;this.attribute3
= attribute3;
}
// Method with object as
parameter public void
myMethod(MyClassobj) {
[Link]("Attribute 1: " +
obj.attribute1); [Link]("Attribute 2: "
Pankaj Yadav | 2102210 Page 31
Department of Information Technology
+ obj.attribute2); [Link]("Attribute 3:
" + obj.attribute3);
}
public static void main(String[] args) {
MyClass myObject1 = new MyClass(10, "Hello", 3.14);
MyClass myObject2 = new MyClass(20, "World", 6.28);
// Call the method with object as
[Link](myObject2);
}
}
Output:
Pankaj Yadav | 2102210 Page 32
Department of Information Technology
PRACTICAL NO. 13
Aim Write a program in Java to implement the concept of Recursion.
Theory: Recursion is a process in which a function calls itself directly or indirectly is
called recursion and the corresponding function is called a recursive function. Using a
recursive algorithm, certain problems can be solved quite easily.
Base Condition in Recursion
In the recursive program, the solution to the base case is provided and the solution to the
bigger problem is expressed in terms of smaller problems.
In the above example, the base case for n < = 1 is defined and the larger value of a
numbercan be solved by converting it to a smaller one till the base case is reached.
Working of Recursion
The idea is to represent a problem in terms of one or more smaller sub-problems and add
base conditions that stop the recursion. For example, we compute factorial n if we know
thefactorial of (n-1). The base case for factorial would be n = 0. We return 1 when n = 0.
Pankaj Yadav | 2102210 Page 33
Department of Information Technology
Source code:
public class
Recursion {static int
count=0;
static void
p(){
count++;
if(count<=5
){
[Link]("hello
"+count);p();
}
}
public static void main (String[] args) {
");p();
}
}
Output:
Pankaj Yadav | 2102210 Page 34
Department of Information Technology
Experiment No: 14
Task: Write a program in Java to show the implementation of File
Handling
Theory: In Java, with the help of File Class, we can work with files. This File Class is
inside the [Link] package. The File class can be used by creating an object of the class and
then specifying the name of the file.
Why File Handling is Required?
• File Handling is an integral part of any programming language as file handling enables
us to store the output of any particular program in a file and allows us to perform
certain operations on it.
• In simple words, file handling means reading and writing data to a file.
Pankaj Yadav | 2102210 Page 35
Department of Information Technology
Source Code:
import [Link];
// Import the IOException class to handle errors
import [Link];
public class GFG {
public static void main(String[] args)
{
try {
File Obj = new File("[Link]");
if ([Link]()) {
[Link]("File created: " + [Link]());
}
else {
[Link]("File already exists.");
}
}
catch (IOException e) {
[Link]("An error has occurred.");
[Link]();
}
}}Output:
Pankaj Yadav | 2102210 Page 36
Department of Information Technology
Experiment No: 15
Task: Write a program in java to show implementation of Java
Synchronized
Method.
Theory: Java Synchronized Method If you declare any method as synchronized, it
is known as synchronized method. Synchronized method is used to lock an object
for any shared resource. When a thread invokes a synchronized method, it
automatically acquires the lock for that object and releases it when the thread
completes its task.
Source Code:
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
[Link](n*i);
try{
[Link](400);
}catch(Exception e){[Link](e);}
}
}
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
[Link](5);
}
}
Pankaj Yadav | 2102210 Page 37
Department of Information Technology
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
[Link](100);
}
} public class TestSynchronization2{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
[Link]();
[Link]();
}
}
Output:
Pankaj Yadav | 2102210 Page 38
Department of Information Technology
Experiment No: 16
Task: Write a program in java to show the implement of Applet.
Theory: What is Applet?
An applet is a Java program that can be embedded into a web page. It runs inside
the web browser and works at client side. An applet is embedded in an HTML
page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
Important points :
1. All applets are sub-classes (either directly or indirectly)
of [Link] class.
2. Applets are not stand-alone programs. Instead, they run within either a web
browser or an applet viewer. JDK provides a standard applet viewer tool
called applet viewer.
3. In general, execution of an applet does not begin at main() method.
4. Output of an applet window is not performed by [Link](). Rather it
is handled with various AWT methods, such as drawString().
Life cycle of an applet :
Creating Hello World applet :
Pankaj Yadav | 2102210 Page 39
Department of Information Technology
Running the HelloWorld Applet :
After you enter the source code for [Link], compile in the same way that you have
been compiling java programs(using javac command). However, running HelloWorld with
the java command will generate an error because it is not an application.
There are two standard ways in which you can run an applet :
1. Executing the applet within a Java-compatible web browser.
2. Using an applet viewer, such as the standard tool, applet-viewer. An applet viewer
executes your applet in a window. This is generally the fastest and easiest way to test
your applet.
Each of these methods is described next.
1. Using java enabled web browser : To execute an applet in a web browser we have to
write a short HTML text file that contains a tag that loads the applet. We can use APPLET
Pankaj Yadav | 2102210 Page 40
Department of Information Technology
or OBJECT tag for this purpose. Using APPLET, here is the HTML file that executes
HelloWorld :
2. Using appletviewer : This is the easiest way to run an applet. To execute HelloWorld
with an applet viewer, you may also execute the HTML file shown earlier. For
example, if the preceding HTML file is saved with
[Link], then the following command line will run HelloWorld :
Pankaj Yadav | 2102210 Page 41