Asks you to: 1.) Write a Savings Account class that stores a savings account\'s
ID: 3623703 • Letter: A
Question
Asks you to:1.) Write a Savings Account 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 withdrawl,
adding the amount of a deposite, 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.
2.) Test the class (create a driver 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 then 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 subtrackt this amount from the account balance.
c. Use class method to calculate monthly interest.
I have created the SavingsAccount.java and the SavingsAccountTest.java (driver)
and the MAIN PROBLEM: WHEN I RUN IT (both compile fine) THE BALANCE IS NOT
BEING UPDATED BY THE DEPOSITS, WITHDRAWS, OR INTEREST - SO WHEN I DISPLAY THE
"ENDING BALANCE" IT DISPLAYS AS "0.00". The deposit and withdraw for each month
and then the totalDeposits and totalWithdrawn seems to be calculating correctly
too. I am not sure about the interest as this part really confuses me.
Please look at my program and notice the variable (I do have a problem managing
module level variables & fear I create to many variables than needed at times )
- please don't refer to them as different names - and keep in mind that this is
chapter 5 & we have not incorporated overloading methods, static fields or
methods or anything beyond that yet.
_______________________________________________________________________________
SavingsAccount.java:
/**
* SavingsAccount Class
* This class sets up the SavingsAccount class
*/
import java.util.Scanner; // Needed for Scanner
import java.io.*; // Needed for File and IOException
public class SavingsAccount
{
private double balance; // Account Balance
private double withdraw; // Account Withdrawals
private double deposit; // Account Deposits
private double interestRate; // Account annual InterestRate
private double interest; // Account monthly Interest
private double totalDeposits;
private double totalWithdrawn;
/**
* The constructor initializes the balance and interestRate fields
* with the values passed to startBalance and intRate. The interest
* field is assigned 0.0.
*/
public SavingsAccount (double startBalance,
double intRate)
{
balance = startBalance;
interestRate = intRate;
interest = 0.00;
}
// The getBalance method returns the value in the balance field.
public double getBalance()
{
return balance;
}
// The setDeposit method accepts the argument that is stored in
// the deposit field.
public void getDeposit(double depAmount)
{
deposit = depAmount;
}
// The addDeposit method add the deposit to the balance.
public void addDeposit()
{
balance = (balance + deposit);
}
// The setWithdraw method accepts the argument that is stored in the
// withdraw field.
public void setWithdraw(double withAmount)
{
withdraw = withAmount;
}
//The subtractWithdraw method subtracts the withdraw from the
// balance.
public void subtractWithdraw()
{
balance = (balance - withdraw);
}
// The getInterestRate method returns the value in the interestRate
// field.
public double getInterestRate()
{
return interestRate;
}
// The calculateInterest method calculates the monthly interest and adds
// it to the account balance.
public void calculateInterest()
{
interest = (interestRate / 12);
interest = (balance * interest);
}
// The addInterest method adds the moInterest to the
public void addInterest()
{
balance = (balance + interest);
}
// The getInterest method returns the value in the interest field.
public double getInterest()
{
return interest;
}
// The addTotalDeposits method adds all deposits for the months the account
// has been active.
public void addTotalDeposits()
{
totalDeposits = totalDeposits + deposit;
}
// The getTotalDeposits method returns the value in totalDeposits.
public double getTotalDeposits()
{
return totalDeposits;
}
// The addTotalWithrawn method adds all deposits for the months the account
// has been active.
public void addTotalWithdrawn()
{
totalWithdrawn = totalWithdrawn + withdraw;
}
// The getTotalWithdrawn method returns the value in totalWithdrawn.
public double getTotalWithdrawn()
{
return totalWithdrawn;
}
}
_______________________________________________________________________________
SavingsAccountTest.java (driver class)
/**
* SavingsAccountTest class
* This class sets up the driver class for the SavingsAccount.
*/
import java.util.Scanner; // Needed for Scanner
import java.io.*; // Needed for File and IOException
import java.text.DecimalFormat; // Needed to format the decimals in variable values.
public class SavingsAccountTest
{
public static void main(String[] args) throws IOException
{
SavingsAccount account;
double bal = 0.00;
double intRate = 0.00;
int months = 0;
double depAmount;
double withAmount;
double totalDeposits = 0.00;
double totalWithdrawn = 0.00;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Create a SavingsAccount object.
account = new SavingsAccount(bal, intRate);
// Create a Decimal Format object to set $ amouts display.
DecimalFormat dollar = new DecimalFormat("##,##0.00");
// Get the starting balance.
System.out.println("Enter the savings account's starting balance: ");
bal = keyboard.nextDouble();
// Get the annual interest rate.
System.out.println("Enter the savings account's annual interest rate: ");
intRate = keyboard.nextDouble();
account.getInterestRate();
// Get the number of month the savings account has been open.
System.out.println("How many months have passed since the account "
+ "was opened? :");
months = keyboard.nextInt();
// Process the information for the months.
for (int month = 1; month <= months; month++)
{
// Get the account's balance.
bal = account.getBalance();
// Get the amount of the deposit.
System.out.println("Enter the amount deposited during month "
+ month + ": ");
depAmount = keyboard.nextDouble();
account.addDeposit();
// Running total for deposits.
totalDeposits = (totalDeposits + depAmount);
// Get the amount of the withdraw.
System.out.println("Enter the amount withdrawn during month "
+ month + ": ");
withAmount = keyboard.nextDouble();
account.subtractWithdraw();
// Running total for withdraw.
totalWithdrawn = (totalWithdrawn + withAmount);
// Calculate the interest for the month.
account.calculateInterest();
account.addInterest();
}
// Display the total deposited.
account.addTotalDeposits();
account.getTotalDeposits();
System.out.println("Total deposited: $" + dollar.format(totalDeposits));
// Display the total withdrawn.
account.addTotalWithdrawn();
account.getTotalWithdrawn();
System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
// Display the ending balance.
System.out.println("Ending balance: $" + dollar.format(account.getBalance()));
}
}
____________________________________________________________________________________
Explanation / Answer
please rate - thanks
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.