Problem 1 (6 pts) he Suppose that you have a class named BankAccount that repres
ID: 3919334 • Letter: P
Question
Problem 1 (6 pts) he Suppose that you have a class named BankAccount that represents (surprise!) a bank account. T BankAccount class has two instance variables: . private double balance prtbate double annualInterestRate The BankAccount class also has the following methods. (Note that no code has been provided for the method thod headers.) bodies, but you should be able to determine what the methods do from the descriptions and me public BankAccount(double b, double ir) A constructor that sets the instance variables to the passed-in values. public double checkBalance0 Returns the current balance of this BankAccount object. public double deposit (double amt) Updates the balance by depositing amt into the account. Returns the new balance. e public double withdraw(double amt) Updates the balance by withdrawing amt from the account. Returns the new balance. public double payInterest0 Updates the balance by making a single annual interest payment based on the current balance. Returns the new balance. n paper or in document, write the code required for the following actions (1 pt each). You do NOT ave to write any code for the BankAccount class. Assume that it already exists as described above. a separate 1. Declare and instantiate a BankAccount object named acct1, with an initial balance of $0 and an annual interest rate of 0.5%. 2. Declare and instantiate a BankAccount object named acct2, with an initial balance of $500 and an annual interest rate of 1.1% 3. Display the current balances of both accti and acct2 on the screen. 4. Deposit $120 to acct1 and display the new balance on the screen. 5. Withdraw $80 from acct2 and display the new balance on the screen. 6. Make an annual interest payment to both acctl and acct2, and display the new balances of each on the screen.Explanation / Answer
The following are the answers for the 6 questions.
//1. declare and instantiate acct1
BankAccount acct1 = new BankAccount(0, 0.5);
//2. declare and instantiate acct1
BankAccount acct2 = new BankAccount(500, 1.1);
//3. display balances for acct1 and acct2
System.out.printf("Balance for acct1 = $%.2f ", acct1.checkBalance());
System.out.printf("Balance for acct2 = $%.2f ", acct2.checkBalance());
//4. deposit 120 to acct1
acct1.deposit(120);
//5. withdraw 80 from acct2
acct2.withdraw(80);
//6. make annual interest payment and display newbalances
System.out.printf("Balance for acct1 = $%.2f ", acct1.payInterest());
System.out.printf("Balance for acct2 = $%.2f ", acct2.payInterest());
=====================================
Given below is the complete code ... you dont need to write this over there... For your reference
BankAccount.java
-=---------------
public class BankAccount{
private double balance;
private double annualInterestRate;
public BankAccount(double b, double ir){
balance = b;
annualInterestRate = ir;
}
public double checkBalance(){
return balance;
}
public double deposit(double amt){
balance += amt;
return balance;
}
public double withdraw(double amt){
balance -= amt;
return balance;
}
public double payInterest(){
double interest = balance * annualInterestRate /100;
balance += interest;
return balance;
}
}
TestBankAccount.java
----------
public class TestBankAccount {
public static void main(String[] args) {
//1. declare and instantiate acct1
BankAccount acct1 = new BankAccount(0, 0.5);
//2. declare and instantiate acct1
BankAccount acct2 = new BankAccount(500, 1.1);
//3. display balances for acct1 and acct2
System.out.printf("Balance for acct1 = $%.2f ", acct1.checkBalance());
System.out.printf("Balance for acct2 = $%.2f ", acct2.checkBalance());
//4. deposit 120 to acct1
acct1.deposit(120);
//5. withdraw 80 from acct2
acct2.withdraw(80);
//6. make annual interest payment and display newbalances
System.out.printf("Balance for acct1 = $%.2f ", acct1.payInterest());
System.out.printf("Balance for acct2 = $%.2f ", acct2.payInterest());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.