LAB REPORT
CSE222: Object-Oriented Programming Lab
Topic: Encapsulation
Submitted To
Shahriar Hossain Shanto (SHS)
Designation
Department of CSE, Daffodil International University
Submitted By
Student ID: 0242310005101263
Section: 64_K2
Student Name: Md Rakibul Hasan
Date of Assignment Submission: 22nd November 2024
02
Experiment No: 02 Mapping: CO1 and CO2
Experiment Name Encapsulation
Experiment Details:
Encapsulation:
Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It is the practice of
bundling the data (variables) and methods (functions) that operate on the data into a single unit, known as a
class. Encapsulation helps protect the data from unauthorized access and modification by making variables
private and providing public getter and setter methods for controlled access.
Real-World Example
Consider a Bank Account. The balance of the account should not be directly accessible or modifiable by
external entities. Instead, we provide controlled access through methods like deposit() and withdraw() to
ensure that the balance is updated correctly and securely.
Code:
//file Name: Main.java
class BankAccount {
private String accountHolderName;
private double balance;
public BankAccount(String accountHolderName, double initialBalance) {
this.accountHolderName = accountHolderName;
if (initialBalance > 0) {
this.balance = initialBalance;
} else {
this.balance = 0;
}
}
public String getAccountHolderName() {
return accountHolderName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount + ". New Balance: " +
balance);
} else {
System.out.println("Invalid deposit amount.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrew: " + amount + ". New Balance: " +
balance);
} else {
System.out.println("Invalid withdrawal amount.");
}
}
}
// Main Class
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("Rakibul Hasan", 500.0);
System.out.println("Account Holder: " +
account.getAccountHolderName());
System.out.println("Initial Balance: " + account.getBalance());
account.deposit(250.0);
account.withdraw(140.0);
account.withdraw(734.0);
}
}
Obtained Output:
Account Holder: Rakibul Hasan
Initial Balance: 500.0
Deposited: 250.0. New Balance: 750.0
Withdrew: 140.0. New Balance: 610.0
Invalid withdrawal amount.
Desired
Output?
YES

Encapsulation - Java Object Oriented Programming Lab Report

  • 1.
    LAB REPORT CSE222: Object-OrientedProgramming Lab Topic: Encapsulation Submitted To Shahriar Hossain Shanto (SHS) Designation Department of CSE, Daffodil International University Submitted By Student ID: 0242310005101263 Section: 64_K2 Student Name: Md Rakibul Hasan Date of Assignment Submission: 22nd November 2024 02
  • 2.
    Experiment No: 02Mapping: CO1 and CO2 Experiment Name Encapsulation Experiment Details: Encapsulation: Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It is the practice of bundling the data (variables) and methods (functions) that operate on the data into a single unit, known as a class. Encapsulation helps protect the data from unauthorized access and modification by making variables private and providing public getter and setter methods for controlled access. Real-World Example Consider a Bank Account. The balance of the account should not be directly accessible or modifiable by external entities. Instead, we provide controlled access through methods like deposit() and withdraw() to ensure that the balance is updated correctly and securely. Code: //file Name: Main.java class BankAccount { private String accountHolderName; private double balance; public BankAccount(String accountHolderName, double initialBalance) { this.accountHolderName = accountHolderName; if (initialBalance > 0) { this.balance = initialBalance; } else { this.balance = 0; } } public String getAccountHolderName() { return accountHolderName; } public double getBalance() { return balance; } public void deposit(double amount) { if (amount > 0) { balance += amount;
  • 3.
    System.out.println("Deposited: " +amount + ". New Balance: " + balance); } else { System.out.println("Invalid deposit amount."); } } public void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; System.out.println("Withdrew: " + amount + ". New Balance: " + balance); } else { System.out.println("Invalid withdrawal amount."); } } } // Main Class public class Main { public static void main(String[] args) { BankAccount account = new BankAccount("Rakibul Hasan", 500.0); System.out.println("Account Holder: " + account.getAccountHolderName()); System.out.println("Initial Balance: " + account.getBalance()); account.deposit(250.0); account.withdraw(140.0); account.withdraw(734.0); } } Obtained Output: Account Holder: Rakibul Hasan Initial Balance: 500.0 Deposited: 250.0. New Balance: 750.0 Withdrew: 140.0. New Balance: 610.0 Invalid withdrawal amount. Desired Output? YES