Hi, I\'m having a little problem with this program. Wanted to see if it is possi
ID: 3607450 • Letter: H
Question
Hi, I'm having a little problem with this program. Wanted to see if it is possible somebody can help me.
Create a class called BankAccount
Each object of the class contains a name, accountNumber, and savingsBalance field
Create getter/setter method for all your fields, using the "this" reference when referring to the fields
Also create a deposit and withdraw method, which takes a parameter and modifies the balance
Overload the BankAccount constructor (have at least 3), and use the "this" reference to call the constructor with the most parameters.
Use a static variable annualInterestRate to store the annual interest rate for all account holders
Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by the annualInterestRate divided by 12 - this interest should be added to the savingsBalance
Write a Driver class with a main method to test out your BankAccount class.
Create two BankAccount objects account1 and account2, with balances of $2000.00 and $3000.00
Set the annualInterestRate to 4%, then calculate the monthly interest for the next 12 months and print out the new balances for both account objects
Perform all calculations by using the BigDecimal's class
Explanation / Answer
class BankAccount{
String name;
int accountNumber;
double savingsBalance;
//Constructors with overload
public BankAccount(){System.out.println("BankAccount Obj created with no initial values");}
public BankAccount(String name){
this.name = name;
}
public BankAccount(String name, int accountNumber){
this.name = name;
this.accountNumber = accountNumber;
}
public BankAccount(String name, int accountNumber, double savingsBalance){
this.name = name;
this.accountNumber = accountNumber;
this.savingsBalance = savingsBalance;
}
//Getter methods
String getName(){ return this.name;};
int getAccountNumber(){return this.accountNumber;};
double getSavingsBalance(){return this.savingsBalance;};
//Deposit and Withdraw methods
void deposit(double amount){this.savingsBalance = this.savingsBalance + amount;};
void withdraw(double amount){this.savingsBalance = this.savingsBalance - amount;};
double calculateMonthlyInterest(int annualInterestRate){
double annualInterest = (this.savingsBalance * annualInterestRate)/100;
return annualInterest/12.0;
};
}
//Driver Class
public class DriverClass{
static int annualInterestRate = 4;
public static void main(String[] args){
BankAccount account1= new BankAccount("Sumanth",01234,2000.00);
BankAccount account2= new BankAccount("Srikanth",43210,3000.00);
double account1Interest = account1.calculateMonthlyInterest(annualInterestRate);
double account2Interest = account2.calculateMonthlyInterest(annualInterestRate);
account1.deposit(account1Interest*12); //for next 12 months balance
account2.deposit(account2Interest*12); //for next 12 months balance
System.out.println("Account1 Balance:::"+account1.getSavingsBalance());
System.out.println("Account2 Balance:::"+account2.getSavingsBalance());
}
}
Output::
---------
Account1 Balance:::2080.0
Account2 Balance:::3120.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.