0% found this document useful (0 votes)
26 views35 pages

BCS-452 (Object Oriented Programming Using Java Lab) 2023-24

The document is a lab manual for the Object Oriented Programming using Java Lab (BCS-452) at PSIT, outlining the vision and mission statements of the institute and department, program educational objectives, outcomes, evaluation scheme, syllabus, lab plan, and a list of experiments. It emphasizes the development of students' skills in designing and implementing object-oriented solutions, analyzing Java features, and deploying web applications. The manual also includes a correlation matrix for course outcomes with program outcomes and specific outcomes.

Uploaded by

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

BCS-452 (Object Oriented Programming Using Java Lab) 2023-24

The document is a lab manual for the Object Oriented Programming using Java Lab (BCS-452) at PSIT, outlining the vision and mission statements of the institute and department, program educational objectives, outcomes, evaluation scheme, syllabus, lab plan, and a list of experiments. It emphasizes the development of students' skills in designing and implementing object-oriented solutions, analyzing Java features, and deploying web applications. The manual also includes a correlation matrix for course outcomes with program outcomes and specific outcomes.

Uploaded by

ishverjaiswal40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Lab Manual

OBJECT ORIENTED
PROGRAMMIG USING
JAVA LAB

BCS-452

Semester: IV Year: Second


Session: 2024-25
Department of Computer Science and Engineering

PSIT-Pranveer Singh Institute of Technology


Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Table of Contents

 Vision and Mission Statements of the Institute

 Vision and Mission Statements of the Department

 PEOs, POs, PSOs of the Department

 Evaluation Scheme and Guidelines

 Syllabus

 Lab Plan

 Course Objective and Outcomes

 CO-PO & CO-PSO Correlation Matrix

 List of Experiments

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Vision Statement of the Institute

To achieve excellence in professional education and create an ecosystem for the holistic
development of all stakeholders.

Mission Statement of the Institute

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.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Vision Statement of the Department

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.

Mission Statements of the Department

1. To provide broad based quality education with knowledge and attitude to succeed in
Computer Science & Engineering careers.
2. To prepare students for emerging trends in computer and related industry.
3. To develop competence in students by providing them skills and aptitude to foster
culture of continuous and life-long learning.
4. 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.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Program Educational Objectives (PEOs)


PEOs Description
The graduates will be efficient leading professionals with the knowledge of
PEO1 Computer Science & Engineering discipline that enables them to pursue
higher education and/or successful careers in various domains.
Graduates will possess capability of designing successful innovative solutions
PEO2 to real life problems that are technically sound, economically viable and
socially acceptable.
Graduates will be competent team leaders, effective communicators and
PEO3 capable of working in multidisciplinary teams following ethical values.
The graduates will be capable of adapting to new technologies/tools,
PEO4 constantly upgrading their knowledge and skills with an attitude for lifelong
learning.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Program Outcomes (POs)


POs Graduate Attributes Description
PO1 Engineering Apply the knowledge of mathematics, science and
Knowledge Computer Science & Engineering fundamentals to the
solution of complex engineering problems.
PO2 Problem Analysis Identify, formulate, review research literature, and
analyze complex Computer Science & Engineering
problems reaching substantiated conclusions using
principles of mathematics, natural sciences, and
engineering
PO3 Design/Development Design solutions for Computer Science & Engineering
of Solutions and allied fields related complex 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.
PO4 Investigation Use research-based knowledge of Computer Science &
Engineering and research methods including design of
experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.

PO5 Modern Tool Usage Create, select, and apply appropriate techniques,
resources, and modern engineering and IT tools including
prediction and modeling to complex Computer Science
& Engineering activities with an understanding of the
limitations.
PO6 The Engineering Apply reasoning informed by the contextual knowledge
and Society 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 & Engineering.
PO7 Environment and Understand the impact of the professional Computer
Sustainability Science & Engineering solutions in societal and
environmental contexts, and demonstrate the knowledge

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

of, and need for sustainable development


PO8 Ethics Apply ethical principles and commit to professional
ethics and responsibilities and norms of the Computer
Science & Engineering practice
PO9 Individual and Function effectively as an individual, and as a member or
Team Work leader in diverse teams, and in multidisciplinary settings.
PO10 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.
PO11 Project Demonstrate knowledge and understanding of the
Management and Computer Science & Engineering and management
principles and apply these to one’s own work, as a
Finance
member and leader in a team, to manage projects and in
multidisciplinary environments.
PO12 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.

Program Specific Outcomes (PSOs)


PSOs Description
Use algorithms, data structures/management, software design, concepts of
PSO1
programming languages and computer organization and architecture.
Understand the processes that support the delivery and management of
PSO2
information systems within a specific application environment.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Evaluation Scheme and Guidelines

CT: Class Test


TA: Teacher Assessment
L-T-P: Lecture - Tutorial - Practical

Evaluation Scheme Marks Sub-Total


Performance 20
Viva 10
50
Internal Lab Record 10
Attendance 10
External University Exam 50 50
Grand Total 100

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Syllabus
Following table outline the syllabus for Object Oriented Programming using Java Lab
(BCS-452) as prescribed by Dr. A.P.J. Abdul Kalam Technical University, Uttar
Pradesh, Lucknow. The Syllabus can also be seen on the university website:
https://fms.aktu.ac.in/Resources/aktu/pdf/syllabus/Syllabus2324/
B.Tech_2nd_Yr_CSE_v3.pdf

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

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 to be *Level of Bloom’s Level to be
Taxonomy met Taxonomy met
L1: Remember 1 L2: Understand 2
L3: Apply 3 L4: Analyze 4
L5: Evaluate 5 L6: Create 6

CO Number Course Outcome


To apply [3. Apply] object-oriented approach to solve real-life problems
BCS-452.1 and the 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.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

CO-PO & CO-PSO Correlation Matrix

Mapping of Course Outcomes with Program Outcomes and Program


Specific Outcomes:
Program Outcomes PSO
COs 1 2 3 4 5 6 7 8 9 10 11 12 1 2
BCS-452.1 3 3 - 3 - - - - - - - 3 -
BCS-452.2 - 3 3 - 3 - - - - - - 3 3 -
BCS-452.3 - 3 3 3 3 - - - 3 - - 3 - 3
AVG

Justification of CO-PO Mapping:


PO1: Students will be substantially able to apply fundamental
principles of specifically object-oriented concepts, enabling them to
efficiently solve real-life computational problems.

PO2: Students will substantially acquire analytical skills required to


BCS452.1 formulate and dissect real-life scenarios using object-oriented
with PO1, methodologies, aiding in effective problem resolution.
PO2, PO5
PO5: Students will substantially utilize modern tools like eclipse IDE
or Java NetBeans and techniques associated with Java Collections and
advanced language features, preparing them to efficiently handle
complex computational tasks.

BCS452.2 PO2: Students will substantially enhance their analytical skills by


with PO2, systematically analyzing and comparing various Java Collections,
PO3, PO5, enabling efficient solution formulation and informed decision-making
PO12 in software development.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

PO3: Students will substantially design and implement object-oriented


solutions tailored to specific real-world requirements, demonstrating
their ability to address practical needs accurately.

PO5: Students will substantially utilize modern tools like eclipse IDE
or Java NetBeans and techniques associated with Java Collections and
advanced language features, preparing them to efficiently handle
complex computational tasks.

PO12: Students will use collection framework and new features of java
and will be able to apply it in future software development journey,
hence PO12 is substantially mapped with CO2.

BCS452.3
with PO2, PO2: Students will substantially enhance their analytical skills by
PO3, PO4, developing and deploying web applications using Spring boot.
PO5, PO9
PO3: Students will substantially be able to design and deploy
comprehensive web solutions that address specific functional
requirements, emphasizing usability, scalability, and real-world
applicability.

PO4: Students will substantially be able to investigate and critically


evaluate the performance of system and can re-engineer it by using
web-based framework.

PO5: Students will substantially utilize Spring Boot, a modern


software framework, ensuring they acquire hands-on proficiency in

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

current industry-standard development tools.

PO9: Students will substantially be capable of handling project


individually or in team effectively.

Justification of CO-PSO Mapping:


PSO1: Students will be proficient in object-oriented techniques and
BCS452.2
with methodologies. Hence, it is substantially co-related.
PSO1

PSO2: Students will be able to implement and deploy real-world web


BCS452.3
with applications using industry-standard tools. Hence, it is substantially co-
PSO2 related.

List of Experiments
Lab Lab Experiment Corresponding CO

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

No.
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
1 CO1
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.
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 CO1
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 CO2
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
polymorphism).
4 CO2
 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.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Write a Java program that demonstrates exception


handling using try, catch, and finally blocks to handle
5 arithmetic exceptions. Extend the program to implement CO2
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
6 CO2
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
7 file. The program should demonstrate file reading CO2
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 CO3
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 CO3
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
10 CO3
data back to the user. Demonstrate integration of
backend logic with frontend rendering using
@Controller and Model.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Experiment No. 1

Objective: 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.

Program:
public class EscapeDemo {
public static void main(String[] args) {
// Basic message
System.out.println("Hello, Java World from Eclipse!");

// Using newline (\n)


System.out.println("\nThis is printed on a new line.");

// Using tab (\t)


System.out.println("Name:\tJohn Doe");
System.out.println("Roll:\t101");

// Using backslash (\\)


System.out.println("File path: C:\\Program Files\\Java\\jdk");

// Using double quote (\")


System.out.println("She said, \"Java is awesome!\"");

// Using single quote (\')


System.out.println("It\'s a good day to learn Java.");

// Using carriage return (\r)


System.out.println("Learning Java is fun!\rStart: ");
// Output may vary depending on the console; \r returns to the beginning of the line.
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Experiment No. 2
Objective: 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.
Program:
public class CommandLineSum {
public static void main(String[] args) {
// Check if two arguments are provided
if (args.length < 2) {
System.out.println("Please provide two integer numbers as command-line
arguments.");
return;
}

try {
// Parse the arguments from String to int
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);

// Calculate the sum


int sum = num1 + num2;

// Display the result


System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
} catch (NumberFormatException e) {
System.out.println("Error: Please enter valid integer numbers.");
}}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Experiment No. 3
Objective: 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.
Program:
public class CommandLineSum {
public static void main(String[] args) {
// Check if two arguments are provided
if (args.length < 2) {
System.out.println("Please provide two integer numbers as command-line
arguments.");
return;
}

try {
// Parse the arguments from String to int
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);

// Calculate the sum


int sum = num1 + num2;

// Display the result


System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

} catch (NumberFormatException e) {
System.out.println("Error: Please enter valid integer numbers.");
}
}
}

Experiment No. 4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Objective: 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.
Program:
class Animal {
void makeSound() {
System.out.println("The animal makes a sound.");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("The dog barks.");
}
void bark() {
System.out.println("Dog is barking normally.");
}
void bark(String type) {
System.out.println("Dog is barking in a " + type + " way.");
}
void bark(int times) {
System.out.println("Dog barked " + times + " times.");
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

public class InheritancePolymorphismDemo {


public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
myAnimal.makeSound();
myDog.makeSound();
Dog dog = new Dog();
dog.bark();
dog.bark("loud");
dog.bark(3);
}
}

Experiment No. 5
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Objective: 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.
Program:
class ExceptionHandlingDemo {
void divide(int a, int b) {
try {
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
} finally {
System.out.println("Execution of divide method completed.");
}
}
}

class MyThread1 extends Thread {


public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println("Thread 1 is running...");
}
}
}

class MyThread2 extends Thread {


public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println("Thread 2 is running...");
}
}
}

public class ExceptionAndMultithreadingDemo {


public static void main(String[] args) {
// Exception Handling Demo
ExceptionHandlingDemo handler = new ExceptionHandlingDemo();
handler.divide(10, 2);
handler.divide(10, 0);
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

t1.start();
t2.start();
}
}

Experiment No. 6

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Objective: 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.
Program:
package studentinfo;

public class Student {


private String name;
private int rollNo;
public Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}

public void displayInfo() {


System.out.println("Student Name: " + name);
System.out.println("Roll Number: " + rollNo);
}
}

Experiment No. 7
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Objective: 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.
Program:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReadWriteDemo {


public static void main(String[] args) {
FileReader reader = null;
FileWriter writer = null;

try {
reader = new FileReader("input.txt");
writer = new FileWriter("output.txt");

int ch;
while ((ch = reader.read()) != -1) {
writer.write(ch);
}
System.out.println("File has been read from 'input.txt' and written to 'output.txt'
successfully.");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
try {
if (reader != null) reader.close();
if (writer != null) writer.close();

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

} catch (IOException e) {
System.out.println("Error while closing resources: " + e.getMessage());
}
}
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Experiment No. 8
Objective: 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.
Program:
CSS:

spring-demo/

├── src/
│ └── main/
│ └── java/
│ ├── AppConfig.java
│ ├── MainApp.java
│ └── service/
│ └── GreetingService.java

└── pom.xml (for Maven projects)

1. GreetingService.java (inside service package)

package service;
import org.springframework.stereotype.Component;
public class GreetingService {
public void greet() {
System.out.println("Hello from Spring Dependency Injection!");
}
}

2. AppConfig.java (Spring configuration class)

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
public class AppConfig {
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

3. MainApp.java (Main class)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import service.GreetingService;

public class MainApp {


private GreetingService greetingService;

public static void main(String[] args) {


ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
MainApp app = context.getBean(MainApp.class);
app.displayGreeting();
}

public void displayGreeting() {


greetingService.greet();
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Steps to Run:
1. Set up Maven Project (Optional but recommended):
Add Spring Core dependencies to pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.22</version>
</dependency>
</dependencies>

2. Compile and run the application using your IDE (like IntelliJ or Eclipse) or using
Maven from terminal:
mvn compile
mvn exec:java -Dexec.mainClass="MainApp"

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Experiment No. 9
Objective: 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.
Program:
CSS:

springboot-demo/

├── src/
│ └── main/
│ └── java/
│ └── com/example/demo/
│ ├── DemoApplication.java
│ └── controller/
│ └── HelloController.java

└── pom.xml
1. DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}}
2. HelloController.java
package com.example.demo.controller;
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452
PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloController {
@GetMapping("/greet")
public Map<String, String> greetUser() {
Map<String, String> response = new HashMap<>();
response.put("message", "Hello! Welcome to your first Spring Boot REST API.");
return response;
}
}

Dependencies (pom.xml for Maven):


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Experiment No. 10
Objective: 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.
Program:
CSS:

thymeleaf-demo/

├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/demo/
│ │ ├── ThymeleafDemoApplication.java
│ │ └── controller/
│ │ └── UserController.java
│ └── resources/
│ ├── templates/
│ │ ├── form.html
│ │ └── result.html
│ └── application.properties

└── pom.xml

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

1. ThymeleafDemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThymeleafDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ThymeleafDemoApplication.class, args);
}
}
2. UserController.java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
@GetMapping("/form")
public String showForm() {
return "form";
}
@PostMapping("/submit")
public String submitForm(@RequestParam String name, @RequestParam String
email, Model model) {
model.addAttribute("name", name);
model.addAttribute("email", email);
return "result";
}}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

3. form.html (Inside resources/templates/)


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User Form</title>
</head>
<body>
<h2>User Input Form</h2>
<form action="/submit" method="post">
Name: <input type="text" name="name"/><br><br>
Email: <input type="email" name="email"/><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
4. result.html (Inside resources/templates/)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Result Page</title>
</head>
<body>
<h2>Submitted Details</h2>
<p>Name: <span th:text="${name}"></span></p>
<p>Email: <span th:text="${email}"></span></p>
</body>
</html>

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452


PSIT-Pranveer Singh Institute of Technology
Kanpur-Delhi National Highway (NH-2), Bhauti, Kanpur-209305 (U.P.), India

Dependencies (pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BCS-452

You might also like