Use Simple and basic introductory ways to code this program. Thank you. Programm
ID: 3566176 • Letter: U
Question
Use Simple and basic introductory ways to code this program. Thank you.
Programming Challenge - Ch. 6: SavingsAccount Class
See pg 361: Programming Challenge- SavingsAccount Class:
Design a SavingsAccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by twelve. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance.
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. A loop should iterate once for every month, performing the following:
a. Ask the user for the amount deposited into the account during the month. Use the class method to add this amount to the account balance.
b. Ask the user for the amount withdrawn from the account during the month. Use the class method to subtract this amount from the account balance.
c. Use the class method to calculate the monthly interest.
After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.
Explanation / Answer
import java.util.Scanner;
class SavingsAccount {
private double interestRate, balance;
public SavingsAccount(double balance) {
this.balance = balance;
}
public void setInterestRate(double interestRate)
{
this.interestRate = interestRate;
}
public void subWithdrawal(double amountToWithdraw) {
this.balance = balance - amountToWithdraw;
}
public void addDeposit(double amountToDeposit){
this.balance = balance + amountToDeposit;
}
public void addMonthlyInterest(){
double monthlyInterestRate;
monthlyInterestRate = interestRate/1200;
double principle = balance;
double monthlyInterest = monthlyInterestRate*principle;
this.balance = principle + monthlyInterest;
}
public double getBalance(){
return balance;
}
}
class Driver {
public static void main(String[] args) {
double startingBalance, interestRate;
int numOfMonths;
System.out.println("Please enter the annual rate of interest.");
Scanner scanIt = new Scanner(System.in);
String curr1 = scanIt.next();
interestRate = Double.parseDouble(curr1);
System.out.println(interestRate);
System.out.println("Now, please enter the starting balance.");
String curr2 = scanIt.next();
startingBalance = Double.parseDouble(curr2);
System.out.println("Now, please enter the number of months that have passed since the account was created.");
String curr3 = scanIt.next();
numOfMonths = Integer.parseInt(curr3);
double deposits = 0, withdrawals = 0, interests = 0;
SavingsAccount sa = new SavingsAccount(startingBalance);
sa.setInterestRate(interestRate);
for(int i = 0; i<numOfMonths; i++)
{
System.out.println("Month number: "+i+" since the account was established.");
System.out.println("Please enter the amount deposited into the savings account during this month.");
String amt1 = scanIt.next();
double deposit = Double.parseDouble(amt1);
sa.addDeposit(deposit);
deposits = deposits + deposit;
System.out.println("Please enter the amount withdrawn from the savings account during this month.");
String amt2 = scanIt.next();
double withdrawn = Double.parseDouble(amt2);
withdrawals = withdrawals + withdrawn;
sa.subWithdrawal(withdrawn);
double beforeBalance = sa.getBalance();
sa.addMonthlyInterest();
interests = sa.getBalance() - beforeBalance;
}
scanIt.close();
System.out.println("The following is the current balance: "+sa.getBalance());
System.out.println("The following is the total amount deposited: "+deposits);
System.out.println("The following is the total amount withdrwan: "+withdrawals);
System.out.println("The following is the total interest earned: "+interests);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.