BankAccount and SavingsAccount Classes in Java
Write a Java program to create a class called "BankAccount" with attributes for account number, account holder's name, and balance. Include methods for depositing and withdrawing money, as well as checking the balance. Create a subclass called "SavingsAccount" that adds an interest rate attribute and a method to apply interest.
Sample Solution:
Java Code:
BankAccount.java
The above Java code defines a BankAccount class with three attributes: accountNumber, accountHolderName, and balance. It includes a constructor to initialize these attributes when an object of this class is created. The class also provides methods to:
- Deposit money (deposit): Adds a specified amount to the balance if the amount is positive.
- Withdraw money (withdraw): Subtracts a specified amount from the balance if the amount is positive and does not exceed the current balance.
- Check balance (checkBalance): Returns the current balance.
- Get account number (getAccountNumber): Returns the account number.
- Get the account holder's name (getAccountHolderName): Returns the account holder's name.
SavingsAccount.java
The above Java code defines a SavingsAccount class that extends the BankAccount class, inheriting its attributes and methods. The SavingsAccount class adds a new attribute, interestRate, which represents the interest rate for the savings account. The class includes:
- Constructor: Initializes the SavingsAccount object with the account number, account holder's name, initial balance, and interest rate by calling the superclass (BankAccount) constructor for the common attributes.
- Method to apply interest (applyInterest): Calculates interest based on the current balance and interest rate, then deposits interest into the account.
- Getter method for interest rate (getInterestRate): Returns the current interest rate.
- Setter method for interest rate (setInterestRate): Updates the interest rate if the provided rate is positive, ensuring it is a valid interest rate.
Main.java
The above Java code defines a Main class with a main method to test the BankAccount and SavingsAccount classes. The main method demonstrates the creation and usage of these classes:
- Create a BankAccount object: An instance of BankAccount is created with the account number "123456789", account holder's name "Henri Lionel", and an initial balance of 1000.0. The current balance is printed.
- Deposit money: 4000.0 is deposited into the BankAccount.
- Withdraw money: 3000.0 is withdrawn from the BankAccount.
- Check balance: The current balance is printed again.
- Create a SavingsAccount object: An instance of SavingsAccount is created with the account number "888888888", account holder's name "Amphitrite Jun", an initial balance of 2000.0, and an interest rate of 5.0.
- Apply interest: Interest is applied to the SavingsAccount, and the new balance is printed.
This code tests the functionality of depositing, withdrawing, and applying interest in bank accounts.
Sample Output:
Current balance: 1000.0 Deposited: 4000.0. New balance: 5000.0 Withdrew: 3000.0. New balance: 2000.0 Current balance: 2000.0 Deposited: 100.0. New balance: 2100.0 Interest applied: 100.0. New balance: 2100.0 Savings account balance: 2100.0
For more Practice: Solve these Related Problems:
- Write a Java program where the "BankAccount" class prevents withdrawals that would result in a negative balance.
- Write a Java program to implement a feature in the "SavingsAccount" class that automatically applies interest every month.
- Write a Java program where the "BankAccount" class tracks the largest deposit and withdrawal for each account.
- Write a Java program to modify the "BankAccount" class so that a joint account can have multiple owners.
Go to:
Java Code Editor:
Improve this sample solution and post your code through Disqus.
PREV : Java: Search, book, cancel hotel and flight reservations.
NEXT : Vehicle, Car, and Truck Classes in Java - Display Details.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.