BCS-452 (Object Oriented Programming Using Java Lab) 2023-24
BCS-452 (Object Oriented Programming Using Java Lab) 2023-24
OBJECT ORIENTED
PROGRAMMIG USING
JAVA LAB
BCS-452
Table of Contents
Syllabus
Lab Plan
List of Experiments
To achieve excellence in professional education and create an ecosystem for the holistic
development of all stakeholders.
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.
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
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
Lab Plan
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.
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.
List of Experiments
Lab Lab Experiment Corresponding CO
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.
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!");
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]);
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]);
} 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
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.");
}
}
}
t1.start();
t2.start();
}
}
Experiment No. 6
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;
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();
} catch (IOException e) {
System.out.println("Error while closing resources: " + e.getMessage());
}
}
}
}
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)
package service;
import org.springframework.stereotype.Component;
public class GreetingService {
public void greet() {
System.out.println("Hello from Spring Dependency Injection!");
}
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
public class AppConfig {
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import service.GreetingService;
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"
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;
}
}
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
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";
}}
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>