1 Reply Latest reply on May 29, 2018 6:47 PM by NickR2600-Oracle

    Error in Lab 3

    user1218779

      In classe SavingsAccount, method earnInterest(),

      the interest is being computed twice in the "println" expression, using the updated balance, not the previous balance:

       

      public void earnInterest() {
             balance = balance*(1+ interestRate/12);
             System.out.println("Interest: $" +balance*(interestRate/12));
             printDetails();
      }

       

      So the printed balance is not the sum of the previous balance and the printed interest.

      The interest shown in the output is wrong!

       

      Account #0

      Withdraw: $100.0

      Savings Account #0

      Account Owner: Duke

      Balance: $5000.0

      Interest Rate: 0.02

       

      Interest: $8.347222222222223

      Savings Account #0

      Account Owner: Duke

      Balance: $5008.333333333334

      Interest Rate: 0.02

       

      I think this code should be something like:

       

      public void earnInterest() {
         

         double earnedInterest = balance * interestRate / 12;

         balance = balance + earnedInterest;

             System.out.println("Interest: $" + earnedInterest);
             printDetails();
      }

       

      or

      public void earnInterest() {
             System.out.println("Interest: $" + balance*(interestRate/12));
             balance = balance*(1+ interestRate/12);
         

         printDetails();

      }

       

      So the printed result would be

       

      Account #0

      Withdraw: $100.0

      Savings Account #0

      Account Owner: Duke

      Balance: $5000.0

      Interest Rate: 0.02

       

      Interest: $8.333333333333334

      Savings Account #0

      Account Owner: Duke

      Balance: $5008.333333333333

      Interest Rate: 0.02

       

      Thanks