Java Kirat 36exp
Java Kirat 36exp
INTRODUCTION TO JAVA
Introduction: Foundation and Philosophy
Mankirat Singh
Date: - 02/04/2026 2
the risk of memory leaks and segmentation faults common in languages like
C++.
Security: Designed for networked environments, Java provides a secure
execution environment. It runs applications within a "sandbox" (the JVM),
restricting access to the host system unless explicitly permitted, thus preventing
malicious code execution.
Multithreading: Java has built-in support for concurrent programming, allowing
developers to execute multiple threads (parts of a program) simultaneously. This
enhances performance in highly responsive applications and server-side
processing.
JDK (Java Development Kit): The JDK is the complete software development
package required to create Java applications. It includes the Java Runtime
Environment (JRE) along with essential tools for developers, such as the javac
compiler, the Javadoc tool for documentation, and the jdb debugger.
JRE (Java Runtime Environment): The JRE is a subset of the JDK designed
for end-users who only need to run Java applications, not develop them. It
provides the necessary libraries and the JVM to execute compiled bytecode.
JVM (Java Virtual Machine): The JVM is the heart of the Java ecosystem. It is
an abstract computing machine that provides the runtime environment in which
Java bytecode is executed. It is platform-dependent, meaning there are different
JVMs for Windows, Linux, and macOS.
Platform Independence Workflow: When Java code is compiled, it is converted
into bytecode, which is not machine code but an intermediate language. The
JVM on the target machine interprets or compiles this bytecode into machine-
specific instructions on the fly, enabling the "Write Once, Run Anywhere"
functionality.
Just-In-Time (JIT) Compiler: Inside the JVM, a JIT compiler enhances
performance by translating frequently executed bytecode segments directly into
native machine code, significantly speeding execution times compared to pure
interpretation.
Mankirat Singh
Date: - 02/04/2026 3
Source Code Writing: The process begins with the developer writing human-
readable code in a .java file. This code adheres to Java syntax, defining
classes, methods, and logic.
Compilation Phase (javac): The Java compiler, included in the JDK, translates
the high-level .java source code into intermediate bytecode, storing it in
a .class file.
Bytecode Platform Independence: Unlike machine code, bytecode is not
specific to any processor or operating system. This intermediate format allows
Java to be portable across different computing platforms.
Loading and Verification: When the program is run, the JVM's Class Loader
brings the bytecode into memory. The Bytecode Verifier then ensures the code is
valid and does not violate security restrictions, preventing unauthorized access to
the underlying system.
Execution Phase (Interpreter & JIT Compiler): The JVM executes the
bytecode. Initially, an interpreter may translate bytecode instructions to machine
code one by one. For improved performance, the Just-In-Time (JIT) compiler
identifies "hot spots" (frequently used code) and compiles them directly into
native machine code for faster execution.
Mankirat Singh
Date: - 02/04/2026 4
Mankirat Singh
Date: - 02/04/2026 5
Mankirat Singh
Date: - 02/04/2026 6
PROGRAM 1
import [Link].*;
class hello {
[Link]("Hello World");
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 7
PROGRAM 2
Write a program to find square root of a number
CODE:-
import [Link];
import [Link];
if (number < 0) {
} else {
}}}
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 8
PROGRAM 3
Write a program to find the largest of three numbers using if else loop.
CODE: -
import [Link];
[Link]("Number 1: ");
[Link]("Number 2: ");
[Link]("Number 3: ");
double largest;
largest = num1;
largest = num2;
} else {
largest = num3;
Mankirat Singh
Date: - 02/04/2026 9
[Link]();
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 10
PROGRAM 4
Write a program to find factorial of a number using non recurssion.
CODE: -
import [Link];
if (number < 0) {
} else {
long factorial = 1;
factorial *= i; }
[Link](); }}
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 11
PROGRAM 5
Write a program to find fibonacci series upto a number using non
recurssion.
CODE: -
import [Link];
int n = [Link]();
long a = 0, b = 1;
int count = 0;
long c = a + b;
a = b;
b = c;
count++;
[Link]();
Mankirat Singh
Date: - 02/04/2026 12
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 13
PROGRAM 6
Write a program to find factorial of a number using recurssion.
CODE: -
import [Link];
[Link](result); }
if (n <= 1) {
return 1; }
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 14
PROGRAM 7
Write a program to find fibonacci series upto a number using recurssion.
CODE: -
import [Link];
if (n <= 1) {
return n; }
OUTPUT: -
PROGRAM 8
Mankirat Singh
Date: - 02/04/2026 15
CODE: -
import [Link];
if (isPrime(i)) {
if (n % i == 0) {
return false; } }
return true; } }
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 16
PROGRAM 9
Write a program to find whether the given number is Armstrong or not.
CODE: -
import [Link];
int n = [Link]();
int temp = n;
int count = 0;
count++;
temp = n;
int sum = 0;
if (sum == n)
Mankirat Singh
Date: - 02/04/2026 17
[Link]("Armstrong Number");
else
[Link]();
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 18
PROGRAM 10
Write a program to print sum of matrices.
CODE: -
import [Link];
matrix1[i][j] = [Link]();
matrix2[i][j] = [Link]();
Mankirat Singh
Date: - 02/04/2026 19
[Link]();
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 20
PROGRAM 11
Write a program to find the area and perimeter of rectangle using methods.
CODE: -
import [Link];
return l * b; }
return 2 * (l + b); } }
OUTPUT: -
Mankirat Singh
Date: - 02/04/2026 21
PROGRAM 12
Write a program to perform linear search on array.
CODE: -
import [Link];
class LinearSearch {
int arr[];
int n = [Link]();
arr[j] = [Link]();
if (arr[i] == target) {
found = true;
break;
Mankirat Singh
Date: - 02/04/2026 22
if (!found) {
[Link]();
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 23
PROGRAM 13
Write a program to demonstrate Default and Parameterized Constructor.
CODE: -
import [Link];
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
Student(String n, int a) {
name = n;
age = a;
void display() {
Mankirat Singh
Date: - 02/04/2026 24
[Link]();
[Link]();
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 25
PROGRAM 14
Write a program to find Area of Rectangle and Circle using Polymorphism.
CODE: -
import [Link];
class Shape {
void area() {} }
Rectangle(double l, double b) {
length = l;
breadth = b; }
@Override
void area() {
double radius;
Circle(double r) {
radius = r; }
@Override
void area() {
Mankirat Singh
Date: - 02/04/2026 26
[Link]();
[Link](); } }
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 27
PROGRAM 15
(a)
Write a program to perform various string operations (Lowercase,
Uppercase, Concatenation, Length of String, Replace of String).
CODE: -
import [Link];
Mankirat Singh
Date: - 02/04/2026 28
OUTPUT:-
(b)
Write a program to perform various StringBuffer operations.
CODE: -
import [Link];
[Link](" World");
[Link](6, 16);
Mankirat Singh
Date: - 02/04/2026 29
[Link]();
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 30
PROGRAM 16
Write a program to demonstrate Super Keyword.
CODE: -
class Vehicle {
void drive() {
[Link]("Driving a vehicle"); } }
@Override
void drive() {
[Link]();
[Link]("Driving a car"); } }
[Link](); } }
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 31
PROGRAM 17
Write a program to demonstrate this Keyword.
CODE: -
class Student {
int rollno;
String name;
[Link] = rollno;
[Link] = name; }
void display() {
[Link](); } }
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 32
PROGRAM 18
Write a program to demonstrate Final Keyword.
CODE: -
[Link]();
print(50); }
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 33
PROGRAM 19
Write a program to demonstrate abstract class and abstract method.
CODE: -
void color() {
@Override
void draw() {
[Link]("Drawing a circle"); } }
[Link]();
[Link](); } }
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 34
PROGRAM 20
Write a program to calculate area of rectangle and circle using interface.
CODE: -
interface Shape {
void calculateArea();
[Link] = length;
[Link] = breadth;
@Override
int radius;
Circle(int radius) {
[Link] = radius;
@Override
Mankirat Singh
Date: - 02/04/2026 35
[Link]();
[Link]();
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 36
PROGRAM 21
Write a program to demonstrate exception handling.
CODE: -
import [Link].*;
int a = [Link]();
int b = [Link]();
int result = a / b;
catch (ArithmeticException e) {
finally {
[Link]("Execution completed");
[Link](); } } }
OUTPUT:-
Mankirat Singh
Date: - 02/04/2026 37
PROGRAM 22
Write a program to create and display a simple applet.
CODE: -
HTML -
<html>
<body>
</body>
</html>
Java -
import [Link];
import [Link];
Mankirat Singh
Date: - 02/04/2026 38
PROGRAM 23
To implement producer-consumer problem using concept of threads
CODE: -
import [Link];
try {
[Link]();
} catch (InterruptedException e) {
[Link]();
});
try {
[Link]();
} catch (InterruptedException e) {
[Link]();
Mankirat Singh
Date: - 02/04/2026 39
});
[Link]();
[Link]();
[Link]();
[Link]();
int capacity = 2;
int value = 0;
while (true) {
synchronized (this) {
wait();
[Link](value++);
notify();
[Link](1000);
Mankirat Singh
Date: - 02/04/2026 40
while (true) {
synchronized (this) {
while ([Link]() == 0)
wait();
notify();
[Link](1000);
Mankirat Singh
Date: - 02/04/2026 41
PROGRAM 24
To create 3 threads by extending the Thread class. First thread displays
“Good Morning” every 1 sec, Second displays “Hello” every 2 sec, Third
displays “Welcome” every 3 sec
CODE: -
import [Link].*;
try {
[Link](1000);
[Link]("Good Morning");
[Link](2000);
[Link]("Hello Mankira");
[Link](3000);
[Link]("Welcome");
} catch (Exception e) {
[Link](e);
Mankirat Singh
Date: - 02/04/2026 42
[Link]();
Mankirat Singh
Date: - 02/04/2026 43
PROGRAM 25
To create three threads by implementing Runnable. First thread displays
“Good Morning” every 1 sec, Second displays “Hello” every 2 sec, Third
displays “Welcome” every 3 sec.
CODE: -
Thread t;
Frst() {
t = new Thread(this);
[Link]("Good Morning");
[Link]();
try {
[Link](1000);
} catch (Exception e) {
[Link](e);
Mankirat Singh
Date: - 02/04/2026 44
Thread t;
sec() {
t = new Thread(this);
[Link]("Hello");
[Link]();
try {
[Link](2000);
} catch (Exception e) {
[Link](e);
} } } }
Thread t;
third() {
t = new Thread(this);
[Link]("Welcome");
[Link]();
Mankirat Singh
Date: - 02/04/2026 45
try {
[Link](3000);
} catch (Exception e) {
[Link](e);
} } } }
new Frst();
new sec();
new third();
} }
Mankirat Singh
Date: - 02/04/2026 46
PROGRAM 26
WAP to develop a program using AWT components: text field, text area,
button, and label.
CODE: -
import [Link].*;
import [Link].*;
[Link](null);
[Link](true);
[Link](600, 600);
Mankirat Singh
Date: - 02/04/2026 47
[Link](l1); [Link](t1);
[Link](l2); [Link](t2);
[Link](l3); [Link](ta);
[Link](l4); [Link](t3);
[Link](btn);
Mankirat Singh
Date: - 02/04/2026 48
PROGRAM 27
WAP to make simple calculator and make use of grid layout and text field
to display the result.
CODE: -
import [Link].*;
import [Link].*;
import [Link].*;
TextField t;
int p = 0, q = 0;
String oper;
t = new TextField(20);
[Link]([Link]);
[Link]("0");
int k = 0;
Mankirat Singh
Date: - 02/04/2026 49
add(b[k]);
b[k].addActionListener(this);
k++;
add(b1[i]);
b1[i].addActionListener(this);
add(t);
p = [Link]([Link]());
oper = str;
[Link]("");
else if ([Link]("=")) {
q = [Link]([Link]());
if ([Link]("+"))
[Link]([Link](p + q));
Mankirat Singh
Date: - 02/04/2026 50
else if ([Link]("-"))
[Link]([Link](p - q));
else if ([Link]("*"))
[Link]([Link](p * q));
else if ([Link]("%"))
[Link]([Link](p % q));
else if ([Link]("C")) {
p = q = 0;
[Link]("");
else {
[Link]([Link]() + str);
Mankirat Singh
Date: - 02/04/2026 51
PROGRAM 28
Write a program to demonstrate mouse listener using grid layout.
CODE: -
import [Link].*;
import [Link].*;
import [Link].*;
public MouseListenerDemo() {
setSize(300, 200);
add(panel);
[Link](button);
[Link](this);
Mankirat Singh
Date: - 02/04/2026 52
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
Mankirat Singh
Date: - 02/04/2026 53
PROGRAM 29
WAP to write an application to simulate calculator using grid bag layout.
CODE: -
import [Link].*;
import [Link].*;
[Link](JFrame.EXIT_ON_CLOSE);
[Link](layout);
[Link](false);
[Link] = 0;
[Link] = 0;
[Link] = 4;
[Link] = [Link];
[Link](textField, c);
String[][] buttons = {
Mankirat Singh
Date: - 02/04/2026 54
};
[Link] = j;
[Link] = i + 1;
[Link] = 1;
[Link](btn, c);
[Link]();
[Link](true);
Mankirat Singh
Date: - 02/04/2026 55
PROGRAM 30
WAP to develop a program to create a menu with various menu and sub-
menu items.
CODE: -
import [Link].*;
import [Link].*;
[Link](menu);
[Link](item1);
[Link](item2);
[Link](item3);
[Link](submenu);
[Link](sub1);
[Link](sub2);
Mankirat Singh
Date: - 02/04/2026 56
[Link](menuBar);
[Link](300, 300);
[Link](this);
[Link](true);
Mankirat Singh
Date: - 02/04/2026 57
PROGRAM 31
WAP to develop a program to handle keyboard events.
CODE: -
import [Link].*;
import [Link].*;
public KeyboardEventExample() {
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addKeyListener(this);
new KeyboardEventExample();
Mankirat Singh
Date: - 02/04/2026 58
Mankirat Singh
Date: - 02/04/2026 59
PROGRAM 32
To develop a program demonstrating the use of URL connection.
CODE: -
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("GET");
new InputStreamReader([Link]()));
String line;
[Link](line);
[Link]();
Mankirat Singh
Date: - 02/04/2026 60
Mankirat Singh
Date: - 02/04/2026 61
PROGRAM 33
WAP to develop a program using JDBC demonstrating the use of prepared
statement.
CODE: -
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
super();
[Link] = studentroll;
[Link] = studentname;
[Link] = studentclass;
Mankirat Singh
Date: - 02/04/2026 62
public Student() {
super();
super();
[Link] = studentname;
[Link] = studentclass;
return studentroll;
[Link] = studentroll;
return studentname;
[Link] = studentname;
return studentclass;
Mankirat Singh
Date: - 02/04/2026 63
[Link] = studentclass;
@Override
try {
[Link]("[Link]");
} catch (Exception e) {
[Link]();
Mankirat Singh
Date: - 02/04/2026 64
return con;
boolean f = false;
try {
[Link](1, [Link]());
[Link](2, [Link]());
[Link]();
f = true;
} catch (Exception e) {
[Link]();
return f;
boolean f = false;
try {
Mankirat Singh
Date: - 02/04/2026 65
[Link](1, userId);
[Link]();
f = true;
} catch (Exception e) {
[Link]();
return f;
try {
while ([Link]()) {
int cl = [Link]("sclass");
Mankirat Singh
Date: - 02/04/2026 66
[Link]("++++++++++++++++++++++++++++++++++++++++++++");
} catch (Exception e) {
[Link]();
[Link]("Welcome to my program");
while (true) {
int c = [Link]([Link]());
if (c == 1) {
Mankirat Singh
Date: - 02/04/2026 67
if (ans) {
} else {
[Link](st);
} else if (c == 2) {
boolean f = [Link](userId);
if (f) {
} else {
} else if (c == 3) {
[Link]();
} else if (c == 4) {
break;
} else {
[Link]("Invalid choice");
Mankirat Singh
Date: - 02/04/2026 68
} }
} }
Mankirat Singh
Date: - 02/04/2026 69
PROGRAM 34
WAP to demonstrate Stack(using array)
CODE: -
capacity = size;
if (top == capacity - 1) {
return; }
stack[++top] = value;
if (top == -1) {
return -1;
return stack[top--]; }
Mankirat Singh
Date: - 02/04/2026 70
if (top == -1) {
[Link]("Stack is empty");
return; }
[Link]();
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
Mankirat Singh
Date: - 02/04/2026 71
PROGRAM 35
WAP to demonstrate Queue(using array)
CODE: -
class Queue {
void enqueue(int x) {
if (rear == [Link] - 1) {
[Link]("Queue Overflow");
} else {
arr[++rear] = x;
void dequeue() {
[Link]("Queue Underflow");
} else {
void display() {
[Link]("Queue is empty");
} else {
Mankirat Singh
Date: - 02/04/2026 72
[Link]();
[Link](40);
[Link](50);
[Link](60);
[Link]();
[Link]();
[Link]();
Mankirat Singh
Date: - 02/04/2026 73
PROGRAM 36
WAP to demonstrate Dynamic Polymorphism
CODE: -
class Animal {
void sound() {
void sound() {
[Link]("Dog barks"); }}
void sound() {
[Link]("Cat meows"); }}
Animal a;
[Link]();
a = new Cat();
[Link](); }
Mankirat Singh
Date: - 02/04/2026 74
Mankirat Singh