This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,889,924 Users
  • 2,269,775 Discussions
  • 7,916,823 Comments

Discussions

Error in Lab 3

user1218779
user1218779 Member Posts: 1
edited May 29, 2018 2:47PM in Java Puzzle Ball

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

Answers