KARPAGAM ACADEMY OF HIGHER EDUCATION
(Deemed to be University)
(Established Under Section 3 of UGC
Act,1956)
Accredited by NAAC with A+ Grade in the Second Cycle
Pollachi Main Road, Eachanari Post, Coimbatore - 641021, Tamilnadu, India.
Phone: 0422-2980011-14,6471113,14 | Fax: 0422-2980022-23 | Email:
info@[Link]
DEPARTMENTOF COMPUTER APPLICATION
I-MCA
OBJECT ORIENTED PROGRAMMING USING JAVA
PRACTICAL
25CAP105
SEMESTER I
(2025-2027
Batch)
Name :
CERTIFICATE
This is to certify that this is a bonafide record work
done by of I-MCA. Information Technology
during the period September / October -2025 for the OBJECT
Oriented Programming Using Java - Practical’s Examination held on
[Link]: Subject Code: 25CAP105
Staff-in-charge Head of the Department
(Internal Examiner) (External Examiner)
CONTENTS
[Link]. Date Name of the Experiment Page Staff
No. Sign
Environmental Setup and create first
1. Java Program
2. Writing basic Java programs using
variables and control flow statements
3. Solve simple programs using Array,
String and Function concept
4. Implementing Classes and Objects
5. Demonstrating Method Overloading
6. Implementing different types
ofInheritance
7 Demonstrating Method Overriding
8 Working with Abstract Classes and
Interfaces
9 Implementing exception handling
techniques
10 Creating multithreading programs
using Runnable and Thread classes
11 Implementing synchronization in
multithreading
12 Demonstrate different Collection
classes and methods
13 Implementing file handling using
FileReader and FileWriter
14 Working with Serialization and
Deserialization
15 Demonstrate Lambda Expressions
and Stream API
16 Implement New Date and Time API
classes and methods
17 Creating a basic GUI application
using Swing
18 Implementing event handling
mechanisms
19 Connecting Java programs with
MySQL using JDBC
Ex No: 1
Environmental Setup and Create First Java Program
Date:
Aim:
To set up the Java environment and write a simple program to display “Hello,
World!”.
Algorithm:
1. Start the process.
2. Install and configure JDK.
3. Open a text editor or IDE.
4. Create a class HelloWorld.
5. Define the main() method.
6. Print “Hello, World!” using [Link]().
7. Save as [Link].
8. Compile and run.
9. Stop the process.
Program:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Output:
Hello, World!
Result:
The above program has been executed successfully and output is v erified.
Ex No: 2 Basic Java Programs Using Variables and Control Flow
Date: Statements
Aim:
To write a Java program using variables and control flow statements to check
whether a number is even or odd.
Algorithm:
1. Start the program.
2. Declare an integer variable num.
3. Input the number.
4. Use if-else to check divisibility by 2.
5. Display the result.
6. Stop the process.
Program:
import [Link];
class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num % 2 == 0)
[Link](num + " is Even");
else
[Link](num + " is Odd");
}
}
Output:
Enter a number: 7
7 is Odd
Result:
The above program has been executed successfully and output is verified.
Ex No: 3
Solve Simple Programs Using Array, String, and
Date: Function Concept
Aim:
To find the largest element in an array using functions.
Algorithm:
1. Start the process.
2. Declare an array of integers.
3. Pass the array to a function findMax().
4. Traverse and compare each element.
5. Return the largest element.
6. Display the result.
7. Stop the process.
Program:
class ArrayMax {
static int findMax(int arr[]) {
int max = arr[0];
for (int i = 1; i < [Link]; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
public static void main(String[] args) {
int[] numbers = {4, 12, 7, 9, 21};
[Link]("Largest number: " + findMax(numbers));
}
}
Output:
Largest number: 21
Result:
The above program has been executed successfully and output is verified.
Ex No: 4 Implementing Classes and Objects
Date:
Aim:
To demonstrate the use of classes and objects by calculating the area of a
rectangle.
Algorithm:
1. Start the process.
2. Create a class Rectangle.
3. Declare variables length, breadth.
4. Use methods to set values and calculate area.
5. Create object and call methods.
6. Stop the process.
Program:
class Rectangle {
int length, breadth;
void setData(int l, int b) {
length = l;
breadth = b;
}
int area() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
[Link](10, 5);
[Link]("Area = " + [Link]());
}
}
Output:
Area = 50
Result:
The above program has been executed successfully and output is verified.
Ex No: 5
Demonstrating Method Overloading
Date:
Aim:
To demonstrate method overloading by calculating the area of different shapes.
Algorithm:
Start the process.
1. Define a class Shape.
2. Overload methods area() for square, rectangle, and circle.
3. Display results.
4. Stop the process.
Program:
class Shape {
void area(int side) {
[Link]("Area of square: " + (side * side));
}
void area(int l, int b) {
[Link]("Area of rectangle: " + (l * b));
}
void area(double r) {
[Link]("Area of circle: " + (3.14 * r * r));
}
public static void main(String[] args) {
Shape s = new Shape();
[Link](5);
[Link](10, 6);
[Link](3.5);
}
}
Output:
Area of square: 25
Area of rectangle: 60
Area of circle: 38.465
Result:
The above program has been executed successfully and output is verified.
Ex No: 6
Implementing Different Types of Inheritance
Date:
Aim:
To demonstrate single and multilevel inheritance in Java.
Algorithm:
1. Start the process.
2. Create a base class Animal.
3. Derive class Dog from Animal.
4. Derive class Puppy from Dog.
5. Call methods from all classes.
6. Stop the process.
Program:
class Animal {
void eat() {
[Link]("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dogs bark");
}
}
class Puppy extends Dog {
void weep() {
[Link]("Puppies weep");
}
public static void main(String[] args) {
Puppy p = new Puppy();
[Link]();
[Link]();
[Link]();
}
}
Output:
Animals eat food
Dogs bark
Puppies weep
Result:
The above program has been executed successfully and output is verified.
Ex No: 7
Demonstrating Method Overriding
Date:
Aim:
To demonstrate method overriding in Java.
Algorithm:
Start the process.
1. Create a parent class Vehicle.
2. Create a child class Car that overrides run().
3. Use runtime polymorphism to call methods.
4. Stop the process.
Program:
class Vehicle {
void run() {
[Link]("Vehicle is running");
}
}
class Car extends Vehicle {
void run() {
[Link]("Car is running safely");
}
public static void main(String[] args) {
Vehicle obj = new Car();
[Link]();
}
}
Output:
Car is running safely
Result:
Ex No: 8
Working with Abstract Classes and Interfaces
Date:
The above program has been executed successfully and output is verified.
Aim:
To demonstrate abstract classes and interfaces in Java.
Algorithm:
1. Start the process.
2. Create an abstract class Shape.
3. Derive Rectangle and Circle classes.
4. Implement abstract method area().
5. Use interface Printable to print area.
6. Stop the process.
Program:
interface Printable {
void print();
}
abstract class Shape {
abstract double area();
}
class Rectangle extends Shape implements Printable {
double l = 5, b = 3;
double area() {
return l * b;
}
public void print() {
[Link]("Area of Rectangle: " + area());
}
}
class Circle extends Shape implements Printable {
double r = 2.5;
double area() {
return 3.14 * r * r;
}
public void print() {
[Link]("Area of Circle: " + area());
}
public static void main(String[] args) {
Printable p1 = new Rectangle();
Printable p2 = new Circle();
[Link]();
[Link]();
}
}
Output:
Area of Rectangle: 15.0
Area of Circle: 19.625
Result:
Ex No: 9
Implementing Exception Handling Techniques
Date:
The above program has been executed successfully and output is verified.
Aim:
To demonstrate exception handling using try, catch, and finally.
Algorithm:
1. Start the process.
2. Take two integers.
3. Divide and handle ArithmeticException if denominator is zero.
4. Use finally block to display a message.
5. Stop the process.
Program:
class Divide {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b;
[Link]("Result: " + c);
} catch (ArithmeticException e) {
[Link]("Error: Division by zero!");
} finally {
[Link]("Program executed successfully.");
}
}
}
Output:
Error: Division by zero!
Program executed successfully.
Result:
The above program has been executed successfully and output is verified.
Ex No: 10
Creating Multithreading Programs Using Runnable and
Date: Thread Classes
Aim:
To demonstrate multithreading using Thread class and Runnable interface.
Algorithm:
1. Start the process.
2. Create two threads: one using Thread class, another using Runnable.
3. Override run() to print messages.
4. Start both threads.
5. Stop the process.
Program:
class ThreadA extends Thread {
public void run() {
for (int i = 1; i <= 3; i++)
[Link]("Thread A: " + i);
}
}
class ThreadB implements Runnable {
public void run() {
for (int i = 1; i <= 3; i++)
[Link]("Thread B: " + i);
}
public static void main(String[] args) {
ThreadA t1 = new ThreadA();
Thread t2 = new Thread(new ThreadB());
[Link]();
[Link]();
}
}
Output (order may vary):
Thread A: 1
Thread B: 1
Thread A: 2
Thread B: 2
Thread A: 3
Thread B: 3
Result:
The above program has been executed successfully and output is verified.
Ex No: 11 Implementing Synchronization in Multithreading
Date:
Aim:
To demonstrate synchronization in multithreading using the synchronized keyword.
Algorithm:
1. Start the process.
2. Create a class Table with a synchronized method printTable().
3. Create two threads that share the same object of Table.
4. Each thread calls the synchronized method.
5. Observe how synchronization prevents simultaneous access.
6. Stop the process.
Program:
class Table {
synchronized void printTable(int n) {
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);
}
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
[Link](100);
}
}
class SyncDemo {
public static void main(String[] args) {
Table obj = new Table();
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
[Link]();
[Link]();
}
}
Output:
5
10
15
20
25
100
200
300
400
500
Result:
The above program has been executed successfully and output is verified.
Ex No: 12
Demonstrate Different Collection Classes and Methods
Date:
Aim:
To demonstrate the usage of various Collection classes such as ArrayList, HashSet,
and HashMap.
Algorithm:
1. Start the process.
2. Create and display an ArrayList.
3. Use HashSet to show unique elements.
4. Use HashMap to store key-value pairs.
5. Stop the process.
Program:
import [Link].*;
class CollectionDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]("C++");
[Link]("ArrayList: " + list);
HashSet<Integer> set = new HashSet<>();
[Link](10);
[Link](20);
[Link](20); // Duplicate ignored
[Link]("HashSet: " + set);
HashMap<Integer, String> map = new HashMap<>();
[Link](1, "Apple");
[Link](2, "Banana");
[Link]("HashMap: " + map);
}
}
Output:
ArrayList: [Java, Python, C++]
HashSet: [20, 10]
HashMap: {1=Apple, 2=Banana}
Result:
The above program has been executed successfully and output is verified.
Ex No: 13
Implementing File Handling Using FileReader and
Date: FileWriter
Aim:
To read and write data to a file using FileReader and FileWriter.
Algorithm:
1. Start the process.
2. Create a text file.
3. Write some content using FileWriter.
4. Read content using FileReader.
5. Display the content.
6. Stop the process.
Program:
import [Link].*;
class FileReadWrite {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Java File Handling Example");
[Link]();
[Link]("File written successfully.");
FileReader fr = new FileReader("[Link]");
int i;
[Link]("File contents: ");
while ((i = [Link]()) != -1)
[Link]((char) i);
[Link]();
} catch (IOException e) {
[Link](e);
}
}
}
Output:
File written successfully.
File contents: Java File Handling Example
Result:
The above program has been executed successfully and output is verified.
Ex No: 14 Working with Serialization and Deserialization
Date:
Aim:
To demonstrate serialization and deserialization of an object.
Algorithm:
1. Start the process.
2. Create a class Student implementing Serializable.
3. Write object data to a file using ObjectOutputStream.
4. Read it back using ObjectInputStream.
5. Stop the process.
Program:
import [Link].*;
class Student implements Serializable {
int id;
String name;
Student(int id, String name) {
[Link] = id;
[Link] = name;
}
}
class SerializeDemo {
public static void main(String[] args) {
try {
Student s1 = new Student(101, "John");
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](s1);
[Link]();
[Link]("Object serialized.");
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("[Link]"));
Student s2 = (Student) [Link]();
[Link]();
[Link]("Deserialized Object: " + [Link] + " " + [Link]);
} catch (Exception e) {
[Link](e);
}
}
}
Output:
Object serialized.
Deserialized Object: 101 John
Result:
The above program has been executed successfully and output is verified.
Ex No: 15
Demonstrate Lambda Expressions and Stream API in Java.
Date:
Aim:
To demonstrate Lambda expressions and Stream API in Java.
Algorithm:
1. Start the process.
2. Create a list of integers.
3. Use Lambda expression to filter and display even numbers.
4. Use Stream API to find sum of elements.
5. Stop the process.
Program:
import [Link].*;
import [Link].*;
class LambdaStreamDemo {
public static void main(String[] args) {
List<Integer> numbers = [Link](2, 5, 8, 10, 13);
[Link]("Even Numbers:");
[Link]().filter(n -> n % 2 == 0).forEach([Link]::println);
int sum = [Link]().mapToInt(Integer::intValue).sum();
[Link]("Sum of elements: " + sum);
}
}
Output:
Even Numbers:
2
8
10
Sum of elements: 38
Result:
Ex No: 16
demonstrate the use of the Java 8 Date and Time API.
Date:
The above program has been executed successfully and output is verified.
Aim:
To demonstrate the use of the Java 8 Date and Time API.
Algorithm:
1. Start the process.
2. Use LocalDate, LocalTime, and LocalDateTime.
3. Display current date, time, and formatted output.
4. Stop the process.
Program:
import [Link].*;
import [Link];
class DateTimeDemo {
public static void main(String[] args) {
LocalDate date = [Link]();
LocalTime time = [Link]();
LocalDateTime dateTime = [Link]();
DateTimeFormatter format = [Link]("dd-MM-yyyy
HH:mm:ss");
[Link]("Current Date: " + date);
[Link]("Current Time: " + time);
[Link]("Formatted DateTime: " + [Link](format));
}
}
Output:
Current Date: 2025-11-01
Current Time: [Link].345
Formatted DateTime: 01-11-2025 [Link]
Result:
The above program has been executed successfully and output is verified.
Ex No: 17 Creating a Basic GUI Application Using Swing
Date:
Aim:
To create a simple GUI application using Swing components.
Algorithm:
1. Start the process.
2. Create a JFrame window.
3. Add a JLabel and JButton.
4. Display GUI on screen.
5. Stop the process.
Program:
import [Link].*;
class SimpleGUI {
public static void main(String[] args) {
JFrame f = new JFrame("My First Swing App");
JLabel label = new JLabel("Welcome to Swing!");
JButton button = new JButton("Click Me");
[Link](100, 50, 150, 30);
[Link](100, 100, 100, 30);
[Link](label);
[Link](button);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Output:
A window titled "My First Swing App" displaying a label “Welcome to Swing!”
and a button “Click Me”.
Result:
The above program has been executed successfully and output is verified.
Ex No: 18 Implementing Event Handling Mechanisms
Date:
Aim:
To demonstrate event handling using ActionListener in Swing.
Algorithm:
1. Start the process.
2. Create a JFrame with a button.
3. Implement ActionListener interface.
4. Display message when button is clicked.
5. Stop the process.
Program:
import [Link].*;
import [Link].*;
class EventDemo extends JFrame implements ActionListener {
JButton b;
JLabel l;
EventDemo() {
b = new JButton("Click Me");
l = new JLabel("Waiting for click...");
[Link](100, 100, 100, 30);
[Link](100, 150, 200, 30);
add(b);
add(l);
[Link](this);
setSize(300, 250);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
public static void main(String[] args) {
new EventDemo();
}
}
Output:
A GUI window — clicking the "Click Me" button changes label text to "Button
Clicked!"
Result:
The above program has been executed successfully and output is verified.
Ex No: 19
Connect Java with MySQL and perform a simple
Date: database query using JDBC
Aim:
To connect Java with MySQL and perform a simple database query using JDBC.
Algorithm:
1. Start the process.
2. Load the JDBC driver.
3. Establish a connection with MySQL.
4. Create a statement and execute query.
5. Display results.
6. Close connection.
7. Stop the process.
Program:
import [Link].*;
class JDBCDemo {
public static void main(String[] args) {
try {
// Load driver
[Link]("[Link]");
// Establish connection
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "password");
// Create statement
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
// Display results
while ([Link]())
[Link]([Link](1) + " " + [Link](2));
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}
Output:
101 John
102 Mary
103 David
Result:
The above program has been executed successfully and output is verified.