0% found this document useful (0 votes)
28 views5 pages

OOP Lab Report

The document contains two programming problems involving object-oriented concepts in Java. The first problem involves creating a class with private instance variables for account number and balance, and public getter and setter methods. The second problem expands on this by adding additional private variables for name, date of birth, and date of account opening, and implementing deposit and withdraw methods while preventing direct updates to the balance.

Uploaded by

MTE1991
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)
28 views5 pages

OOP Lab Report

The document contains two programming problems involving object-oriented concepts in Java. The first problem involves creating a class with private instance variables for account number and balance, and public getter and setter methods. The second problem expands on this by adding additional private variables for name, date of birth, and date of account opening, and implementing deposit and withdraw methods while preventing direct updates to the balance.

Uploaded by

MTE1991
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/ 5

-

Bangladesh University of Professionals


Dept. of CSE
Faculty of Science and Technology
Object Oriented Programming Laboratory - II
CSE 2208
Lab report: 04

Submitted by Mostafa Tahsin Ekleel


Id no. 2252421052
Submitted to Anup Majumder &
Sobhana Jahan
Date 5th May, 2024
Problem 01: Write down a Java program creating a class with private instance and
variable, account number & balance. Implement encapsulation, public getter &
setter methods.
Code:
package com.example.gettersetter;

public class BankAccount {

private String accountNo;


private double balance;

public String getAccountNo() {


return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}

Output:
Problem 02: Write down a Java program creating a class with private instance and
variable, account number, account holder’s name, date of birth, date of account
opening & balance. Implement encapsulation, public getter & setter methods in
such a way that the user cannot change their account balance themselves.
Code:
// BankAccount.java
package com.example.bankacc;

public class BankAccount {


private String name;
private String accountNo;
private String dob; // Format: DD/MM/YYYY
private double balance = 0;

public BankAccount(String name, String accountNo, String


dob) {
this.name = name;
this.accountNo = accountNo;
this.dob = dob;
}

public void depositBalance(double amount) {


if (amount <= 0) {
System.out.println("Insufficient amount!");
return;
}
balance += amount;
}

public double withdrawBalance(double amount) {


if (amount <= 0) {
System.out.println("Error! Amount can't be
negative.");
return balance;
}
if (balance < amount) {
System.out.println("Insufficient balance!");
return balance;
}
balance -= amount;
return balance; // balance after withdrawal
}

public double getBalance() {


return balance;
}

// Main.java
package com.example.bankacc;
public class Main {
public static void main(String[] args) {
BankAccount acc1 = new BankAccount("Mizanur Rahman",
"123456", "23/08/2001");
acc1.depositBalance(100);
System.out.println(acc1.getBalance());
acc1.withdrawBalance(60);
System.out.println(acc1.getBalance());
}

}
Output:

You might also like