Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Error in Lab 3

user1218779May 26 2018 — edited May 29 2018

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

Comments

Processing

Post Details

Added on May 26 2018
1 comment
207 views