PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR
DEPARTMENT OF ARTIFICIAL INTELLIGENCE
AND MACHINE LEARNING
Even Semester 2024-25
B. Tech.- Second Year
Semester- IV
Lab File
OBJECT ORIENTED PROGRAMMIG USING JAVA
LAB
(BCS452)
Submitted To : Submitted By :
Faculty Name : Name :
Designation : Roll No. :
Section :
1
Table of Contents
Vision and Mission Statements of the Institute
Vision and Mission Statements of the Department
PEOs, POs, PSOs of the Department
Course Objective and Outcomes
List of Experiments
Index
2
Institute Vision Statement
To achieve excellence in professional education and create an ecosystem for the
holistic development of all stakeholders.
Institute Mission Statements
To provide an environment of effective learning and innovation transforming students
into dynamic, responsible, and productive professionals in their respective fields, who
are capable of adapting to the changing needs of the industry and society.
3
Department Vision Statement
To be a recognized Department of Computer Science & Engineering that produces
versatile computer engineers, capable of adapting to the changing needs of computer
and related industry.
Department Mission Statements
The mission of the Department of Computer Science and Engineering is:
i. To provide broad based quality education with knowledge and attitude to succeed in
Computer Science & Engineering careers.
ii. To prepare students for emerging trends in computer and related industry.
iii. To develop competence in students by providing them skills and aptitude to foster
culture of continuous and lifelong learning.
iv. To develop practicing engineers who investigate research, design, and find
workable solutions to complex engineering problems with awareness & concern for
society as well as environment.
Program Educational Objectives (PEOs)
i. The graduates will be efficient leading professionals with knowledge of computer
science & engineering discipline that enables them to pursue higher education and/or
successful careers in various domains.
ii. Graduates will possess capability of designing successful innovative solutions to
real life problems that are technically sound, economically viable and socially
acceptable.
iii. Graduates will be competent team leaders, effective communicators and capable of
working in multidisciplinary teams following ethical values.
iv. The graduates will be capable of adapting to new technologies/tools and constantly
upgrading their knowledge and skills with an attitude for lifelong learning
4
Department Program Outcomes (POs)
The students of Computer Science and Engineering Department will be able:
1. Engineering knowledge: Apply the knowledge of mathematics, science, Computer Science &
Engineering fundamentals, and an engineering specialization to the solution of complex
engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and Computer Science & Engineering sciences.
3. Design/development of solutions: Design solutions for complex Computer Science &
Engineering problems and design system components or processes that meet the specified needs
with appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
4. Investigation: Use research-based knowledge and research methods including design of
experiments, analysis and interpretation of data, and synthesis of the information to provide valid
conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex Computer Science &
Engineering activities with an understanding of the limitations.
6. The Engineering and Society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional engineering practice in the field of Computer Science and Engineering.
7. Environment and sustainability: Understand the impact of the professional Computer Science
& Engineering solutions in societal and environmental contexts, and demonstrate the knowledge
of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the Computer Science & Engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex Computer Science & Engineering
activities with the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make effective presentations,
and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
Computer Science & Engineering and management principles and apply these to one’s own work,
as a member and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
5
Department Program Specific Outcomes (PSOs)
The students will be able to:
1. Use algorithms, data structures/management, software design, concepts of
programming languages and computer organization and architecture.
2. Understand the processes that support the delivery and management of
information systems within a specific application environment.
6
Lab Plan
SUBJECT NAME: Object Oriented Programming using Java
Lab SUBJECT CODE: BCS-452
Lab Schedule: As per the time table
i) Course Objective:
Students shall be able to design and implement object-oriented solutions to real life problems,
algorithmic problems and web application development using basic Java programming
constructs, Collection Framework and Java Spring Boot respectively.
ii) Course Outcomes
*Level of Bloom’s *Level of Bloom’s
Level to be met Level to be met
Taxonomy Taxonomy
L1: Remember 1 L2: Understand 2
L3: Apply 3 L4: Analyze 4
L5: Evaluate 5 L6: Create 6
CO Course Outcomes
Number
To apply [3. Apply] object-oriented approach to solve real-life problems and the
BCS-452.1 compilation and interpretation process of JRE, take argument from command
line and eclipse platform.
BCS-452.2 To analyze [4. Analyze] Java Collection Framework and new language features
for efficient application development.
BCS-452.3
To develop [6. Create] and deploy web applications using Spring Boot.
7
List of Experiments
Lab Correspo
No. Lab Experiment nding CO
Write a simple Java program using Eclipse IDE to display the message "Hello, Java
World from Eclipse!" on the console, also use all major escape sequences including \n
1 (newline), \t (tab), \\ (backslash), \" (double quote), \' (single quote), and \r (carriage CO1
return). The program should demonstrate the basic structure of a Java application
including the main() method and System.out.println() function for output.
Write a Java program that accepts two integer numbers as command-line arguments,
2 calculates their sum, and displays the result. Ensure that the program properly parses CO1
the command-line arguments and handles basic integer operations.
Write a Java program to create two classes: Student and Rectangle.
The student class should have two data members: name and rollNo, with
a method to display the student's details.
3 The Rectangle class should have two data members: length and width, with CO2
a method to calculate and display the area of the rectangle.
Create objects for both classes, assign values to the data members, and invoke their
respective methods to display the output.
Write a Java program to demonstrate the concepts of inheritance and polymorphism:
Create two classes, Animal and Dog, where Dog inherits from Animal to
demonstrate single-level inheritance.
Override a method in the Dog class to demonstrate method overriding
(runtime polymorphism).
4 Implement method overloading within the Dog class by creating multiple CO2
versions of a bark() method with different parameters to demonstrate
compile- time polymorphism.
The program should create objects of the classes and invoke the methods to show the
behavior of inheritance, method overriding, and method overloading.
Write a Java program that demonstrates exception handling using try, catch, and
5 finally blocks to handle arithmetic exceptions. Extend the program to CO2
implement
multithreading by creating and running two threads that print a message concurrently.
Write a Java program that creates a user-defined package named studentinfo,
containing a class Student with basic details. In another class, import this package
6 CO2
and display the student information, demonstrating the use of Java packages for
modular
development.
Write a Java program that uses Java I/O streams to read data from a text file and
7 write data to another text file. The program should demonstrate file reading using CO2
FileReader and writing using FileWriter, along with proper exception handling.
Create a simple Spring-based Java application using annotation-based configuration.
The program should demonstrate dependency injection and include a service class and
8 CO3
a main class to invoke a method through Spring's @Component and @Autowired
annotations.
Develop a RESTful web service using Spring Boot. Create a controller that responds
9 to HTTP GET requests and returns a simple JSON message. Use Spring Boot CO3
annotations like @RestController and @GetMapping to handle requests.
8
Build a basic frontend web application using Spring Boot and Thymeleaf. Create a
webpage that collects user input from a form and displays the submitted data back
10 CO3
to the user. Demonstrate integration of backend logic with frontend rendering using
@Controller and Model.
Date
Date of Faculty
Lab of
Lab Experiment Experim Marks Signat
No. Subm
ent ission ure
Write a simple Java program using Eclipse IDE to display
the message "Hello, Java World from Eclipse!" on the
console, also use all major escape sequences including \n
(newline), \t (tab), \\ (backslash), \" (double quote), \'
1
(single quote), and \r (carriage return). The program
should demonstrate the basic structure of a Java
application including the main() method and
System.out.println()
function for output.
Write a Java program that accepts two integer numbers as
command-line arguments, calculates their sum, and
2 displays the result. Ensure that the program properly
parses the command-line arguments and handles basic
integer operations.
Write a Java program to create two classes: Student and
Rectangle.
The student class should have two data
members: name and rollNo, with a method to
display the student's details.
3 The Rectangle class should have two data
members: length and width, with a method to
calculate and display the area of the rectangle.
Create objects for both classes, assign values to the data
members, and invoke their respective methods to
display the output.
Write a Java program to demonstrate the concepts of
inheritance and polymorphism:
Create two classes, Animal and Dog, where Dog
inherits from Animal to demonstrate single-level
inheritance.
Override a method in the Dog class to
demonstrate method overriding (runtime
4 polymorphism).
Implement method overloading within the Dog
class by creating multiple versions of a bark()
method with different parameters to demonstrate
compile-time polymorphism.
The program should create objects of the classes and
invoke the methods to show the behavior of inheritance,
method overriding, and method overloading.
9
Write a Java program that demonstrates exception
handling using try, catch, and finally blocks to handle
5 arithmetic exceptions. Extend the program to implement
multithreading by creating and running two threads that
print a message concurrently.
Write a Java program that creates a user-defined package
named studentinfo, containing a class Student with basic
6
details. In another class, import this package and display
the student information, demonstrating the use of Java
packages for modular development.
Write a Java program that uses Java I/O streams to read
data from a text file and write data to another text file.
7 The program should demonstrate file reading using
FileReader and writing using FileWriter, along with
proper exception handling.
Create a simple Spring-based Java application using
annotation-based configuration. The program should
8 demonstrate dependency injection and include a service
class and a main class to invoke a method through
Spring's
@Component and @Autowired annotations.
Develop a RESTful web service using Spring Boot.
Create a controller that responds to HTTP GET
9 requests and returns a simple JSON message. Use
Spring Boot annotations like @RestController and
@GetMapping to handle requests.
Build a basic frontend web application using Spring
Boot and Thymeleaf. Create a webpage that collects
user input from a form and displays the submitted data
10
back to the user. Demonstrate integration of backend
logic with frontend rendering using @Controller and
Model.
10
Experiments:- 1
Write a simple Java program using Eclipse IDE to display the message "Hello, Java World
from Eclipse!" on the console, also use all major escape sequences including \n (newline),
\t (tab), \\ (backslash), \" (double quote), \' (single quote), and \r (carriage return). The
program should demonstrate the basic structure of a Java application including the main()
method and System.out.println() function for output
public class arohi {
public static void main(String[] args) {
// Displaying the main message
System.out.println("Hello, Java World from Eclipse!");
// Using newline character
System.out.println("This is on line one.\nThis is on line two.");
// Using tab character System.out.println("Column1\
tColumn2\tColumn3");
// Using backslash character
System.out.println("This is a backslash:
\\");
// Using double quote
System.out.println("She said, \"Java is awesome!\"");
// Using single quote
System.out.println("It\'s a great day to code in Java.");
11
// Using carriage return
System.out.println("Start of line\rReplaced");
OUTPUT:-
Student name- Arohi Agarwal
Hello, Java World from Eclipse!
This is on line one.
This is on line two.
Column1 Column2 Column3
This is a backslash: \
She said, "Java is awesome!"
It's a great day to code in Java.
Replaced line
12
Experiments:- 2
Write a Java program that accepts two integer numbers as command-line arguments,
calculates their sum, and displays the result. Ensure that the program properly parses
the command-line arguments and handles basic integer operations.
public class SumArguments {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Please provide two integer arguments.");
return;
int num1 = Integer.parseInt(args[0]);
int num2 =
Integer.parseInt(args[1]);
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
OUTPUT :-
Student name- Arohi Agarwal
Sum is: 40
13
Experiments:- 3
Write a Java program to create two classes: Student and Rectangle. • The student class
should have two data members: name and rollNo, with a method to display the student's
details. • The Rectangle class should have two data members: length and width, with a
method to calculate and display the area of the rectangle. Create objects for both classes,
assign values to the data members, and invoke their respective methods to display the
output
class Student {
String name;
int rollNo;
void displayStudentDetails()
{ System.out.println("Student Name: " +
name); System.out.println("Roll Number: " +
rollNo);
class Rectangle {
double length;
double width;
void displayArea() {
double area = length * width;
System.out.println("Rectangle Area: " +
area);
14
public class MainProgram {
public static void main(String[] args) {
Student student = new Student();
student.name = "Arohi";
student.rollNo = 2301641530042;
student.displayStudentDetails();
Rectangle rectangle = new Rectangle();
rectangle.length = 10.5;
rectangle.width = 4.2;
rectangle.displayArea();
OUTPUT :-
Student Name: Arohi
Agarwal Roll Number:
2301641530042
Rectangle Area: 44.1
15
Experiments:- 4
Write a Java program to demonstrate the concepts of inheritance and polymorphism: •
Create two classes, Animal and Dog, where Dog inherits from Animal to demonstrate
single-level inheritance. • Override a method in the Dog class to demonstrate method
overriding (runtime polymorphism). • Implement method overloading within the Dog
class by creating multiple versions of a bark() method with different parameters to
demonstrate compile time polymorphism. The program should create objects of the
classes and invoke the methods to show the behavior of inheritance, method
overriding, and method overloading.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
class Dog extends Animal {
// Method overriding (runtime polymorphism)
@Override
void makeSound() {
System.out.println("Dog barks");
// Method overloading (compile-time
polymorphism) void bark() {
System.out.println("Dog barks normally");
void bark(String mood) {
16
System.out.println("Dog barks in a " + mood + " mood");
void bark(int times) {
System.out.println("Dog barks " + times + " times");
public class PolymorphismDemo
{ public static void main(String[] args)
// Runtime polymorphism - Animal reference, Dog object
Animal a = new Dog();
a.makeSound();
// Creating Dog object
Dog d = new Dog();
// Method overloading examples
d.bark(); // No parameter
d.bark("happy"); // String parameter
d.bark(3); // int parameter
17
OUTPUT:-
Student name- Arohi Agarwal
Dog barks
Dog barks normally
Dog barks in a happy mood
Dog barks 3 times
18
Experiments:- 5
Write a Java program that demonstrates exception handling using try, catch, and finally
blocks to handle arithmetic exceptions. Extend the program to implement
multithreading by creating and running two threads that print a message concurrently.
class ExceptionDemo
{ void divide(int a, int b)
try {
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
class MyThread extends Thread {
private String message;
MyThread(String message) {
this.message = message;
public void run() {
for (int i = 1; i <= 5; i++)
{ System.out.println(message + " - iteration " +
19
i); try {
20
Thread.sleep(500); // delay to simulate concurrency
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
public class ExceptionAndThreadDemo {
public static void main(String[] args) {
// Exception handling part
ExceptionDemo obj = new ExceptionDemo();
obj.divide(10, 0); // triggers ArithmeticException
// Multithreading part
MyThread t1 = new MyThread("Thread 1 is running");
MyThread t2 = new MyThread("Thread 2 is running");
t1.start();
t2.start();
21
OUTPUT:-
Student name- Arohi Agarwal
Arithmetic Exception caught: / by
zero
Finally block executed
Thread 1 is running - iteration
1 Thread 2 is running -
iteration 1 Thread 1 is running
- iteration 2 Thread 2 is
running - iteration 2 Thread 1
is running - iteration 3 Thread
2 is running - iteration 3
Thread 1 is running - iteration
4 Thread 2 is running -
iteration 4 Thread 1 is running
- iteration 5 Thread 2 is
running - iteration 5
22
Experiments:- 6
Write a Java program that creates a user-defined package named studentinfo, containing a
class Student with basic details. In another class, import this package and display the
student information, demonstrating the use of Java packages for modular development.
package studentinfo;
public class Student {
public String name = " Arohi Agarwal";
public long rollNo = 2301641530042L;
public String department = "Computer Science";
public void display()
{ System.out.println("Student Name: " +
name); System.out.println("Roll Number: " +
rollNo);
System.out.println("Department: " + department);
// MAIN CLASS TO USE THE PACKAGE
import studentinfo.Student;
public class MainApp {
public static void main(String[] args)
{ Student s = new Student();
s.display();
}
23
}
24
OUTPUT:-
Student Name: Arohi Agarwal
Roll Number: 2301641530042
Department: Computer Science
25
Experiments:- 7
Write a Java program that uses Java I/O streams to read data from a text file and write
data to another text file. The program should demonstrate file reading using
FileReader and writing using FileWriter, along with proper exception handling.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReadWriteExample
{ public static void main(String[] args)
FileReader reader =
null; FileWriter writer =
null;
try {
// Read from input.txt
reader = new FileReader("input.txt");
// Write to output.txt
writer = new FileWriter("output.txt");
int character;
while ((character = reader.read()) != -1)
{ writer.write(character); // write each character read
System.out.println("Data successfully copied from input.txt to output.txt");
26
} catch (IOException e) {
System.out.println("I/O Exception occurred: " + e.getMessage());
27
} finally {
try {
if (reader != null) reader.close();
if (writer != null) writer.close();
} catch (IOException e) {
System.out.println("Error closing the file: " + e.getMessage());
// CSS FILE NEEDED
Hello from Java File I/O!
This text will be copied to output.txt
// OUTPUT IN CONSOLE
Student name- Arohi Agarwal
Data successfully copied from input.txt to output.txt
OUTPUT:-
Student name- Arohi Agarwal
Hello from Java File I/O!
This text will be copied to output.txt
28
Experiments:- 8
Create a simple Spring-based Java application using annotation-based configuration. The
program should demonstrate dependency injection and include a service class and a
main class to invoke a method through Spring's @Component and @Autowired
annotations.
//CREATE THE SERVICE CLASS WITH @COMPONENT
import org.springframework.stereotype.Component;
@Component
public class GreetingService {
public void sayHello() {
System.out.println("Hello from Spring Dependency Injection!");
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class MainApp {
private final GreetingService greetingService;
@Autowired
public MainApp(GreetingService greetingService)
29
{ this.greetingService = greetingService;
30
}
public void run() {
greetingService.sayHello();
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext("your.package.name");
MainApp app = context.getBean(MainApp.class);
app.run();
context.close();
OUTPUT:-
Student name- Arohi Agarwal
Hello from Spring Dependency Injection!
31
Experiments:- 9
Develop a RESTful web service using Spring Boot. Create a controller that responds to
HTTP GET requests and returns a simple JSON message. Use Spring Boot annotations like
@RestController and @GetMapping to handle requests.
// MAIN APPLICATION CLASS
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoRestApplication
{ public static void main(String[] args)
SpringApplication.run(DemoRestApplication.class, args);
// REST CONTROL CLASS
import org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class HelloController {
@GetMapping("/hello")
public Map<String, String> sayHello() {
return Map.of("message", "Hello, this is a JSON response from Spring Boot!");
// EXPECTED JSON RESPONSE
32
"message": "Hello, this is a JSON response from Spring Boot!"
33
Experiments:- 10
Build a basic frontend web application using Spring Boot and Thymeleaf. Create a
webpage that collects user input from a form and displays the submitted data back to the
user.
Demonstrate integration of backend logic with frontend rendering using @Controller and
Model.
// DATA DEPENDENCIES IN XML
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
// MAIN APPLICATION CLASS
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThymeleafFormApp
{ public static void main(String[]
34
args) {
35
SpringApplication.run(ThymeleafFormApp.class, args);
// CONTROLLER CLASS
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController {
@GetMapping("/")
public String showForm() {
return "form"; // returns form.html
@PostMapping("/submit")
public String submitForm(@RequestParam("name") String name, Model model)
{ model.addAttribute("userName", name);
return "result"; // returns result.html
36
// FORM . HTML
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User Form</title>
</head>
<body>
<h2>Enter your name</h2>
<form action="#" th:action="@{/submit}" method="post">
<input type="text" name="name" placeholder="Your name" required />
<button type="submit">Submit</button>
</form>
</body>
</html>
// RESULT.HTML
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Result</title>
</head>
<body>
<h2>Submitted Data</h2>
<p>Hello, <span th:text="${userName}">User</span>!</p>
<a href="/" >Go back</a>
</body>
</html>
37
OUTPUT:-
Student name – Arohi Agarwal
Form page: asks for your name
Result page: displays "Hello, your name!"
38