Java Project File
Java Project File
Experiment 1
Use Java Compiler and Eclipse Platform to Write and Execute Java
Program
AIM
To write and execute a basic Java program using the Java compiler (javac) and Eclipse IDE.
OBJECTIVE
Students will learn how to set up the Java Development Environment, write a simple Java program,
and execute it using both command line (javac/java) and Eclipse IDE.
THEORY
Java is a high-level, object-oriented programming language developed by Sun Microsystems. A Java
program is compiled using 'javac' into bytecode (.class file), then executed by the Java Virtual
Machine (JVM) using 'java'.
Eclipse is a popular Integrated Development Environment (IDE) for Java development that provides
features like syntax highlighting, code completion, debugging, and project management.
Steps to compile and run a Java program:
1. Write the source code in a .java file
2. Compile using: javac [Link]
3. Execute using: java ClassName
PROGRAM CODE
// Experiment 1: Hello World - First Java Program
// File: [Link]
[Link]("==============================");
[Link]("==============================");
[Link]();
[Link]("Hello, World!");
// Basic arithmetic
// String operations
Page 1
BCS452 – Object Oriented Programming with Java | Practical File
EXPECTED OUTPUT
==============================
==============================
Hello, World!
Sum of 10 and 20 = 30
CONCLUSION
Successfully wrote and executed a Java program using both command-line compiler and Eclipse
IDE. Understood the process of writing, compiling (.class file generation), and running Java
programs.
Page 2
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 2
OBJECTIVE
To understand how to pass arguments to a Java program through the command line and process
them within the main() method using the String[] args parameter.
THEORY
Command line arguments in Java are passed to the main() method via the String[] args parameter.
These are the arguments provided after the class name when running a Java program.
Syntax: java ClassName arg1 arg2 arg3
Key points:
- args[0] is the first argument, args[1] is the second, etc.
- [Link] gives the total number of arguments passed
- All arguments are received as String; numeric conversions are done using [Link](),
[Link](), etc.
PROGRAM CODE
// Experiment 2: Command Line Arguments
// File: [Link]
if ([Link] == 0) {
return;
if ([Link] >= 1) {
Page 3
BCS452 – Object Oriented Programming with Java | Practical File
if ([Link] >= 2) {
if ([Link] >= 3) {
EXPECTED OUTPUT
=== Command Line Arguments Demo ===
Number of arguments: 3
Name: Alice
Age: 25
GPA: 3.14
args[0] = Alice
args[1] = 25
args[2] = 3.14
Page 4
BCS452 – Object Oriented Programming with Java | Practical File
CONCLUSION
Successfully demonstrated the use of command line arguments in Java. Understood how to access,
count, and convert arguments passed via String[] args at runtime.
Page 5
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 3
OBJECTIVE
To demonstrate the four pillars of OOP — Encapsulation, Abstraction, Inheritance, and
Polymorphism — through practical Java code examples.
THEORY
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design
around objects rather than functions. Java is a purely object-oriented language.
Four pillars of OOP:
1. Encapsulation: Binding data and methods together; hiding data using access modifiers (private,
public, protected).
2. Abstraction: Hiding implementation details and showing only the necessary features of an object.
3. Inheritance: Acquiring properties and behaviors of one class into another using 'extends'.
4. Polymorphism: One method/object behaving differently in different contexts (method overloading
& overriding).
PROGRAM CODE
// Experiment 3: OOP Concepts Demo
// File: [Link]
class Student {
// Constructor
[Link] = name;
[Link] = rollNo;
[Link] = marks;
// Getters
Page 6
BCS452 – Object Oriented Programming with Java | Practical File
// Method
// Inheritance
double radius;
Circle(double r) { [Link] = r; }
Page 7
BCS452 – Object Oriented Programming with Java | Practical File
// Encapsulation
[Link]();
[Link]();
EXPECTED OUTPUT
=== OOP Concepts Demo ===
Page 8
BCS452 – Object Oriented Programming with Java | Practical File
CONCLUSION
Successfully demonstrated all four pillars of OOP — Encapsulation using private fields and
getters/setters, Abstraction through abstract classes, Inheritance through class extension, and
Polymorphism through method overriding.
Page 9
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 4
OBJECTIVE
To understand how inheritance promotes code reusability and how polymorphism allows one
interface to be used for a general class of actions.
THEORY
Inheritance allows a child class to inherit properties and methods from a parent class using the
'extends' keyword. It promotes code reuse.
Types of Inheritance in Java:
1. Single Inheritance: One child inherits from one parent.
2. Multilevel Inheritance: A → B → C (chain of inheritance).
3. Hierarchical Inheritance: One parent, multiple children.
Polymorphism means 'many forms'. Runtime polymorphism is achieved through method overriding
— the method that is called is determined at runtime based on the actual object type.
The 'super' keyword is used to call the parent class constructor or methods.
PROGRAM CODE
// Experiment 4: Inheritance and Polymorphism
// File: [Link]
// Base class
class Animal {
String name;
Animal(String name) {
[Link] = name;
Page 10
BCS452 – Object Oriented Programming with Java | Practical File
// Single Inheritance
String breed;
[Link] = breed;
@Override
// Hierarchical Inheritance
@Override
// Multilevel Inheritance
GuideDog(String name) {
super(name, "Labrador");
@Override
Page 11
BCS452 – Object Oriented Programming with Java | Practical File
// Single Inheritance
[Link]();
// Hierarchical Inheritance
[Link]();
// Multilevel Inheritance
[Link]();
[Link]();
// Runtime Polymorphism
Page 12
BCS452 – Object Oriented Programming with Java | Practical File
EXPECTED OUTPUT
=== Inheritance & Polymorphism Demo ===
-- Single Inheritance --
Bruno is eating.
-- Hierarchical Inheritance --
-- Multilevel Inheritance --
Buddy is eating.
CONCLUSION
Successfully demonstrated single, multilevel, and hierarchical inheritance. Implemented runtime
polymorphism through method overriding and upcasting. Understood the use of 'super' keyword.
Page 13
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 5
OBJECTIVE
To understand how to handle runtime errors gracefully and how to create concurrent programs using
threads in Java.
THEORY
Exception Handling: An exception is an unwanted event that interrupts normal program execution.
Java uses try, catch, finally, throw, and throws keywords.
- try: Block that contains code that might throw an exception.
- catch: Block that handles the specific exception.
- finally: Block that always executes, used for cleanup.
- throws: Declares exceptions a method may throw.
Multithreading: A thread is a lightweight sub-process. Java supports multithreading via:
1. Extending the Thread class
2. Implementing the Runnable interface
Thread lifecycle: New → Runnable → Running → Blocked → Dead
PROGRAM CODE
// Experiment 5: Exception Handling and Multithreading
// File: [Link]
class ExceptionDemo {
// Custom Exception
Page 14
BCS452 – Object Oriented Programming with Java | Practical File
// ArithmeticException
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
} finally {
// ArrayIndexOutOfBoundsException
try {
[Link](arr[10]);
} catch (ArrayIndexOutOfBoundsException e) {
// NumberFormatException
try {
int n = [Link]("abc");
} catch (NumberFormatException e) {
// Custom exception
try {
withdraw(1000.0, 1500.0);
} catch (NegativeBalanceException e) {
try {
withdraw(1000.0, 500.0);
Page 15
BCS452 – Object Oriented Programming with Java | Practical File
} catch (NegativeBalanceException e) {
[Link]([Link]());
String threadName;
@Override
@Override
[Link]();
Page 16
BCS452 – Object Oriented Programming with Java | Practical File
[Link]();
[Link]();
[Link]();
EXPECTED OUTPUT
=== Exception Handling Demo ===
...
CONCLUSION
Successfully implemented exception handling using try-catch-finally and custom exceptions. Created
multithreaded programs using both Thread class and Runnable interface. Observed concurrent
execution behavior.
Page 17
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 6
OBJECTIVE
To understand how packages organize Java classes, how to create user-defined packages, and how
import statements are used to access them.
THEORY
A package in Java is a namespace that organizes a set of related classes and interfaces. Packages
help avoid name conflicts and provide access protection.
Types of Packages:
1. Built-in Packages: [Link], [Link], [Link], [Link], etc.
2. User-defined Packages: Created using the 'package' keyword.
Creating a package: package packageName; (first statement in the Java file)
Importing: import [Link]; or import packageName.*;
Access Modifiers with Packages:
- public: Accessible everywhere
- protected: Accessible within package and subclasses
- default (no modifier): Accessible only within same package
- private: Accessible only within the same class
PROGRAM CODE
// Experiment 6: Java Packages
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
class GeometryUtil {
Page 18
BCS452 – Object Oriented Programming with Java | Practical File
class StringUtil {
return [Link](reverse(s));
return [Link]().split("\\s+").length;
[Link](list);
// Using [Link]
Page 19
BCS452 – Object Oriented Programming with Java | Practical File
EXPECTED OUTPUT
=== Java Packages Demo ===
-- [Link] Package --
Original: [5, 2, 8, 1, 9, 3]
Sorted: [1, 2, 3, 5, 8, 9]
Max: 9, Min: 1
-- [Link] Package --
Sqrt(144) = 12.0
2^10 = 1024
Pi = 3.14159
Word count: 4
Page 20
BCS452 – Object Oriented Programming with Java | Practical File
CONCLUSION
Successfully created and used Java packages. Demonstrated usage of built-in packages ([Link],
[Link]) and simulated user-defined utility packages for geometry and string operations.
Page 21
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 7
OBJECTIVE
To understand how to read and write data to files using various I/O streams — FileWriter,
FileReader, BufferedReader, BufferedWriter, and Scanner.
THEORY
Java I/O (Input/Output) is used to process input and produce output. The [Link] package provides
classes for file handling.
Key I/O Classes:
- FileWriter / FileReader: Write/Read characters to/from files.
- BufferedWriter / BufferedReader: Buffered I/O for efficient reading/writing.
- PrintWriter: Prints formatted text to a file.
- Scanner: Reads input from files or console.
Streams in Java:
- Byte Streams: Handle I/O of 8-bit bytes (FileInputStream, FileOutputStream).
- Character Streams: Handle I/O of 16-bit Unicode characters (FileReader, FileWriter).
Always close file streams after use, preferably using try-with-resources.
PROGRAM CODE
// Experiment 7: Java I/O Package
// File: [Link]
import [Link].*;
import [Link];
[Link]();
[Link]();
[Link]();
Page 22
BCS452 – Object Oriented Programming with Java | Practical File
[Link]();
[Link]("Date: 2025-04-01");
[Link]();
String line;
int lineNo = 1;
[Link]();
[Link]();
String line;
Page 23
BCS452 – Object Oriented Programming with Java | Practical File
[Link](line);
[Link]();
try {
// Write
writeToFile(filename);
// Append
appendToFile(filename);
// Read
readFromFile(filename);
// Copy
copyFile(filename, copyFile);
readFromFile(copyFile);
} catch (IOException e) {
Page 24
BCS452 – Object Oriented Programming with Java | Practical File
EXPECTED OUTPUT
=== Java I/O Package Demo ===
5: Date: 2025-04-01
6:
CONCLUSION
Successfully demonstrated Java I/O operations including writing, reading, appending, and copying
files using BufferedReader, BufferedWriter, FileReader, and FileWriter. Used try-with-resources for
automatic stream closing.
Page 25
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 8
OBJECTIVE
To understand Spring Framework's core concepts — IoC (Inversion of Control), Dependency
Injection (DI), Spring Beans, and the Spring MVC pattern — by building a practical application.
THEORY
Spring Framework is the most popular Java enterprise framework for building scalable, maintainable
industry-level applications.
Core Concepts:
1. IoC Container: Manages Java object lifecycle and configuration (ApplicationContext,
BeanFactory).
2. Dependency Injection (DI): Objects are provided their dependencies rather than creating them —
via Constructor Injection or Setter Injection.
3. Spring Beans: Objects managed by the Spring IoC container, defined using @Component,
@Service, @Repository annotations.
4. Spring MVC: Model-View-Controller pattern for web applications using @Controller,
@RequestMapping.
5. @Autowired: Auto-wires dependencies by type.
Key Annotations: @Component, @Service, @Repository, @Controller, @Autowired,
@Configuration, @Bean
Maven Dependency: spring-context (for core), spring-webmvc (for web layer).
PROGRAM CODE
// Experiment 8: Industry Application using Spring Framework
import [Link].*;
class Employee {
Page 26
BCS452 – Object Oriented Programming with Java | Practical File
class EmployeeRepository {
return result;
class EmployeeService {
Page 27
BCS452 – Object Oriented Programming with Java | Practical File
[Link]("\n" + "-".repeat(60));
[Link]("-".repeat(60));
[Link]([Link]::println);
[Link]("-".repeat(60));
return
[Link]().stream().mapToDouble(Employee::getSalary).average().orElse(0);
Page 28
BCS452 – Object Oriented Programming with Java | Practical File
class EmployeeController {
// @GetMapping("/employees")
[Link]("\n[GET /employees]");
[Link]();
// @PostMapping("/employees")
[Link]("\n[POST /employees]");
// @GetMapping("/employees/report")
[Link]("\n[GET /employees/report]");
[Link]();
Page 29
BCS452 – Object Oriented Programming with Java | Practical File
[Link]();
[Link]();
EXPECTED OUTPUT
=== Spring Framework - Employee Management App ===
[POST /employees]
[GET /employees]
------------------------------------------------------------
------------------------------------------------------------
[GET /employees/report]
Page 30
BCS452 – Object Oriented Programming with Java | Practical File
Engineering: 3 employee(s)
HR: 1 employee(s)
Finance: 1 employee(s)
CONCLUSION
Successfully demonstrated the Spring Framework architecture with three-layer design: Repository
(data), Service (business logic), and Controller (request handling). Implemented Dependency
Injection via constructor injection and simulated the Spring IoC container.
Page 31
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 9
OBJECTIVE
To understand how Spring Boot simplifies REST API development using @RestController,
@RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and
ResponseEntity.
THEORY
REST (Representational State Transfer) is an architectural style for building web services. RESTful
APIs use HTTP methods to perform CRUD operations.
HTTP Methods in REST:
- GET: Retrieve data (Read)
- POST: Create new data
- PUT: Update existing data
- DELETE: Remove data
Spring Boot Key Annotations:
- @SpringBootApplication: Marks the main entry point; enables auto-configuration.
- @RestController: Combines @Controller + @ResponseBody; returns JSON directly.
- @RequestMapping: Maps HTTP requests to handler methods.
- @PathVariable: Extracts value from URI path.
- @RequestBody: Maps HTTP request body to a Java object.
- ResponseEntity<T>: Represents HTTP response including status code and body.
Spring Boot auto-configures an embedded Tomcat server on port 8080 by default.
PROGRAM CODE
// Experiment 9: RESTful Web Services using Spring Boot
import [Link].*;
class Product {
public Product(int id, String name, String category, double price, int qty) {
Page 32
BCS452 – Object Oriented Programming with Java | Practical File
class ResponseEntity<T> {
private T body;
[Link]("\n[" + method + " " + uri + "] -> HTTP " + statusCode
+ " " + status);
// @RequestMapping("/api/products")
class ProductController {
Page 33
BCS452 – Object Oriented Programming with Java | Practical File
[Link]("]");
Product p = [Link](id);
[Link]([Link](), p);
Product p = [Link](id);
[Link](price); [Link](qty);
[Link](id);
Page 34
BCS452 – Object Oriented Programming with Java | Practical File
// GET all
[Link]().print("GET", "/api/products");
// GET by ID
[Link](1).print("GET", "/api/products/1");
[Link](99).print("GET", "/api/products/99");
// PUT - Update
// DELETE
[Link](3).print("DELETE", "/api/products/3");
[Link]().print("GET", "/api/products");
EXPECTED OUTPUT
Page 35
BCS452 – Object Oriented Programming with Java | Practical File
Response: [
CONCLUSION
Successfully built and tested a RESTful API with full CRUD operations using Spring Boot patterns.
Demonstrated GET, POST, PUT, DELETE endpoints with proper HTTP status codes (200, 201,
404). Understood @RestController, @RequestMapping, @PathVariable, and ResponseEntity.
Page 36
BCS452 – Object Oriented Programming with Java | Practical File
Experiment 10
OBJECTIVE
To understand how Spring Boot serves static frontend files, how Thymeleaf template engine works,
and how a frontend communicates with Spring Boot REST endpoints using Fetch API.
THEORY
Spring Boot can serve frontend web applications in two ways:
1. Static Resources: Place HTML/CSS/JS files in src/main/resources/static/ folder. Spring Boot
auto-serves them.
2. Thymeleaf Templates: Server-side rendering using Thymeleaf engine; files placed in
src/main/resources/templates/.
Thymeleaf Key Attributes:
- th:text: Renders model attribute as text content
- th:each: Loops over a collection
- th:if / th:unless: Conditional rendering
- th:href / th:src: Dynamic URL generation
- th:action: Form action URL
Frontend-Backend Integration:
- The browser sends HTTP requests (via forms or JavaScript Fetch API) to Spring Boot REST
endpoints.
- Spring Boot processes the request and returns JSON or renders a Thymeleaf view.
- The response is displayed dynamically using JavaScript DOM manipulation.
Project Structure:
src/main/java/ -> Java source (Controllers, Services, Models)
src/main/resources/static/ -> HTML, CSS, JS files
src/main/resources/templates/ -> Thymeleaf HTML templates
PROGRAM CODE
// Experiment 10: Frontend Web Application with Spring Boot
// ============================================
// ============================================
// package [Link];
//
// import [Link];
// import [Link];
// import [Link].*;
Page 37
BCS452 – Object Oriented Programming with Java | Practical File
// import [Link].*;
import [Link].*;
class StudentController {
// @GetMapping("/") or @GetMapping("/students")
[Link]("students", students);
[Link]("count", [Link]());
// @PostMapping("/students/add")
[Link]("id", [Link](nextId++));
[Link]("name", name);
[Link]("branch", branch);
[Link]("email", email);
[Link](s);
return showHomePage(model);
// @DeleteMapping("/students/{id}")
Page 38
BCS452 – Object Oriented Programming with Java | Practical File
// ============================================
// Path: src/main/resources/templates/[Link]
// ============================================
/*
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<title>Student Management</title>
</head>
<body>
<div class="container">
</form>
<table>
<tr><th>ID</th><th>Name</th><th>Branch</th><th>Email</th><th>Action</th></tr>
<td th:text="${[Link]}">1</td>
<td th:text="${[Link]}">Name</td>
<td th:text="${[Link]}">Branch</td>
<td th:text="${[Link]}">Email</td>
<td>
Page 39
BCS452 – Object Oriented Programming with Java | Practical File
<button >
</td>
</tr>
<tr th:if="${#[Link](students)}">
</tr>
</table>
</div>
<script th:src="@{/js/[Link]}"></script>
</body>
</html>
*/
// ============================================
// Path: src/main/resources/static/js/[Link]
// ============================================
/*
function deleteStudent(id) {
.then(data => {
})
*/
// ============================================
// ============================================
Page 40
BCS452 – Object Oriented Programming with Java | Practical File
[Link](model);
@SuppressWarnings("unchecked")
[Link]("Rendered Table:");
[Link]("-".repeat(55));
[Link]("id"),[Link]("name"),[Link]("branch"),[Link]("email")));
[Link]([Link](2));
// Updated view
[Link](model);
[Link]("id"),[Link]("name"),[Link]("branch")));
EXPECTED OUTPUT
=== Spring Boot Full-Stack App ===
Page 41
BCS452 – Object Oriented Programming with Java | Practical File
Rendered Table:
-------------------------------------------------------
3 | Amit Verma | ME
CONCLUSION
Successfully built a full-stack web application using Spring Boot backend and an HTML/Thymeleaf
frontend. Demonstrated static file serving, Thymeleaf template rendering with model data, form
submission via POST, and asynchronous DELETE via JavaScript Fetch API.
Page 42