Lab Manual
Fall Semester 2025-26
Programming in Java CSE2006
Slot: F11+F12
Submitted to: Dr. Suneet Joshi Sir
Submitted by: Pracheer Srivastava 23BCY10126
[Link] Practical Title
1 Java program to demonstrate concept of Strings and its various methods
2 Java program to demonstrate concept of Access Modifiers
3 Java program to demonstrate concept of this keyword
4 Java program to demonstrate concept of Abstract class and Methods
5 Java program to demonstrate concept of Nested, Inner class and Static
class
6 Java program to demonstrate concept of Exception handling using try,
catch, throw, throws and finally keyword
7 Java Program to demonstrate concept of Multithreading, Thread creation,
Thread Life cycle and Java Synchronization methods
8 Java program to demonstrate concept of 1-D array, 2-D array and Jagged
Arrays and its operations
9 Java program to demonstrate concept of Java Collections Framework,
Java Collection Interface, Java List Interface, Java ArrayList, Java Vector
and Java Stack
10 Java program to demonstrate concept of Byte-Oriented and Character-
Oriented Streams, Java I/O Streams and Java Reader/Writer
11 Java program to connect to a database by using a JDBC driver
12 Java program to submit queries and getting results from the database
13 Java program to perform CRUD operations using the JDBC API
14 Java program to swap two numbers using Bitwise Operator
15 Java program to check if a given number is a perfect square
16 Java program to print duplicate elements of an Array
17 Java program to multiply two Matrices
18 Java program to find maximum and minimum occurring character in a
string
19 Java program to remove a Sublist from a List
Practical 1: Java program to demonstrate concept of
Strings and its various methods
Aim:
To write a Java program demonstrating the concept of Strings and commonly used
methods.
Theory:
In Java, String is an immutable class representing a sequence of characters. Java
provides many useful methods such as:
length() – returns length of string
charAt(int) – returns character at given index
concat(String) – concatenates two strings
toUpperCase(), toLowerCase() – changes case
equals() – compares content of two strings
substring() – returns a part of the string
Program:
class StringDemo {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "World";
[Link]("Length of s1: " + [Link]());
[Link]("Character at index 1 in s1: " + [Link](1));
[Link]("Concatenation: " + [Link](" " + s2));
[Link]("Uppercase: " + [Link]());
[Link]("Lowercase: " + [Link]());
[Link]("Equals check: " + [Link]("Hello"));
[Link]("Substring of s2: " + [Link](1, 4));
}
}
Output:
Length of s1: 5
Character at index 1 in s1: e
Concatenation: Hello World
Uppercase: HELLO
Lowercase: world
Equals check: true
Substring of s2: orl
Practical 2: Java program to demonstrate concept of
Access Modifiers
Aim:
To demonstrate the concept of access modifiers in Java.
Theory:
Java provides four types of access modifiers:
public – accessible everywhere
private – accessible only within the class
protected – accessible within the package and subclasses
default (no modifier) – accessible only within the package
Program:
class AccessDemo {
public int publicVar = 10;
private int privateVar = 20;
protected int protectedVar = 30;
int defaultVar = 40;
public void showVars() {
[Link]("Public: " + publicVar);
[Link]("Private: " + privateVar);
[Link]("Protected: " + protectedVar);
[Link]("Default: " + defaultVar);
}
}
public class AccessTest {
public static void main(String[] args) {
AccessDemo obj = new AccessDemo();
[Link]();
[Link]("Public: " + [Link]);
[Link]("Protected: " + [Link]);
[Link]("Default: " + [Link]);
// [Link]("Private: " + [Link]); // Error
}
}
Output:
Public: 10
P i t 20
Private: 20
Protected: 30
Default: 40
Public: 10
Protected: 30
Default: 40
Practical 3: Java program to demonstrate concept of this
keyword
Aim:
To write a program demonstrating the usage of this keyword.
Theory:
The this keyword in Java is used to:
Refer to the current object
Invoke current class methods
Invoke current class constructor
Pass the current object as parameter
Program:
class Student {
int roll;
String name;
Student(int roll, String name) {
[Link] = roll; // differentiating local and instance
variable
[Link] = name;
}
void display() {
[Link]("Roll: " + [Link] + ", Name: " + [Link]);
}
}
public class ThisDemo {
public static void main(String[] args) {
Student s1 = new Student(101, "Aman");
Student s2 = new Student(102, "Riya");
[Link]();
[Link]();
}
}
Output:
Roll: 101, Name: Aman
Roll: 102, Name: Riya
Practical 4: Java program to demonstrate concept of
Abstract class and Methods
Aim:
To write a Java program demonstrating the concept of abstract class and abstract
methods.
Theory:
An abstract class is a class that cannot be instantiated and may contain abstract
methods.
An abstract method is declared without implementation; it must be defined in a
subclass.
Used to provide a base for other classes and achieve abstraction.
Program:
abstract class Shape {
abstract void draw(); // abstract method
void info() {
[Link]("This is a shape.");
}
}
class Circle extends Shape {
void draw() {
[Link]("Drawing a Circle.");
}
}
class Rectangle extends Shape {
void draw() {
[Link]("Drawing a Rectangle.");
}
}
public class AbstractDemo {
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();
[Link]();
1 d ()
[Link]();
[Link]();
[Link]();
}
}
Output:
This is a shape.
Drawing a Circle.
This is a shape.
Drawing a Rectangle.
Practical 5: Java program to demonstrate concept of
Nested, Inner class and Static class
Aim:
To write a Java program demonstrating Nested, Inner and Static classes.
Theory:
Nested/Inner Class: A class defined inside another class.
Member Inner Class: Has access to outer class members.
Static Nested Class: Declared static, can be accessed without creating object of
outer class.
Improves encapsulation and logical grouping.
Program:
class Outer {
int x = 10;
class Inner {
void display() {
[Link]("Inner Class, x = " + x);
}
}
static class StaticInner {
void display() {
[Link]("Static Inner Class.");
}
}
}
public class NestedClassDemo {
public static void main(String[] args) {
Outer o = new Outer();
[Link] in = [Link] Inner();
[Link]();
[Link] sin = new [Link]();
[Link]();
}
}
Output:
I Cl 10
Inner Class, x = 10
Static Inner Class.
Practical 6: Java program to demonstrate Exception
Handling (try, catch, throw, throws, finally)
Aim:
To demonstrate Java exception handling using try, catch, throw, throws and finally.
Theory:
try – block of code to monitor for exceptions
catch – handles the exception
throw – used to explicitly throw an exception
throws – declares exceptions in method signature
finally – block always executed (cleanup code)
Program:
class ExceptionDemo {
static void divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Division by zero not allowed");
}
[Link]("Result: " + (a / b));
}
public static void main(String[] args) {
try {
divide(10, 2);
divide(5, 0);
} catch (ArithmeticException e) {
[Link]("Caught Exception: " + [Link]());
} finally {
[Link]("Finally block executed.");
}
}
}
Output:
Result: 5
Caught Exception: Division by zero not allowed
Finally block executed.
Practical 7: Java Program to demonstrate Multithreading,
Thread Creation, Thread Life cycle and Synchronization
Aim:
To write a program demonstrating the concept of multithreading, thread life cycle, and
synchronization in Java.
Theory:
Thread: Smallest unit of execution.
Threads can be created by:
1. Extending Thread class
2. Implementing Runnable interface
Thread life cycle: New→ Runnable → Running →Waiting →Terminated.
Synchronization: Ensures only one thread accesses shared resource at a time.
Program:
class Table {
synchronized void printTable(int n) {
for (int i = 1; i <= 5; i++) {
[Link](n * i);
try { [Link](500); } catch (Exception e) { }
}
}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) { this.t = t; }
public void run() { [Link](5); }
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) { this.t = t; }
public void run() { [Link](100); }
}
public class ThreadDemo {
public static void main(String[] args) {
Table obj = new Table();
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
y y ( j);
[Link]();
[Link]();
}
}
Output: (Threads execute in synchronized order)
5
10
15
20
25
100
200
300
400
500
Practical 8: Java program to demonstrate 1-D, 2-D, and
Jagged Arrays
Aim:
To write a Java program demonstrating operations on 1-D, 2-D and Jagged arrays.
Theory:
1-D array: Linear collection of elements.
2-D array: Matrix form of rows and columns.
Jagged array: Array of arrays with variable column sizes.
Program:
public class ArrayDemo {
public static void main(String[] args) {
// 1-D Array
int[] arr1 = {10, 20, 30, 40};
[Link]("1-D Array:");
for (int x : arr1) [Link](x + " ");
[Link]();
// 2-D Array
int[][] arr2 = {{1, 2}, {3, 4}, {5, 6}};
[Link]("2-D Array:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr2[i].length; j++) {
[Link](arr2[i][j] + " ");
}
[Link]();
}
// Jagged Array
int[][] jagged = new int[3][];
jagged[0] = new int[]{1, 2};
jagged[1] = new int[]{3, 4, 5};
jagged[2] = new int[]{6};
[Link]("Jagged Array:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < jagged[i].length; j++) {
[Link](jagged[i][j] + " ");
}
S t t i tl ()
[Link]();
}
}
}
Output:
1-D Array:
10 20 30 40
2-D Array:
1 2
3 4
5 6
Jagged Array:
1 2
3 4 5
6
Practical 9: Java program to demonstrate Java
Collections (List, ArrayList, Vector, Stack)
Aim:
To demonstrate usage of Java Collection Framework classes.
Theory:
Collection Framework provides dynamic data structures.
List Interface: Ordered collection of elements.
ArrayList: Resizable array.
Vector: Legacy synchronized list.
Stack: Subclass of Vector, follows LIFO.
Program:
import [Link].*;
public class CollectionDemo {
public static void main(String[] args) {
// ArrayList
ArrayList<String> list = new ArrayList<>();
[Link]("A"); [Link]("B"); [Link]("C");
[Link]("ArrayList: " + list);
// Vector
Vector<Integer> v = new Vector<>();
[Link](10); [Link](20); [Link](30);
[Link]("Vector: " + v);
// Stack
Stack<String> stack = new Stack<>();
[Link]("Book1");
[Link]("Book2");
[Link]("Book3");
[Link]("Stack: " + stack);
[Link]();
[Link]("After Pop: " + stack);
}
}
Output:
ArrayList: [A, B, C]
Vector: [10, 20, 30]
St k [B k1 B k2 B k3]
Stack: [Book1, Book2, Book3]
After Pop: [Book1, Book2]
Practical 10: Java program to demonstrate Byte-Oriented
and Character-Oriented Streams (I/O Streams,
Reader/Writer)
Aim:
To write a program demonstrating byte-oriented and character-oriented streams in
Java.
Theory:
Byte Streams (InputStream, OutputStream) handle binary data (8-bit bytes).
Example: FileInputStream, FileOutputStream.
Character Streams (Reader, Writer) handle character data (16-bit Unicode). Example:
FileReader, FileWriter.
Used for reading/writing files efficiently.
Program:
import [Link].*;
public class StreamDemo {
public static void main(String[] args) {
try {
// Byte-oriented stream
FileOutputStream fout = new FileOutputStream("[Link]");
String text = "Hello Byte Stream!";
[Link]([Link]());
[Link]();
FileInputStream fin = new FileInputStream("[Link]");
int i;
[Link]("Byte Stream Output: ");
while ((i = [Link]()) != -1) {
[Link]((char) i);
}
[Link]();
[Link]();
// Character-oriented stream
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Character Stream!");
[Link]();
FileReader fr = new FileReader("[Link]");
[Link]("Character Stream Output: ");
while ((i = [Link]()) != -1) {
[Link]((char) i);
}
[Link]();
} catch (Exception e) {
[Link]("Error: " + e);
}
}
}
Output:
Byte Stream Output: Hello Byte Stream!
Character Stream Output: Hello Character Stream!
Practical 11: Java program to connect to a database using
JDBC driver
Aim:
To establish a connection between Java and a database using JDBC.
Theory:
JDBC (Java Database Connectivity) provides API to connect Java with databases.
Steps:
1. Load the driver class ([Link]).
2. Establish connection ([Link]).
3. Create Statement/PreparedStatement.
4. Execute queries.
5. Close connection.
Program:
import [Link].*;
public class JDBCConnect {
public static void main(String[] args) {
try {
// Load Driver
[Link]("[Link]");
// Establish Connection
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "password");
if (con != null) {
[Link]("Database connected successfully!");
}
[Link]();
} catch (Exception e) {
[Link]("Error: " + e);
}
}
}
Output:
Database connected successfully!
Practical 12: Java program to submit queries and get
results from the database
Aim:
To write a program to submit queries and retrieve results using JDBC.
Theory:
Statement or PreparedStatement is used to execute SQL queries.
ResultSet stores the output of SELECT queries.
Methods like next(), getString(), getInt() retrieve data row by row.
Program:
import [Link].*;
public class JDBCQueryDemo {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
[Link]("ID | Name | Age");
while ([Link]()) {
[Link]([Link]("id") + " | " +
[Link]("name") + " | " +
[Link]("age"));
}
[Link]();
} catch (Exception e) {
[Link]("Error: " + e);
}
}
}
Output: (Assume students table has data)
ID | Name | Age
1 | Aman | 20
2 | Riya | 21
3 | K l | 19
y
3 | Kunal | 19
Practical 13: Java program to perform CRUD operations
using JDBC API
Aim:
To perform Create, Read, Update, Delete operations on a database table using JDBC.
Theory:
CRUD operations are basic database operations:
Create – insert new records
Read – retrieve records
Update – modify existing records
Delete – remove records
JDBC provides Statement and PreparedStatement to execute SQL queries.
Program:
import [Link].*;
public class CRUDDemo {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement stmt = [Link]();
// Create (Insert)
[Link]("INSERT INTO Employee
VALUES(101,'Aman',5000,'Suresh')");
// Read (Select)
ResultSet rs = [Link]("SELECT * FROM Employee");
[Link]("E_ID | E_Name | E_Sal | E_Mgr");
while([Link]()){
[Link]([Link]("E_ID")+" | "+
[Link]("E_Name")+" | "+
[Link]("E_Sal")+" | "+
[Link]("E_Mgr"));
}
// Update
[Link]("UPDATE Employee SET E_Sal=6000 WHERE
E_ID=101");
// D l t
// Delete
[Link]("DELETE FROM Employee WHERE E_ID=101");
[Link]();
} catch (Exception e) { [Link]("Error: "+e); }
}
}
Output: (After Insert & Select)
E_ID | E_Name | E_Sal | E_Mgr
101 | Aman | 5000 | Suresh
Practical 14: Java program to swap two numbers using
Bitwise Operator
Aim:
To swap two numbers without using a temporary variable using bitwise XOR operator.
Theory:
XOR (^) can be used to swap numbers:
a=a^b
b=a^b
a=a^b
This avoids using extra memory.
Program:
public class BitwiseSwap {
public static void main(String[] args) {
int a = 5, b = 10;
[Link]("Before Swap: a=" + a + ", b=" + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
[Link]("After Swap: a=" + a + ", b=" + b);
}
}
Output:
Before Swap: a=5, b=10
After Swap: a=10, b=5
Practical 15: Java program to check if a given number is a
perfect square
Aim:
To write a program to check whether a number is a perfect square.
Theory:
A number n is a perfect square if √n is an integer.
Use [Link]() to calculate square root and check if it’s whole.
Program:
import [Link];
public class PerfectSquare {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
double sqrt = [Link](n);
if (sqrt == (int)sqrt) {
[Link](n + " is a perfect square.");
} else {
[Link](n + " is not a perfect square.");
}
[Link]();
}
}
Output:
Enter a number: 36
36 is a perfect square.
Practical 16: Java program to print duplicate elements of
an Array
Aim:
To find and display duplicate elements in an array.
Theory:
An array can have repeated elements.
To detect duplicates, we can compare each element with all other elements or use a
HashSet.
Program:
import [Link].*;
public class DuplicateArray {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 2, 4, 5, 1, 6};
[Link]("Duplicate Elements: ");
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
[Link](arr[i] + " ");
break;
}
}
}
}
}
Output:
Duplicate Elements: 1 2
Practical 17: Java program to multiply two Matrices
Aim:
To perform multiplication of two matrices using Java.
Theory:
Matrix multiplication: C[i][j] = sum(A[i][k] * B[k][j])
Number of columns in first matrix must equal number of rows in second matrix.
Program:
public class MatrixMultiplication {
public static void main(String[] args) {
int A[][] = {{1,2,3}, {4,5,6}};
int B[][] = {{7,8}, {9,10}, {11,12}};
int C[][] = new int[2][2];
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
for(int k=0; k<3; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
[Link]("Product of Matrices:");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
[Link](C[i][j]+" ");
}
[Link]();
}
}
}
Output:
Product of Matrices:
58 64
139 154
Practical 18: Java program to find maximum and
minimum occurring character in a string
Aim:
To find the character that occurs maximum and minimum times in a string.
Theory:
Count frequency of each character using array/map.
Track max and min counts and corresponding characters.
Program:
import [Link].*;
public class CharFrequency {
public static void main(String[] args) {
String str = "programming";
str = [Link]();
int freq[] = new int[256];
for (int i = 0; i < [Link](); i++) {
freq[[Link](i)]++;
}
char maxChar = ' ', minChar = ' ';
int max = 0, min = [Link]();
for (int i = 0; i < 256; i++) {
if (freq[i] > max) { max = freq[i]; maxChar = (char)i; }
if (freq[i] != 0 && freq[i] < min) { min = freq[i]; minChar =
(char)i; }
}
[Link]("Max occurring character: " + maxChar + " (" +
max + " times)");
[Link]("Min occurring character: " + minChar + " (" +
min + " times)");
}
}
Output:
Max occurring character: g (2 times)
Min occurring character: p (1 times)
Practical 19: Java program to remove a Sublist from a List
Aim:
To demonstrate removing a sublist of elements from a List in Java.
Theory:
List interface supports dynamic arrays in Java.
subList(fromIndex, toIndex) returns a view of part of the list.
clear() can be used to remove all elements of the sublist from the original list.
Program:
import [Link].*;
public class RemoveSublist {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(
[Link](1, 2, 3, 4, 5, 6, 7)
);
[Link]("Original List: " + list);
// Remove elements from index 2 to 4 (3rd and 4th elements)
[Link](2, 5).clear();
[Link]("List after removing sublist: " + list);
}
}
Output:
Original List: [1, 2, 3, 4, 5, 6, 7]
List after removing sublist: [1, 2, 6, 7]