0% found this document useful (0 votes)
24 views2 pages

Extra Prac1 Shrey

The document describes a Java program that implements a class named 'BankAccount' with deposit and withdraw methods, and a subclass 'SavingsAccount' that overrides the withdraw method to enforce a minimum balance requirement. The code demonstrates creating instances of both classes, performing deposits and withdrawals, and checking the account balances. It also includes error handling for insufficient funds and invalid deposit amounts.

Uploaded by

shreyvyas.23.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Extra Prac1 Shrey

The document describes a Java program that implements a class named 'BankAccount' with deposit and withdraw methods, and a subclass 'SavingsAccount' that overrides the withdraw method to enforce a minimum balance requirement. The code demonstrates creating instances of both classes, performing deposits and withdrawals, and checking the account balances. It also includes error handling for insufficient funds and invalid deposit amounts.

Uploaded by

shreyvyas.23.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Core Java Programming (CE0421)

Practical : Write a Java program to create a class known as "BankAccount" with methods called
deposit() and withdraw(). Create a subclass called SavingsAccount that overrides the withdraw()
method to prevent withdrawals if the account balance falls below one hundred.
Code:
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(500);
acc.withdraw(200);
System.out.println("Current Balance: " + acc.getBalance());

SavingsAccount savingsAcc = new SavingsAccount();


savingsAccount.deposit(300);
savingsAccount.withdraw(150);
System.out.println("Savings Account Balance: " + savingsAccount.getBalance());

savingsAccount.withdraw(100);
System.out.println("Savings Account Balance after withdrawal: " +
savingsAccount.getBalance());

savingsAccount.withdraw(50);
System.out.println("Savings Account Balance after attempted withdrawal: " +
savingsAccount.getBalance());
}
}

class BankAccount {
private double balance;

public BankAccount() {
this.balance = 0.0;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrew: " + amount);
} else

IU2341230377 B.Tech CSE-D1


Core Java Programming (CE0421)

{
System.out.println("Insufficient balance or invalid amount.");
}
}

public double getBalance() {


return balance;
}
}

class SavingsAccount extends BankAccount {


private static final double MIN_BALANCE = 100.0;

@Override
public void withdraw(double amount) {
if (amount > 0 && (getBalance() - amount) >= MIN_BALANCE) {
super.withdraw(amount);
} else {
System.out.println("Withdrawal denied. Minimum balance of " + MIN_BALANCE + "
must be maintained.");
}
}
}

Output:

Signature Date

IU2341230377 B.Tech CSE-D1

You might also like