Design a BankAccount class that stores the account\'s annual interest rate and b
ID: 3711169 • Letter: D
Question
Design a BankAccount class that stores the account's annual interest rate and balance. the class constructor should accept the amount of the account's starting balance and Annual Interest Rate. The class should also have methods for subtracting the amount of withdrawal, add the amount of deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by 12. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance. ( this is compound interest ) Format output to 2 decimal places. Test the class in a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established, or prompt after each loop iteration to continue. Set up Accumulator variables: totalInterest, totalDeposits, totalWithdrawals Prompt for starting balance and annual Interest Rate Create a SavingsAccount object. Loop through each of the following steps for each set of Input Data: Prompt the user for the amount deposited into the account during the month. Use the class method to add this amount to account balance. Prompt the user for the amount withdrawn from the account during the month. Use the class method to subtract the amount from the balance. Use the class method to calculate the monthly interest. After the last monthly interest calculation, display the ending balance, the total of deposits, the total of withdrawals, and the total interest.
class BankAccount { private: double balance; // Account balance double annualInterestRate; public: BankAccount(double startBalance, double annualinterestrate); void deposit(double amount); void withdraw(double amount); double getBalance(); void setBalance(double b); double calculateInterest(); };
Explanation / Answer
import java.util.*;
public class SavingAccountTest
{public static void main(String[] args)
{double balance,interest,withdraw,deposit;
int months,i;
Scanner in=new Scanner(System.in);
SavingAccount account = new SavingAccount();
System.out.println("Enter the starting balance: ");
balance=in.nextDouble();
account.Balance(balance);
System.out.println("Enter the annual interest rate: ");
interest=in.nextDouble();
account.setInterestRate(interest);
System.out.println("Enter how many months the account has been open: ");
months=in.nextInt();
for(i=0;i<months;i++)
{System.out.println("Enter amount deposited month "+(i+1));
deposit=in.nextDouble();
account.deposit(deposit);
System.out.println("Enter amount to withdraw month "+(i+1));
withdraw=in.nextDouble();
account.withdraw(withdraw);
account.LastInterest();
}
System.out.printf("Total deposited $ %.2f ",account.getDeposits());
System.out.printf("Total withdrawn $ %.2f ",account. getWithdrawals());
System.out.printf("Interest earned $ %.2f ",account.getInterest());
System.out.printf("Ending balance $%.2f ",account.getBalance() );
}
}
--------------------------------------------
public class SavingAccount
{private double balance,annual,month,deposits,withdraws,earned;
public void Balance(double b)
{ balance=b;
}
public void deposit(double a)
{balance+=a;
deposits+=a;
}
public void withdraw(double a)
{ balance-=a;
withdraws+=a;
}
public void LastInterest()
{double interest,month;
month=annual/12./100.;
interest=month*balance;
balance+=interest;
earned+=interest;
}
public void setInterestRate(double i)
{ annual=i;
}
public double getBalance()
{return balance;
}
public double getDeposits()
{return deposits;
}
public double getWithdrawals()
{return withdraws;
}
public double getInterest()
{return earned;
}
}