[Go to site: main page, start]

0% found this document useful (0 votes)
5 views38 pages

Java

This document is a practical file for a Java programming course at Tika Ram P.G. Girls College for the session 2025-2026. It includes a detailed index of practical exercises covering various Java concepts such as JDK installation, primitive and wrapper classes, arithmetic operations, array manipulations, string comparisons, object-oriented programming principles, inheritance, method overriding, and GUI components. Each exercise is accompanied by code examples and expected outputs.

Uploaded by

sakshiantil72
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views38 pages

Java

This document is a practical file for a Java programming course at Tika Ram P.G. Girls College for the session 2025-2026. It includes a detailed index of practical exercises covering various Java concepts such as JDK installation, primitive and wrapper classes, arithmetic operations, array manipulations, string comparisons, object-oriented programming principles, inheritance, method overriding, and GUI components. Each exercise is accompanied by code examples and expected outputs.

Uploaded by

sakshiantil72
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TIKA RAM P.G.

GIRLS COLLEGE
SONIPAT
SESSION : 2025-2026

PRACTICAL FILE ON
JAVA PROGRAMMING
SUBMITTED BY:
CLASS: [Link]. 2nd YEAR
ROLL NO. :
SUBMITTED TO: MS ANU
( A.P. )
INDEX
[Link] Particulars Teacher's
. Sign

1. Install and Configure JDK ,then compile and run a basic Java
program.

2. Demonstrate the use of primitive and Wrapper class in Java

3. Implement arithmetic operations using user defined


functions.

4. Develop an array program to Perform matrix addition ,


subtraction and Multiplication.

5. Write a program to demonstrate String comparision and


Mutable Immutable Strings.

6. Create a constructor based java program showcaseing


Constructor Chaining

7. Implement method overloading with different parameter set.

8. Develop an application showcaseing the aggregation and


composition in java classes in simple and easy language

9. Demonstrate inheritance by creating a subclass from a


baseclass

10. Implement and explain the difference between method


overriding and hiding

11. Write a program implementing abstract classes and


interface together.

12. Create a Java program with multiple thread executing


concurrently.

13. Write a program using GUI component with different layout


manager
14. Implement the Producer Consumer problem using
Thread Synchronization.

15. Create a simple library Management System using OOP.


1.​ Install and Configure JDK ,then compile and run a basic Java program.
public class Displaying {
public static void main(String args[])
{
[Link]("screen Display");
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](" ");
[Link](i);
}
[Link]("\n");
}
[Link]("Screen Display Done");
}
}
Output:
2 . Demonstrate the use of primitive and Wrapper class in Java
public class PrimitiveWrapperExample {

public static void main(String[] args)

int a = 10;

[Link]("Primitive int : " + a);

Integer b = [Link](20);

[Link]("Wrapper Integer : " + b);

Integer c = a;

[Link]("Auto-boxed Integer : " +c);

int d = b;

[Link]("Auto-unboxed int : " + d);

String binary = [Link](a);

[Link]("Binary of a: " + binary);

Output:
[Link] arithmetic operations using user defined functions.
public class FloatPoint {

public static void main(String args[])

float a = 20.5F, b = 6.4F;

[Link](" a = " + a);

[Link](" b =" + b);

[Link](" a+b = " + (a+b));

[Link](" a-b = " + (a-b));

[Link](" a*b = " + (a*b));

[Link](" a/b = " + (a/b));

[Link](" a%b = " + (a%b));

Output:
4. Develop an array program to Perform matrix addition , subtraction and
Multiplication
public class MatrixOperation

public static void main(String[] args) {

int[][] A = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

int[][] B = {

{9, 8, 7},

{6, 5, 4},

{3, 2, 1}

};

int[][] sum = addMatrices(A, B);

int[][] diff = subtractMatrices(A, B);

int[][] prod = multiplyMatrices(A, B);

[Link]("Matrix A:");

printMatrix(A);

[Link]("\nMatrix B:");

printMatrix(B);

[Link]("\nMatrix A + B (Addition):");

printMatrix(sum);

[Link]("\nMatrix A - B (Subtraction):");

printMatrix(diff);

[Link]("\nMatrix A x B (Multiplication):");
printMatrix(prod);

public static int[][] addMatrices(int[][] A, int[][] B) {

int rows = [Link];

int cols = A[0].length;

int[][] result = new int[rows][cols];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[i][j] = A[i][j] + B[i][j];

return result;

public static int[][] subtractMatrices(int[][] A, int[][] B) {

int rows = [Link];

int cols = A[0].length;

int[][] result = new int[rows][cols];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[i][j] = A[i][j] - B[i][j];

return result;

public static int[][] multiplyMatrices(int[][] A, int[][] B) {

int rows = [Link];


int cols = B[0].length;

int common = [Link];

int[][] result = new int[rows][cols];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

for (int k = 0; k < common; k++) {

result[i][j] += A[i][k] * B[k][j];

return result;

public static void printMatrix(int[][] matrix) {

for (int[] row : matrix) {

for (int value : row) {

[Link](value + " ");

[Link]();

Output
Matrix A:

123

456

789
Matrix B:

987

654

321

Matrix A + B (Addition):

10 10 10

10 10 10

10 10 10

Matrix A - B (Subtraction):

-8 -6 -4

-2 0 2

468

Matrix A x B (Multiplication):

30 24 18

84 69 54

138 114 90
5..Write a program to demonstrate String comparision and Mutable
Immutable Strings
public class StringDemo

public static void main(String[] args)

[Link]("=== Immutable String Example ===");

String str1 = "Hello";

String str2 = "Hello";

String str3 = new String ("Hello");

[Link]("str1 == str2:"+ (str1==str2));

[Link]("str1==str3:"+(str1==str3));

[Link]("[Link](str3);"+[Link](str3));

[Link]("Original str1:"+str1);

[Link]("World");

[Link]("After concat(no assignment):"+str1);

str1=[Link]("World");

[Link]("After concat(with assignment):"+str1);

[Link]("\n===MutableStringExample===");

StringBuilder sb= new StringBuilder("Hello");

[Link]("Original StringBuilder:"+sb);

[Link]("World");

[Link]("After append:"+sb);

[Link]("After reverse:"+sb);

}
Output:
6. Create a constructor based java program showcaseing Constructor Chaining
public class Student {

String name;

int age;

String course;

Student(){

this("Unknown");

[Link]("Constructor with 1 parameter called");

Student(String name){

this("No Name" , 0 , "Not Assigned");

[Link]("Constructor with 2 parameters called ");

Student(String name, int age, String course){

[Link] = name;

[Link] = age;

[Link] = course;

[Link]("Constructor with 3 parameter called");

void display(){

[Link]("Name:"+ name);

[Link]("Age:"+ age);

[Link]("Course:"+ course);
}

public class ConstructorChainingExample

public static void main(String[] args){

Student s1 = new Student();

[Link]();

Output:
[Link] method overloading with different parameter set.
public class

Calculator {

public int add(int a, int b) {

return a + b;

public int add(int a, int b, int c){

return a + b + c;

public double add(double a, double b){

return a + b;

public double add(int a, double b){

return a + b;

public static void main(String[]args){

Calculator calc = new Calculator();

[Link]([Link](5, 10));

[Link]([Link](5, 10, 15));

[Link]([Link](5.5, 4.5));

[Link]([Link](10, 3.5));

} }
[Link] an application showcasing the aggregation and composition in java
classes in simple and easy language
class Engine {

void start() {

[Link]("Engine starts...");

class Car {

private Engine engine; // Composition: Car owns Engine

public Car() {

engine = new Engine(); // Engine created inside Car

void startCar() {

[Link]();

[Link]("Car is running!");

class Teacher {

private String name;

public Teacher(String name) {

[Link] = name;

void teach() {

[Link](name + " is teaching.");

}
}

class School {

private String schoolName;

private Teacher teacher;

public School(String schoolName, Teacher teacher) {

[Link] = schoolName;

[Link] = teacher;

void showInfo() {

[Link]("School: " + schoolName);

[Link]();

public class RelationshipDemo {

public static void main(String[] args) {

[Link]("=== Composition Example ===");

Car car = new Car();

[Link]();

[Link]("\n=== Aggregation Example ===");

Teacher teacher = new Teacher("Mr. Smith");

School school = new School("Green Valley High", teacher);

[Link]();

[Link]("\nSchool closed...");

[Link](); // Teacher still can teach

}
}

Output:
=== Composition Example ===

Engine starts...

Car is running!

=== Aggregation Example ===

School: Green Valley High

Mr. Smith is teaching.

School closed...

Mr. Smith is reaching


9. Demonstrate inheritance by creating a subclass from a baseclass
class Animal {

void eat(){

[Link]("This animal eats food. ");

class Dog extends Animal {

void bark(){

[Link]("The dog barks. ");

public class Main {

public static void main(String[] args){

Dog myDog = new Dog();

[Link]();

[Link]();

}
10. Implement and explain the difference between method overriding and
hiding
public class MethodOverrideHideDemo {

public static void main(String[] args)

Parent p1= new Parent();

Parent p2= new Child();

Child c1= new Child();

[Link]("==Instance Method(Overriding) ===");

[Link]();

[Link]();

[Link]();

[Link]("\n=== Static Method (Hiding) ===");

[Link]();

[Link]();

[Link]();

}
[Link] a program implementing abstract classes and interface together.
interface Vehicle {

void start();

void stop();

abstract class Car implements Vehicle{

abstract void fuelType();

public void wheels(){

[Link]("All caes have 4 wheels");

public class AbstractInterfaceDemo {

public static void main(String[] args) {

Car c1= new ElectricCar();

Car c2= new PetrolCar();

[Link]("=== Electric Car Details ===");

[Link]();

[Link]();

[Link]();

[Link]();

[Link]("\n=== Petrol Car Details ===");

[Link]();

[Link]();

[Link]();
[Link]();

}public class ElectricCar extends Car{

@Override

public void start(){

[Link]("Electric car starts silently.");

@Override

public void stop(){

[Link]("electtric car stop silently.");

public void fueltype(){

[Link]("fueltype electricity");

@Override

void fuelType() {

throw new UnsupportedOperationException("Not supported yet."); //To change body of


generated methods, choose Tools | Templates.

}public class PetrolCar extends Car {

@Override

public void start(){

[Link]("Petrol car starts with a rumble.");

@Override
public void stop(){

[Link]("petrol car stops.");

@Override

public void fuelType(){

[Link]("Fuel type :Petrol");

}
12. Create a Java program with multiple thread executing concurrently.
class A extends Thread

public void run()

for (int i = 1; i<=5; i++)

if(i==1) yield();

[Link]("\tFrom Thread A : i = " +i);

[Link]("exit from A ");

class B extends Thread

public void run( )

for(int i=1; i<=5; i++)

[Link]("\tFrom Thread B ");

if(i==3) stop( );

[Link]("Exit from B ");

class C extends Thread


{

public void run( )

for( int k=1; k<=5; k++)

[Link]("\t From Thread C : k = " +k);

if(k==1)

try

sleep(1000);

catch (Exception e)

[Link]("Exit from C ");

public class ThreadMethod {

public static void main(String args[ ])

A threadA = new A( );

B threadB = new B( );

C threadC = new C( );

System. [Link]("Start thread A");

[Link]( );

[Link]("Start thread B");


[Link]( );

[Link]("Start thread C");

[Link]( );

[Link]("End of main thread");

}
[Link] a program using GUI component with different layout manager

import [Link].*;

import [Link].*;

public class LayoutDemo extends JFrame {

public LayoutDemo() {

setTitle("Layout Manager Demo");

setSize(600, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null); // Center window

JTabbedPane tabs = new JTabbedPane();

[Link]("FlowLayout", createFlowLayoutPanel());

[Link]("BorderLayout", createBorderLayoutPanel());

[Link]("GridLayout", createGridLayoutPanel());

[Link]("BoxLayout", createBoxLayoutPanel());

add(tabs);

setVisible(true);

private JPanel createFlowLayoutPanel() {

JPanel panel = new JPanel(new FlowLayout());

[Link](new JButton("Button 1"));

[Link](new JButton("Button 2"));

[Link](new JButton("Button 3"));


[Link](new JButton("Button 4"));

[Link](new JButton("Button 5"));

return panel;

private JPanel createBorderLayoutPanel() {

JPanel panel = new JPanel(new BorderLayout(5, 5));

[Link](new JButton("North"), [Link]);

[Link](new JButton("South"), [Link]);

[Link](new JButton("East"), [Link]);

[Link](new JButton("West"), [Link]);

[Link](new JButton("Center"), [Link]);

return panel;

private JPanel createGridLayoutPanel() {

JPanel panel = new JPanel(new GridLayout(2, 3, 5, 5));

for (int i = 1; i <= 6; i++) {

[Link](new JButton("Button " + i));

return panel;

private JPanel createBoxLayoutPanel() {

JPanel panel = new JPanel();

[Link](new BoxLayout(panel, BoxLayout.Y_AXIS)); // vertical

[Link](new JButton("Button 1"));

[Link]([Link](10)); // space between buttons

[Link](new JButton("Button 2"));


[Link]([Link](10));

[Link](new JButton("Button 3"));

return panel;

public static void main(String[] args) {

[Link](() -> new LayoutDemo());

}
14..Implement the Producer Consumer problem using Thread
Synchronization
public class Box {

private int item;

private boolean available = false ;

synchronized void put (int value ){

while(available){

try{

wait();

catch (InterruptedException e){

[Link]();

item = value;

available = true;

[Link]("Producer produced: " +item);

notify();

synchronized int gets(){

while (!available){

try{

wait();

catch (InterruptedException e){

[Link]();
}

[Link]("Consumer consumed :" +item);

available = false ;

notify();

return item;

class Producer extends Thread {

Box box;

Producer (Box b){

box = b;

public void run (){

int i;

for( i= 1; i<=5; i++);

[Link](i);

try{

[Link](500);

catch (InterruptedException e){}

class Consumer extends Thread{

Box box;

Consumer (Box b){


box = b;

public void run(){

for(int i = 1; i<=5; i++){

[Link]();

try{

[Link](800);

catch (InterruptedException e){}

}. } }

public class ProducerConsumerExample {

public static void main(String[] args){

Box box = new Box();

Producer producer = new Producer(box);

Consumer consumer = new Consumer(box);

[Link]();

[Link]();

Output:
15.. Create a simple library Management System using OOP.
import [Link];

import [Link];

public class Book {

private String title;

private String author;

private boolean isAvailable;

public Book(String title, String author){

[Link] = title;

[Link] = author;

[Link] = true;

Book(int id, String title, String author) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of


generated methods, choose Tools | Templates.

public String getTitle(){

return title;

public String getAuthor(){

return author;

public boolean isAvailable(){

return isAvailable;

public void borrowBook(){


if (isAvailable){

isAvailable = false;

[Link]("You have borrowed: " +title);

}else{

[Link]("This book was not borrowed.");

public void displayBook(){

[Link]("Title:" +title +" [Author:" +author +"]Available :" +(isAvailable ?


"Yes" : "No"));

public class Library {

private ArrayList<Book> books = new ArrayList<>();

private ArrayList<Book> newArrayList;

public void addbook(Book book){

[Link](book);

[Link]("Book added successfully ");

public void showAllBooks(){

if([Link]()){

[Link]("No books in the library ");

} else{

for(Book b : books){

[Link]();

}
}

public void borrowBook(String title){

for(Book b : books){

if

([Link]().equalsIgnoreCase(title)){

[Link]();

return;

[Link]("Book not found");

void borrowBook(int borrowId) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of


generated methods, choose Tools | Templates.

void returnBook(int returnId) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of


generated methods, choose Tools | Templates.

void addBook(Book book) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of


generated methods, choose Tools | Templates.

public class LibraryManagementSystem {

public static void main(String[] args){


Scanner sc = new Scanner([Link]);

Library library = new Library();

while (true){

[Link]("\n=== Library Management System ===");

[Link]("1. Add Book ");

[Link]("2. Shoe All Books ");

[Link]("3. Borrow Book ");

[Link]("4. return ");

[Link]("5. Exit your choice: ");

int choice = [Link]();

switch (choice){

case 1:

[Link]("Enter Book Id: ");

int id = [Link]();

[Link]();

[Link]("Enter Book Title: ");

String title = [Link]();

[Link]("Enter Author Name: ");

String author = [Link]();

[Link](new Book(id,title, author));

break;

case 2:

[Link]();

break;

case 3:

[Link]("Enter Book ID to borrow: ");


int borrowId =[Link]();

[Link](borrowId);

break;

case 4:

[Link]("Enter Book ID to return : ");

int returnId = [Link]();

[Link](returnId);

break;

case 5:

[Link] ("Existing...Goodbye!");

[Link]();

return;

default:

[Link]("Invalid choice Try again.");

Output:

You might also like