Q.
1 Write a Java program to create a banking system with three classes - Bank Account,
SavingsAccount, and CurrentAccount. The bank should have a list of accounts and methods
for adding them. Accounts should be an interface with methods to deposit, withdraw, calculate
interest, and view balances. SavingsAccount and CurrentAccount should implement the
Account interface and have their own unique methods.
-Java Program to Create Banking System;
Imports :(import java.util.ArrayList; and import java.util.List;):
We import ArrayList and List from the java.util package to use them for managing lists of
accounts.
Interface (interface Account):
Defines the operations that any account type should support, such as depositing, withdrawing,
calculating interest, and viewing the balance.
Classes:
BankAccount Class: Implements the Account interface and represents a generic bank account
with a balance. It provides methods to deposit, withdraw, calculate interest, and view the
balance.
SavingsAccount Class: (extends BankAccount): Represents a savings account with an
additional attribute for the interest rate. It overrides the calculateInterest method to calculate the
interest based on the balance and interest rate.
CurrentAccount Class:(extends BankAccount): Represents a current account with an overdraft
limit. It overrides the withdrawal method to check if the withdrawal amount exceeds the balance
plus the overdraft limit.
Bank Class:
Manages a list of accounts (List<Account> accounts) using an ArrayList.
Provides methods to add an account to the list (addAccount) and to retrieve the list of accounts
(getAccounts).
Main Class (public static void main(String[] args)):
Creates an instance of the Bank class.
Creates instances of SavingsAccount and CurrentAccount and adds them to the bank.
Performs operations like depositing, withdrawing, and calculating interest on the accounts.
Displays the balances and interest for the accounts.
Pseudocode:
interface Account
deposit(amount)
withdraw(amount)
calculateInterest()
viewBalance()
class BankAccount implements Account
balance
constructor(initialBalance)
deposit(amount)
add amount to balance
withdraw(amount)
if amount <= balance
subtract amount from balance
else
print "Insufficient balance"
calculateInterest()
return 0 // Placeholder for implementation
viewBalance()
return balance
class SavingsAccount extends BankAccount
interestRate
constructor(initialBalance, interestRate)
calculateInterest()
return balance * interestRate
class CurrentAccount extends BankAccount
overdraftLimit
constructor(initialBalance, overdraftLimit)
withdraw(amount)
if amount <= balance + overdraftLimit
call BankAccount's withdraw method
else
print "Exceeded overdraft limit"
class Bank
accounts: list of Account
constructor()
addAccount(account)
add account to accounts list
getAccounts()
return accounts
main()
bank = new Bank()
savingsAccount = new SavingsAccount(1000, 0.05)
currentAccount = new CurrentAccount(2000, 500)
bank.addAccount(savingsAccount)
bank.addAccount(currentAccount)
print "Savings Account Balance:", savingsAccount.viewBalance()
print "Current Account Balance:", currentAccount.viewBalance()
savingsAccount.deposit(500)
currentAccount.withdraw(300)
print "Savings Account Balance after deposit:", savingsAccount.viewBalance()
print "Current Account Balance after withdrawal:", currentAccount.viewBalance()
print "Savings Account Interest:", savingsAccount.calculateInterest()
Main Code:
import java.util.ArrayList;
import java.util.List;
interface Account {
void deposit(double amount);
void withdraw(double amount);
double calculateInterest();
double viewBalance();
}
class BankAccount implements Account {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance.");
}
}
public double calculateInterest() {
// Implement interest calculation logic here
return 0.0;
}
public double viewBalance() {
return balance;
}
}
class SavingsAccount extends BankAccount {
private double interestRate;
public SavingsAccount(double initialBalance, double interestRate) {
super(initialBalance);
this.interestRate = interestRate;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
@Override
public double calculateInterest() {
return super.viewBalance() * interestRate;
}
}
class CurrentAccount extends BankAccount {
private double overdraftLimit;
public CurrentAccount(double initialBalance, double overdraftLimit) {
super(initialBalance);
this.overdraftLimit = overdraftLimit;
}
public double getOverdraftLimit() {
return overdraftLimit;
}
public void setOverdraftLimit(double overdraftLimit) {
this.overdraftLimit = overdraftLimit;
}
@Override
public void withdraw(double amount) {
if (amount <= viewBalance() + overdraftLimit) {
super.withdraw(amount);
} else {
System.out.println("Exceeded overdraft limit.");
}
}
}
class Bank {
private List<Account> accounts;
public Bank() {
this.accounts = new ArrayList<>();
}
public void addAccount(Account account) {
accounts.add(account);
}
public List<Account> getAccounts() {
return accounts;
}
}
public class Main {
public static void main(String[] args) {
Bank bank = new Bank();
SavingsAccount savingsAccount = new SavingsAccount(1000, 0.05);
CurrentAccount currentAccount = new CurrentAccount(2000, 500);
bank.addAccount(savingsAccount);
bank.addAccount(currentAccount);
System.out.println("Savings Account Balance: " + savingsAccount.viewBalance());
System.out.println("Current Account Balance: " + currentAccount.viewBalance());
savingsAccount.deposit(500);
currentAccount.withdraw(300);
System.out.println("Savings Account Balance after deposit: " +
savingsAccount.viewBalance());
System.out.println("Current Account Balance after withdrawal: " +
currentAccount.viewBalance());
System.out.println("Savings Account Interest: " + savingsAccount.calculateInterest());
}
}
Q.2 Write a java program on how to Reverse a Number in Java.
-Reverse a Number in Java;
public class Main {
public static void main(String[] args) {
int number = 12345;
int reversedNumber = reverseNumber(number);
System.out.println("Original Number: " + number);
System.out.println("Reversed Number: " + reversedNumber);
}
public static int reverseNumber(int number) {
int reversedNumber = 0;
while (number != 0) {
int lastDigit = number % 10;
reversedNumber = reversedNumber * 10 + lastDigit;
number = number / 10;
}
return reversedNumber;
}
}