Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class SavingAccount { private double annualInterestRate, balance, accumul

ID: 3828512 • Letter: P

Question

public class SavingAccount

{

  private double annualInterestRate, balance, accumulativeInterest,

          accumulativeDeposits = 0, accumulativeWithdrawals = 0;

  public SavingAccount()

  {

      balance = 0.0;

  }

  

  

  public SavingAccount(double balance)

  {

      this.balance = balance;

  }


  public void addMonthlyInterest()

  {

      accumulativeInterest += (balance * annualInterestRate / 12);

      balance += balance * (annualInterestRate / 12);

  }


  public double getAnnualInterestRate()

  {

      return annualInterestRate;

  }


  public double getBalance()

  {

      return balance;

  }


  public double getAccumulativeInterest()

  {

      return accumulativeInterest;

  }


  public double getAccumulativeDeposits()

  {

      return accumulativeDeposits;

  }

  public double getAccumulativeWithdrawals()

  {

      return accumulativeWithdrawals;

  }


  public void setAnnualInterestRate(double annualInterestRate)

  {

      this.annualInterestRate = annualInterestRate;

  }


  public void setBalance(double balance)

  {

      this.balance = balance;

  }


  public void setAccumulativeInterest(double accumulativeInterest)

  {

      this.accumulativeInterest = accumulativeInterest;

  }


  public void setAccumulativeDeposits(double accumulativeDeposits)

  {

      this.accumulativeDeposits = accumulativeDeposits;

  }


  public void setAccumulativeWithdrawals(double accumulativeWithdrawals)

  {

      this.accumulativeWithdrawals = accumulativeWithdrawals;

  }


  public void withdraw(double w)

  

  {

      balance -= w;

      accumulativeWithdrawals += w;

  }


  public void deposit(double d)

  {

      balance += d;

      accumulativeDeposits += d;

  }


  public double getMonthlyInterest()

  {

      return balance * (annualInterestRate / 12);

  }

}


















import java.io.*;

public class Assignment8

{


  public static void main(String[] args)

  {

      final int MONTH_PERIOD = 3;

      

      try

      {

          // create an InputStreamReader object to read from keyboard

          InputStreamReader ir = new InputStreamReader(System.in);

          // create BufferedReader object br

          BufferedReader br = new BufferedReader(ir);

          // create printwriter object to write data to the file result.txt

          PrintWriter pw = new PrintWriter(new File("result.txt"));

          double startingBalance, interestRate;

          System.out.print("Enter the starting balance on the account: $");

          startingBalance = Double.parseDouble(br.readLine());

          SavingAccount savingAccount = new SavingAccount(startingBalance);

          System.out.print("Enter the anual interest rate on the account: $");

          interestRate = Double.parseDouble(br.readLine());

          savingAccount.setAnnualInterestRate(interestRate);

          for (int count = 1; count <= MONTH_PERIOD; count++) {

           

              System.out.println(" MONTH " + count + "");

              

              // read the amount to deposit to balance

              System.out.printf("Total deposits for this month: $");

              savingAccount.deposit(Double.parseDouble(br.readLine()));

              // read the amount to withdraw from balance from the console

              System.out.printf("Total withdrawals for this month: $");

              savingAccount.withdraw(Double.parseDouble(br.readLine()));

              savingAccount.addMonthlyInterest();

          }

          

          System.out.println(" Quaterly Savings Account Statement");

          System.out.printf(" Starting balance: $ %.2f", startingBalance);

          System.out.printf(" Total deposits: + $%.2f",

                  savingAccount.getAccumulativeDeposits());

          System.out.printf(" Total withdrawals: - $%.2f",

                  savingAccount.getAccumulativeWithdrawals());

          System.out.printf(" Total interest: + $%.2f",

                  savingAccount.getAccumulativeInterest());

          System.out.println(" ----------");

          System.out.printf(" Ending balance: $%.2f",

                  savingAccount.getBalance());

          //writing data to file

          pw.write(" Starting balance: $ " + startingBalance);

          pw.write(" Total deposits: + $"

                  + savingAccount.getAccumulativeDeposits());

          pw.write(" Total withdrawals: - $"

                  + savingAccount.getAccumulativeWithdrawals());

          pw.write(" Total interest: + $"

                  + savingAccount.getAccumulativeInterest());

          pw.write(" ----------");

          pw.write(" Ending balance: $" + savingAccount.getBalance());

          pw.flush();

      }

      catch (Exception e)

      {

         

      }

  }

}



Using the test case:

5000
0.035
780.5
472.75
550.25
510
729
350.65

//////////////////////////////////////////////////////////////////////////////

The output is supposed to be:

Quarterly Savings Account Statement

Starting balance:    $   5000.00
Total deposits:    + $   2059.75
Total withdrawals: - $   1333.40
Total interest:    + $     46.86
                      ----------
Ending balance:      $   5773.21

////////////////////////////////////////////////////////////////////////////


But I’m getting

///////////////////////////////////////////////////////////////////////////

Quaterly Savings Account Statement

Starting balance:   $ 5000.00
Total deposits:       + $2059.75
Total withdrawals:   - $1333.40
Total interest:      + $47.92
         ----------

Ending balance:      $5774.27

//////////////////////////////////////////////////////////////////////

Can you help me fix the Total interest?

Explanation / Answer

Hi Friend, Code is fine.

It is working fine.

Interest should be : 47.92

I have printed more statics after each month, have a look :

Enter the starting balance on the account: $5000

Enter the anual interest rate on the account: $0.035

MONTH 1

Total deposits for this month: $780.5

Total withdrawals for this month: $472.75

Interest Earned in Month 1 : 15.480937500000001

After Month 1 accumulative interest: 15.480937500000001

After Month 1 balance : 5323.2309375

MONTH 2

Total deposits for this month: $550.25

Total withdrawals for this month: $510

Interest Earned in Month 2 : 15.643486067708336

After Month 2 accumulative interest: 31.124423567708337

After Month 2 balance : 5379.124423567709

MONTH 3

Total deposits for this month: $729

Total withdrawals for this month: $350.65

Interest Earned in Month 3 : 16.79263373540582

After Month 3 accumulative interest: 47.917057303114156

After Month 3 balance : 5774.267057303115

Quaterly Savings Account Statement

Starting balance:   $ 5000.00

Total deposits:       + $2059.75

Total withdrawals:   - $1333.40

Total interest:       + $47.92

           ----------

Ending balance:       $5774.27

Please let me know in case of any issue.