Lab Report
Only for course Teacher
Needs Developing Sufficient Above Total
Improvement Average Mark
Allocate mark & Percentage 25% 50% 75% 100% 25
Understanding 3
Analysis 4
Implementation 8
Report Writing 10
Total obtained mark
Comments
Semester: Spring 20
Course Code: SE 217 Course Name: Object Oriented Programming Lab
Course Teacher Name:
Designation: Assistant Professor
Submission Date: 20/04/2026
Lab 1. Setting Up the Development Environment and Writing the First Java Program;
Introduction to IDE.
Objective: To set up JDK and an IDE (Eclipse/IntelliJ/VS Code), understand the basic
structure of a Java program, and execute the classic "Hello World" program.
Tasks:
Install JDK and configure environment variables.
Install and configure an IDE.
Write and run the first Java program.
Code:
package helloworld;
public class Helloworld {
public static void main(String[] args) {
[Link]("Hello World!");
}
}
Output:
Explanation: The program demonstrates the basic structure: public class, public static void
main(String[] args) as the entry point, and [Link]() for output.
Conclusion: Successfully set up the development environment and executed the first Java
program.
Lab 2. Implementing Access Specifiers; Working with Static and Non-Static Members; Creating
Classes, Objects, and Methods; Understanding Constructors.
Objective: To understand access specifiers, static vs non-static members, classes, objects,
and constructors.
Tasks:
Create a Student class with different access specifiers.
Implement default and parameterized constructors.
Use static and non-static members.
Code :
package lab2;
class Student {
private String name;
protected int id;
public static String university = "University of Dhaka";
public Student() {
[Link] = "Seum";
[Link] = 111;
}
public Student(String name, int id) {
[Link] = name;
[Link] = id;
}
public void displayInfo() {
[Link]("Name: " + name + " | ID: " + id + " | University: " + university);
}
public static void changeUniversity(String newUni) {
university = newUni;
}
}
public class Lab2 {
public static void main(String[] args) {
Student A1=new Student("Abir",209);
Student A2=new Student();
[Link]();
[Link]();
[Link]("DIU");
[Link]();
}
}
Output:
Explanation: Demonstrated private, protected, public access specifiers, constructor overloading
and static members.
Conclusion: Good understanding of OOP basics achieved.
Lab 3. Exploring Encapsulation in Java.
Objective: To implement Encapsulation using private variables and getter/setter methods.
Tasks:
Create BankAccount class with encapsulated data.
Add validation in deposit and withdraw methods.
Code:
package lab2;
class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accNo, double initial) {
[Link] = accNo;
[Link] = [Link](initial, 0);
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Deposited: " + amount);
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
}
public class Lab2 {
public static void main(String[] args) {
BankAccount acc = new BankAccount("ACC2026", 10000);
[Link](5000);
[Link]("Current Balance: " + [Link]());
[Link]("Withdraw 7000: " + [Link](7000));
[Link]("Final Balance: " + [Link]());
}
}
Output:
Explanation: Data is hidden using private and accessed only through public methods with
validation.
Conclusion: Encapsulation successfully implemented.
Lab 4: Understanding Inheritance and Polymorphism.
Objective: To understand Inheritance and Polymorphism (Method Overriding).
Tasks:
Create Shape class.
Create Circle and Rectangle subclasses.
Demonstrate runtime polymorphism.
Code :
package lab2;
class Shape {
public double getArea() {
return 0.0;
}
}
class Circle extends Shape {
private double radius;
public Circle(double r) { [Link] = r; }
@Override
public double getArea() {
return [Link] * radius * radius;
}
}
class Rectangle extends Shape {
private double length, width;
public Rectangle(double l, double w) {
[Link] = l;
[Link] = w;
}
@Override
public double getArea() {
return length * width;
}
}
public class Lab2 {
public static void main(String[] args) {
Shape s1 = new Circle(5.0);
Shape s2 = new Rectangle(10, 6);
[Link]("Circle Area: %.2f\n", [Link]());
[Link]("Rectangle Area: %.2f\n", [Link]());
}
}
Output:
Explanation: Method overriding allows runtime polymorphism.
Conclusion: Inheritance and Polymorphism concepts cleared.
Lab 5: Converting UML Diagrams to Java Code: Generalization, Association, Aggregation, and
Composition.
Objective: To convert UML relationships into Java code.
Tasks:
Association: General relationship between objects
Aggregation: Weak “has-a” relationship
Composition: Strong “has-a” relationship (lifecycle dependent)
Code:
package lab2;
class Heart {
public void beat() {
[Link]("Heart is beating...");
}
}
// Aggregation
class Professor {
String name;
public Professor(String name) { [Link] = name; }
}
// Association
class Course {
String title;
public Course(String title) { [Link] = title; }
}
class Student {
String name;
Course enrolledCourse; // Association
Professor advisor; // Aggregation
Heart heart = new Heart(); // Composition
public Student(String name, Course c, Professor p) {
[Link] = name;
[Link] = c;
[Link] = p;
}
}
public class Lab2 {
public static void main(String[] args) {
Professor p = new Professor("Dr. Rahman");
Course c = new Course("Object Oriented Programming");
Student s = new Student("Ahmmed", c, p);
[Link]();
[Link]([Link] + " is studying " + [Link]);
}
}
Output:
Explanation: Composition means strong relationship where one object depends on another.
Conclusion: Understood UML relationships in Java.
Lab 6: Working with Packages and Interfaces in Java.
Objective: To understand how to use Packages and Interfaces in Java for better code
organization and multiple inheritance.
Tasks:
Create a package.
Create multiple interfaces.
Implement interfaces in a class.
Code:
package lab2;
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Duck implements Flyable, Swimmable {
@Override
public void fly() {
[Link]("Duck is flying high...");
}
@Override
public void swim() {
[Link]("Duck is swimming in the pond.");
}
}
public class Lab2 {
public static void main(String[] args) {
Duck d = new Duck();
[Link]();
[Link]();
}
}
Output:
Explanation: Interfaces allow multiple inheritance. Package helps to organize classes.
Conclusion: Successfully learned Packages and Interfaces.
Lab 7: Exception Handling in Java.
Objective: To handle runtime errors using try-catch-finally and create custom exceptions.
Tasks:
Handle built-in exceptions.
Create and throw custom exception.
Code:
package lab2;
public class Lab2 {
public static void main(String[] args) {
int a;
try {
a = 10 / 0;
} catch (Exception e) {
[Link]("Error occurred");
}
}
}
Output:
Explanation: try-catch handles exceptions, finally block always runs. Custom exception created
by extending Exception class.
Conclusion: Exception handling implemented successfully.
Lab 8: Introduction to the Java Collections Framework (Ex: ArrayList).
Objective: To use ArrayList for dynamic array operations.
Tasks:
Add, remove, and sort elements in ArrayList.
Search elements.
Code:
package lab2;
import [Link].*;
public class Lab2 {
public static void main(String[] args) {
ArrayList<String> students = new ArrayList<>();
[Link]("Ahmmed");
[Link]("Rahim");
[Link]("Karim");
[Link]("Sami");
[Link]("Students List: " + students);
[Link](2); // Remove Karim
[Link]("After Remove: " + students);
[Link](students);
[Link]("Sorted List: " + students);
[Link]("Contains Ahmmed: " + [Link]("Ahmmed"));
[Link]("Total Students: " + [Link]());
}
}
Output:
Explanation: ArrayList is dynamic in size and part of Java Collections Framework.
Conclusion: Learned basic operations of ArrayList.
Lab 9: Java Input/Output (IO): Perform file operations : reading data from a text file, writing
data to a file, searching for specific data within a file and appending data to an existing file.
Objective: To perform file reading, writing, searching, and appending using Java IO.
Tasks:
Write data to file.
Append data.
Read file and search specific data.
Code :
package lab2;
import [Link].*;
import [Link];
public class Lab2 {
public static void main(String[] args) {
String fileName = "[Link]";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
[Link]("Ahmmed\n");
[Link]("Rahim\n");
[Link]("Karim\n");
[Link]("Data written successfully.");
} catch (IOException e) {
[Link]();
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
[Link]("Sami\n");
[Link]("Data appended successfully.");
} catch (IOException e) {
[Link]();
}
try (Scanner scanner = new Scanner(new File(fileName))) {
[Link]("\nFile Contents:");
while ([Link]()) {
String line = [Link]();
[Link](line);
if ([Link]("Ahmmed")) {
[Link](">>> Found: Ahmmed (Search Successful)");
}
}
} catch (FileNotFoundException e) {
[Link]("File not found!");
}
}
}
Output:
Explanation: Used BufferedWriter for writing/appending and Scanner for reading.
Conclusion: File handling operations completed successfully.
Lab 10: Refining Design Practices: Assessing Cohesion, Complexity, and Coupling.
Objective: To evaluate and improve code quality by focusing on Cohesion, Coupling, and
Complexity.
Tasks:
Cohesion: Degree to which elements of a module belong together (higher is better)
Coupling: Degree of dependency between modules (lower is better
Code:
package lab2;
class Calculator {
int add(int a, int b) {
return a + b;
}
}
class Display {
void show(int result) {
[Link]("Result: " + result);
}
}
public class Lab2 {
public static void main(String[] args) {
Calculator calc = new Calculator();
Display display = new Display();
int result = [Link](5, 3);
[Link](result);
}
}
Output:
Explanation :
The Calculator class performs only calculation → High Cohesion
The Display class handles output separately → Single Responsibility
Both classes work independently → Low Coupling
Simple structure → Low Complexity
Conclusion: The program follows good design principles by maintaining high cohesion, low
coupling, and minimal complexity, making the code clean and maintainable.