I'm making a method that are supposed to calculate the interest rate of an amount over a certain period of years (these values are already defined in the parameters). This is the code i have so far:
public void balance(int amount, double rate, int year){
double yearlyRate = amount * rate;
double totalAmount;
System.out.print(amount + " :- " + " grows with the interest rate of " + rate);
for (int i = 0; i <= year; i++ ){
for ( int j = amount; j)
totalAmount = amount + yearlyRate;
System.out.println(i + " " + totalAmount);
}
}
I was on my way to make the nested for-loop as you can see there where there is missing code. Here i ran in too some trouble. The first for-loop runs through the years and the other are supposed to calculate the total amount. To be clear the years that are defined in the variable "int year" let's say that it is 7, then the program are supposed to calculate the growth of the amount each year so:
year1 totalAmount
year2 totalAmount
year3 totalAmount
and so on.....
The main method looks like this:
public void exerciceG(Prog1 prog1) {
System.out.println("TEST OF: balance");
prog1.balance(1000, 0.04, 7);
}
I appreciate any help i can get!
Here's one change to make, but there may be a lot else to do as I mentioned in comment:
totalAmount = totalAmount + amount + yearlyRate;
which could be written:
totalAmount += amount + yearlyRate;
You also might want to drop the for j
loop since it doesn't do anything as is.
EDIT
This is a guess since I'm not sure we know what the objective is, but how about:
public static void balance(int amount, double rate, int year){
double yearlyInterestPaid ;
double totalAmount = amount;
System.out.println(amount + " :- " + " grows with the interest rate of " + rate);
for (int i = 0; i <= year; i++ ){
yearlyInterestPaid = totalAmount * rate;
totalAmount += yearlyInterestPaid;
System.out.println(i + " " + totalAmount);
}
}
This is the output:
TEST OF: balance
1000 :- grows with the interest rate of 0.04
0 1040.0
1 1081.6
2 1124.8639999999998
3 1169.85856
4 1216.6529024
5 1265.319018496
6 1315.93177923584
7 1368.5690504052736
It's reasonable to assume that this is the objective.