0% found this document useful (0 votes)
4 views

Set 7

The document contains Java code for a banking system with abstract classes and subclasses for SavingsAccount and CurrentAccount, allowing for deposit and withdrawal operations. It also includes a separate program for swapping rows and columns of a user-defined 2D array, displaying both the original and swapped arrays. The code demonstrates object-oriented programming principles and user interaction through console input.

Uploaded by

kareena.it222110
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)
4 views

Set 7

The document contains Java code for a banking system with abstract classes and subclasses for SavingsAccount and CurrentAccount, allowing for deposit and withdrawal operations. It also includes a separate program for swapping rows and columns of a user-defined 2D array, displaying both the original and swapped arrays. The code demonstrates object-oriented programming principles and user interaction through console input.

Uploaded by

kareena.it222110
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/ 9

Set 7

import java.util.Scanner;

// Abstract class: Accounts

abstract class Accounts {

protected double balance;

protected int accountNumber;

protected String accountHolderName;

protected String address;

// Abstract method: withdrawal

public abstract void withdrawal(double amount);

// Abstract method: deposit

public abstract void deposit(double amount);

// Method to display the balance

public void display() {

System.out.println("Account Number: " + accountNumber);

System.out.println("Account Holder Name: " + accountHolderName);

System.out.println("Address: " + address);

System.out.println("Balance: " + balance);

// Subclass: SavingsAccount

class SavingsAccount extends Accounts {

private double rateOfInterest;

// Constructor for SavingsAccount


public SavingsAccount(int accountNumber, String accountHolderName, String address, double
initialBalance, double rateOfInterest) {

this.accountNumber = accountNumber;

this.accountHolderName = accountHolderName;

this.address = address;

this.balance = initialBalance;

this.rateOfInterest = rateOfInterest;

// Implement withdrawal method

@Override

public void withdrawal(double amount) {

if (amount <= balance) {

balance -= amount;

System.out.println("Withdrawal successful! Current balance: " + balance);

} else {

System.out.println("Insufficient balance.");

// Implement deposit method

@Override

public void deposit(double amount) {

balance += amount;

System.out.println("Deposit successful! Current balance: " + balance);

// Method to calculate the amount after adding interest

public double calculateAmount() {

return balance + (balance * rateOfInterest / 100);

}
// Display method for SavingsAccount

@Override

public void display() {

super.display();

System.out.println("Rate of Interest: " + rateOfInterest + "%");

System.out.println("New Balance with Interest: " + calculateAmount());

// Subclass: CurrentAccount

class CurrentAccount extends Accounts {

private double overdraftLimit;

// Constructor for CurrentAccount

public CurrentAccount(int accountNumber, String accountHolderName, String address, double


initialBalance, double overdraftLimit) {

this.accountNumber = accountNumber;

this.accountHolderName = accountHolderName;

this.address = address;

this.balance = initialBalance;

this.overdraftLimit = overdraftLimit;

// Implement withdrawal method

@Override

public void withdrawal(double amount) {

if (amount <= balance + overdraftLimit) {

balance -= amount;

System.out.println("Withdrawal successful! Current balance: " + balance);

} else {
System.out.println("Exceeds overdraft limit.");

// Implement deposit method

@Override

public void deposit(double amount) {

balance += amount;

System.out.println("Deposit successful! Current balance: " + balance);

// Display method for CurrentAccount

@Override

public void display() {

super.display();

System.out.println("Overdraft Limit: " + overdraftLimit);

public class BankSystem {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Create a SavingsAccount object

System.out.println("Enter details for Savings Account:");

System.out.print("Account Number: ");

int savingsAccountNumber = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Account Holder Name: ");

String savingsHolderName = scanner.nextLine();

System.out.print("Address: ");
String savingsAddress = scanner.nextLine();

System.out.print("Initial Balance: ");

double savingsInitialBalance = scanner.nextDouble();

System.out.print("Rate of Interest: ");

double savingsRateOfInterest = scanner.nextDouble();

SavingsAccount savingsAccount = new SavingsAccount(savingsAccountNumber,


savingsHolderName, savingsAddress, savingsInitialBalance, savingsRateOfInterest);

// Create a CurrentAccount object

System.out.println("\nEnter details for Current Account:");

System.out.print("Account Number: ");

int currentAccountNumber = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Account Holder Name: ");

String currentHolderName = scanner.nextLine();

System.out.print("Address: ");

String currentAddress = scanner.nextLine();

System.out.print("Initial Balance: ");

double currentInitialBalance = scanner.nextDouble();

System.out.print("Overdraft Limit: ");

double overdraftLimit = scanner.nextDouble();

CurrentAccount currentAccount = new CurrentAccount(currentAccountNumber,


currentHolderName, currentAddress, currentInitialBalance, overdraftLimit);

// Test the SavingsAccount methods

System.out.println("\n--- Savings Account Details ---");

savingsAccount.display();

// Test the CurrentAccount methods

System.out.println("\n--- Current Account Details ---");


currentAccount.display();

scanner.close();

Output

Enter details for Savings Account:

Account Number: 12345

Account Holder Name: John Doe

Address: 456 Elm Street

Initial Balance: 1000

Rate of Interest: 5

Enter details for Current Account:

Account Number: 67890

Account Holder Name: Jane Smith

Address: 789 Oak Street

Initial Balance: 2000

Overdraft Limit: 500

Q2

import java.util.Scanner;

public class ArrayRowColumnSwap {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input dimensions of the array

System.out.print("Enter number of rows: ");

int rows = scanner.nextInt();

System.out.print("Enter number of columns: ");

int columns = scanner.nextInt();


// Declare the array

int[][] array = new int[rows][columns];

// Input the elements of the array

System.out.println("Enter the elements of the array:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print("Element [" + i + "][" + j + "]: ");

array[i][j] = scanner.nextInt();

// Display the original array

System.out.println("\nOriginal Array:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(array[i][j] + " ");

System.out.println();

// Swap rows and columns

int[][] swappedArray = new int[columns][rows];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

swappedArray[j][i] = array[i][j];

// Display the swapped array


System.out.println("\nAfter changing the rows and columns:");

for (int i = 0; i < columns; i++) {

for (int j = 0; j < rows; j++) {

System.out.print(swappedArray[i][j] + " ");

System.out.println();

scanner.close();

Output

Enter number of rows: 2

Enter number of columns: 3

Enter the elements of the array:

Element [0][0]: 10

Element [0][1]: 20

Element [0][2]: 30

Element [1][0]: 40

Element [1][1]: 50

Element [1][2]: 60

Original Array:

10 20 30

40 50 60

After changing the rows and columns:

10 40

20 50

30 60

Enter number of rows: 3


Enter number of columns: 2

Enter the elements of the array:

Element [0][0]: 7

Element [0][1]: 8

Element [1][0]: 9

Element [1][1]: 10

Element [2][0]: 11

Element [2][1]: 12

Original Array:

78

9 10

11 12

After changing the rows and columns:

7 9 11

8 10 12

You might also like