Calculate interest rate using 2D and 1D arrays

Viewed 97

I am new in Java and I am supposed to calculate the interest rate development of 1000 € in 10 years with an increasing percentage (3%, 4%, 5%, 6%, 7%). I have many troubles figuring out how to make the calculation between the two arrays. The real difficulty is to put the 1000 € in the first [0] line of the bi-dimensional array (year 0) and to add the rate every year on the next line of the array. This is was I was able to do until now but now I am completely stuck:

public class zinsenEntwicklung {
    /**
     * @param args
     */
    public static void main(String[] args) {
        final int N1 = 11;
        final int N2 = 5;
        double anfang = 1000;
        double zinsenTable[] = {0.03, 0.04, 0.05, 0.06, 0.07};
        double b[][] = new double[N1][N2];
        System.out.println("Jahr");
        for (int i = 0; i < 11; i++) {
            System.out.print(i + "\t");
            for (int j = 0; j < zinsenTable.length; j++) {
                for (int k = 0; k < i; k++) {
                    b[i][j] = (anfang * zinsenTable[j]) + anfang;
                }
                System.out.print(b[i][j] + "\t");
            }
            System.out.print("\n");
        }
    }
}

What I should get:


Jahr   3%        4%        5%         6%       7%
0   1.000,00  1.000,00  1.000,00  1.000,00  1.000,00 
1   1.030,00  1.040,00  1.050,00  1.060,00  1.070,00
2   1.060,90  1.081,60  1.102,50  1.123,60  1.144,90
...
10  1.343,92  1.480,24  1.628,89  1.790,85  1.967,15

What I am getting:

Jahr
0   0.0 0.0 0.0 0.0 0.0 
1   1030.0  1040.0  1050.0  1060.0  1070.0  
2   1030.0  1040.0  1050.0  1060.0  1070.0  
3   1030.0  1040.0  1050.0  1060.0  1070.0  
4   1030.0  1040.0  1050.0  1060.0  1070.0  
5   1030.0  1040.0  1050.0  1060.0  1070.0  
6   1030.0  1040.0  1050.0  1060.0  1070.0  
7   1030.0  1040.0  1050.0  1060.0  1070.0  
8   1030.0  1040.0  1050.0  1060.0  1070.0  
9   1030.0  1040.0  1050.0  1060.0  1070.0  
10  1030.0  1040.0  1050.0  1060.0  1070.0  

I would be really thankful for every suggestion on what I am doing wrong.

2 Answers

You need to add the interest to the value of the last year. Also I added in the first year the anfang value.

public class ZinsenEntwicklung {
    /**
     * @param args
     */
    public static void main(String[] args) {
        final int N1 = 11;
        final int N2 = 5;
        double anfang = 1000;
        double zinsenTable[] = {0.03, 0.04, 0.05, 0.06, 0.07};
        double b[][] = new double[N1][N2];
        for (int i = 0; i < b[0].length; i++) {
            b[0][i] = anfang;
        }
        System.out.println("Jahr");
        for (int i = 1; i < 11; i++) {
            System.out.print(i + "\t");
            for (int j = 0; j < zinsenTable.length; j++) {
                for (int k = 0; k < i; k++) {
                    b[i][j] = (b[i - 1][j] * zinsenTable[j]) + b[i - 1][j];
                }
                System.out.print(b[i][j] + "\t");
            }
            System.out.print("\n");
        }
    }
}

Running this gave me following output :

Jahr
1       1030.0  1040.0  1050.0  1060.0  1070.0
2       1060.9  1081.6  1102.5  1123.6  1144.9
3       1092.727        1124.8639999999998      1157.625        1191.0159999999998      1225.0430000000001
4       1125.50881      1169.85856      1215.50625      1262.4769599999997      1310.79601
5       1159.2740743    1216.6529024    1276.2815624999998      1338.2255775999997      1402.5517307
6       1194.052296529  1265.319018496  1340.0956406249998      1418.5191122559997      1500.730351849
7       1229.87386542487        1315.93177923584        1407.1004226562497      1503.6302589913598      1605.78147647843
8       1266.770081387616       1368.5690504052736      1477.4554437890622      1593.8480745308414      1718.18617983192
9       1304.7731838292445      1423.3118124214845      1551.3282159785153      1689.478959002692       1838.4592124201545
10      1343.9163793441219      1480.244284918344       1628.894626777441       1790.8476965428533      1967.1513572895653

I would you use a formatted String for the output, will look nice and you don´t end up with many decimal place.

Btw. class names in java are written with an capital at the beginning.

In your code, you're always multiplying the same values:

b[i][j] = (anfang * zinsenTable[j]) + anfang;

That's why you're getting the same value on every row, what you need to do, is to set the first line of your matrix to 1000 (whatever anfang means) and then the upcoming values calculate based on previous row.

Like this:

for (int i = 0; i < N1; i++) {
    System.out.print(i + "\t");
    for (int j = 0; j < N2; j++) {
        if (i == 0) {
            b[i][j] = anfang;
        } else {
            b[i][j] = (b[i - 1][j] * zinsenTable[j]) + b[i - 1][j];
        }
        System.out.print(b[i][j] + "\t");
    }
    System.out.print("\n");
}

Note how on the first run all rows are initialized to anfang

if (i == 0) {
    b[i][j] = anfang;
}

And then all the following rows are calculated based on the previous row * percentage

b[i][j] = (b[i - 1][j] * zinsenTable[j]) + b[i - 1][j];

Just running this code, you will see a lot of decimal places displayed, if you want to limit this to 2 (as in your example) you can use System.out.printf

System.out.printf("%.2f" + "\t", b[i][j]);

An additional hint:

You're using Double to calculate currency, which is a known bad practice due to floating point precission. The accepted way to do so is using BigDecimal

Related