Reputation: 23
I'm learning java from books and I've come across a logic problem, I know the code isn't as effective as it could be but I want to understand the problem so I can better understand how Java works and avoid more complex issues in the future.
The program I'm trying to write is supposed to read in account balance and an interest rate then give the balance after a year and two years.
The interest rate for the second year should be calculated on the total from the first year.
But my program is just adding the same amount of interest from the first year onto the second year. So in the case that the balance is 6000 and the interest is 4.25, I'm getting 6255.0 for the first year and 6510.0 for the second. I should be getting 6520.83 for the second year total cause the interest for the first year should be earning calculated interest too.
import acm.program.*;
public class BalanceAndIntrest extends ConsoleProgram {
public void run() {
println("This program calculates intrest.");
double balance = readDouble("Enter your balance here: ");
double intrest = readDouble("Enter your intrest rate here: ");
double yearsIntrest = (balance / 100) * intrest;
balance += yearsIntrest;
println("The balance after a year would be £" + balance +".");
balance += yearsIntrest;
println("The balance after two years would be £" + balance +".");
my logic here is that
it reads in the balance it reads in the interest years interest is defined by dividing the balance by 100 then multiplying that by the interest rate. then interest rate is added to balance then I add the interest rate again, which should give a different interest rate, seeing as the value of balance has changed at this point, but it doesn't, it just calculates interest on the number that is read in rather than the updated balance.
Why is this?
I thought that by the end of the program the value of balance should be the updated value so the years interest var should work.., but obviously I'm getting something wrong.
Upvotes: 2
Views: 99
Reputation: 4833
If your write a statement like
double yearsInterest = (balance / 100) * interest;
you don't define what interest means in a mathematical sense. What you actually do is to calculate the interest with the values currently referenced by balance
and interest
. If you want to define this simply add a method
private double calculateInterest(double balance, double interest) {
return (balance / 100) * interest;
}
and use it like this
balance += calculateInterest(balance, interest);
println("The balance after a year would be £" + balance +".");
balance += calculateInterest(balance, interest);
println("The balance after two years would be £" + balance +".");
Upvotes: 2
Reputation: 691685
you didn't recompute the interest based on the updated balance, after the first year:
double balance = readDouble("Enter your balance here: ");
double intrest = readDouble("Enter your intrest rate here: ");
double yearsIntrest = (balance / 100) * intrest;
balance += yearsIntrest;
println("The balance after a year would be £" + balance +".");
// Now the interest must be recomputed, since the balance has changed:
yearsIntrest = (balance / 100) * intrest;
balance += yearsIntrest;
println("The balance after two years would be £" + balance +".");
Upvotes: 0
Reputation: 23550
You need to re-calculate the interest for the second year e.g. like this:
println("This program calculates intrest.");
double balance = readDouble("Enter your balance here: ");
double intrest = readDouble("Enter your intrest rate here: ");
double firstYearIntrest = (balance / 100) * intrest;
balance += firstYearIntrest;
println("The balance after a year would be £" + balance +".");
double secondYearIntrest = (balance / 100) * intrest;
balance += secondYearIntrest;
println("The balance after two years would be £" + balance +".");
(Also for when you finished learning Java: later dont use float/double for money, always either use an arbitrary-precision decimal integer or the longest integer your language has and represent fractions of cent's)
Upvotes: 1