Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with java array printing the loan balance and interest paid of a mortgage.

ID: 3562602 • Letter: H

Question

Help with java array printing the loan balance and interest paid of a mortgage.

I'm new to java and I'm trying to display 3 mortgage loans with the principal of 200000 and then list the loan balance and paid for each month. So at the end of the month it should go down to zero.

Here's an example of one I did that doesnt include arrays that worked.

http://pastebin.com/0fvsNPL7

Using the principal 200000, rate of 5.75, and term of 30 years.

In the array program the rate and term is hardcoded onto the array but the principal isnt, however for this example it should be 200000 too. In this program it doesn't go all the way down to zero and can't figure out why. Any help would be appreciated.

Current program: http://pastebin.com/3TxbS4Q1

Explanation / Answer

/* OUTPUT
----jGRASP exec: java -ea Purchase

1   198031.88   891.67
2   196054.98   882.89
3   194069.27   874.08
4   192074.70   865.23
5   190071.24   856.33
6   188058.85   847.40
7   186037.49   838.43
8   184007.12   829.42
9   181967.69   820.37
10   179919.18   811.27
11   177861.52   802.14
12   175794.70   792.97
13   173718.66   783.75
14   171633.37   774.50
15   169538.77   765.20
16   167434.84   755.86
17   165321.53   746.48
18   163198.80   737.06
19   161066.61   727.59
20   158924.90   718.09
21   156773.65   708.54
22   154612.81   698.95
23   152442.34   689.32
24   150262.19   679.64
25   148072.31   669.92
26   145872.68   660.16
27   143663.24   650.35
28   141443.95   640.50
29   139214.76   630.60
30   136975.64   620.67
31   134726.53   610.68
32   132467.39   600.66
33   130198.19   590.58
34   127918.86   580.47
35   125629.38   570.30
36   123329.68   560.10
37   121019.74   549.84
38   118699.49   539.55
39   116368.91   529.20
40   114027.93   518.81
41   111676.51   508.37
42   109314.61   497.89
43   106942.18   487.36
44   104559.18   476.78
45   102165.54   466.16
46   99761.24   455.49
47   97346.22   444.77
48   94920.43   434.00
49   92483.83   423.19
50   90036.36   412.32
51   87577.98   401.41
52   85108.64   390.45
53   82628.30   379.44
54   80136.89   368.38
55   77634.38   357.28
56   75120.71   346.12
57   72595.83   334.91
58   70059.70   323.66
59   67512.25   312.35
60   64953.46   300.99
61   62383.25   289.58
62   59801.58   278.13
63   57208.41   266.62
64   54603.67   255.05
65   51987.32   243.44
66   49359.31   231.78
67   46719.58   220.06
68   44068.08   208.29
69   41404.76   196.47
70   38729.57   184.60
71   36042.45   172.67
72   33343.34   160.69
73   30632.21   148.66
74   27908.99   136.57
75   25173.62   124.43
76   22426.07   112.23
77   19666.26   99.98
78   16894.15   87.68
79   14109.68   75.32
80   11312.79   62.91
81   8503.44   50.44
82   5681.56   37.91
83   2847.10   25.33
84   -0.00   12.69

1   199282.50   916.67
2   198561.71   913.38
3   197837.62   910.07
4   197110.21   906.76
5   196379.46   903.42
6   195645.37   900.07
7   194907.91   896.71
8   194167.07   893.33
9   193422.84   889.93
10   192675.19   886.52
11   191924.12   883.09
12   191169.60   879.65
13   190411.63   876.19
14   189650.18   872.72
15   188885.25   869.23
16   188116.80   865.72
17   187344.84   862.20
18   186569.34   858.66
19   185790.28   855.11
20   185007.65   851.54
21   184221.43   847.95
22   183431.62   844.35
23   182638.18   840.73
24   181841.10   837.09
25   181040.37   833.44
26   180235.97   829.77
27   179427.89   826.08
28   178616.10   822.38
29   177800.59   818.66
30   176981.34   814.92
31   176158.34   811.16
32   175331.57   807.39
33   174501.00   803.60
34   173666.63   799.80
35   172828.44   795.97
36   171986.40   792.13
37   171140.50   788.27
38   170290.73   784.39
39   169437.06   780.50
40   168579.48   776.59
41   167717.97   772.66
42   166852.51   768.71
43   165983.09   764.74
44   165109.68   760.76
45   164232.26   756.75
46   163350.83   752.73
47   162465.35   748.69
48   161575.82   744.63
49   160682.20   740.56
50   159784.50   736.46
51   158882.68   732.35
52   157976.72   728.21
53   157066.62   724.06
54   156152.34   719.89
55   155233.87   715.70
56   154311.19   711.49
57   153384.28   707.26
58   152453.13   703.01
59   151517.70   698.74
60   150577.99   694.46
61   149633.97   690.15
62   148685.63   685.82
63   147732.94   681.48
64   146775.88   677.11
65   145814.44   672.72
66   144848.59   668.32
67   143878.31   663.89
68   142903.58   659.44
69   141924.39   654.97
70   140940.71   650.49
71   139952.52   645.98
72   138959.81   641.45
73   137962.54   636.90
74   136960.70   632.33
75   135954.27   627.74
76   134943.23   623.12
77   133927.55   618.49
78   132907.22   613.83
79   131882.21   609.16
80   130852.50   604.46
81   129818.07   599.74
82   128778.91   595.00
83   127734.98   590.24
84   126686.26   585.45
85   125632.74   580.65
86   124574.39   575.82
87   123511.19   570.97
88   122443.12   566.09
89   121370.15   561.20
90   120292.26   556.28
91   119209.43   551.34
92   118121.64   546.38
93   117028.87   541.39
94   115931.08   536.38
95   114828.26   531.35
96   113720.39   526.30
97   112607.45   521.22
98   111489.40   516.12
99   110366.22   510.99
100   109237.90   505.85
101   108104.41   500.67
102   106965.72   495.48
103   105821.81   490.26
104   104672.66   485.02
105   103518.24   479.75
106   102358.54   474.46
107   101193.51   469.14
108   100023.15   463.80
109   98847.42   458.44
110   97666.30   453.05
111   96479.78   447.64
112   95287.81   442.20
113   94090.38   436.74
114   92887.46   431.25
115   91679.02   425.73
116   90465.05   420.20
117   89245.52   414.63
118   88020.39   409.04
119   86789.65   403.43
120   85553.27   397.79
121   84311.22   392.12
122   83063.48   386.43
123   81810.02   380.71
124   80550.82   374.96
125   79285.84   369.19
126   78015.07   363.39
127   76738.47   357.57
128   75456.02   351.72
129   74167.70   345.84
130   72873.47   339.94
131   71573.30   334.00
132   70267.18   328.04
133   68955.07   322.06
134   67636.95   316.04
135   66312.78   310.00
136   64982.55   303.93
137   63646.22   297.84
138   62303.76   291.71
139   60955.16   285.56
140   59600.37   279.38
141   58239.37   273.17
142   56872.13   266.93
143   55498.63   260.66
144   54118.83   254.37
145   52732.71   248.04
146   51340.23   241.69
147   49941.38   235.31
148   48536.11   228.90
149   47124.40   222.46
150   45706.22   215.99
151   44281.54   209.49
152   42850.33   202.96
153   41412.56   196.40
154   39968.20   189.81
155   38517.22   183.19
156   37059.59   176.54
157   35595.28   169.86
158   34124.26   163.15
159   32646.49   156.40
160   31161.96   149.63
161   29670.61   142.83
162   28172.44   135.99
163   26667.39   129.12
164   25155.45   122.23
165   23636.58   115.30
166   22110.75   108.33
167   20577.92   101.34
168   19038.07   94.32
169   17491.16   87.26
170   15937.16   80.17
171   14376.04   73.05
172   12807.77   65.89
173   11232.30   58.70
174   9649.62   51.48
175   8059.68   44.23
176   6462.45   36.94
177   4857.90   29.62
178   3246.00   22.27
179   1626.71   14.88
180   0.00   7.46

1   199791.19   958.33
2   199581.37   957.33
3   199370.56   956.33
4   199158.73   955.32
5   198945.88   954.30
6   198732.02   953.28
7   198517.13   952.26
8   198301.22   951.23
9   198084.26   950.19
10   197866.27   949.15
11   197647.23   948.11
12   197427.15   947.06
13   197206.01   946.01
14   196983.81   944.95
15   196760.54   943.88
16   196536.21   942.81
17   196310.80   941.74
18   196084.31   940.66
19   195856.73   939.57
20   195628.07   938.48
21   195398.31   937.38
22   195167.44   936.28
23   194935.48   935.18
24   194702.40   934.07
25   194468.20   932.95
26   194232.88   931.83
27   193996.43   930.70
28   193758.85   929.57
29   193520.14   928.43
30   193280.27   927.28
31   193039.26   926.13
32   192797.10   924.98
33   192553.77   923.82
34   192309.28   922.65
35   192063.62   921.48
36   191816.77   920.30
37   191568.75   919.12
38   191319.54   917.93
39   191069.13   916.74
40   190817.53   915.54
41   190564.71   914.33
42   190310.69   913.12
43   190055.45   911.91
44   189798.99   910.68
45   189541.30   909.45
46   189282.37   908.22
47   189022.20   906.98
48   188760.79   905.73
49   188498.12   904.48
50   188234.19   903.22
51   187969.00   901.96
52   187702.54   900.68
53   187434.81   899.41
54   187165.78   898.13
55   186895.48   896.84
56   186623.87   895.54
57   186350.96   894.24
58   186076.75   892.93
59   185801.22   891.62
60   185524.37   890.30
61   185246.20   888.97
62   184966.69   887.64
63   184685.84   886.30
64   184403.65   884.95
65   184120.11   883.60
66   183835.20   882.24
67   183548.93   880.88
68   183261.29   879.51
69   182972.28   878.13
70   182681.87   876.74
71   182390.08   875.35
72   182096.88   873.95
73   181802.29   872.55
74   181506.28   871.14
75   181208.85   869.72
76   180909.99   868.29
77   180609.71   866.86
78   180307.98   865.42
79   180004.81   863.98
80   179700.19   862.52
81   179394.11   861.06
82   179086.56   859.60
83   178777.54   858.12
84   178467.03   856.64
85   178155.04   855.15
86   177841.56   853.66
87   177526.57   852.16
88   177210.07   850.65
89   176892.06   849.13
90   176572.52   847.61
91   176251.45   846.08
92   175928.84   844.54
93   175604.69   842.99
94   175278.98   841.44
95   174951.72   839.88
96   174622.88   838.31
97   174292.47   836.73
98   173960.48   835.15
99   173626.89   833.56
100   173291.71   831.96
101   172954.92   830.36
102   172616.51   828.74
103   172276.49   827.12
104   171934.83   825.49
105   171591.54   823.85
106   171246.61   822.21
107   170900.02   820.56
108   170551.77   818.90
109   170201.85   817.23
110   169850.25   815.55
111   169496.97   813.87
112   169142.00   812.17
113   168785.33   810.47
114   168426.95   808.76
115   168066.85   807.05
116   167705.02   805.32
117   167341.46   803.59
118   166976.16   801.84
119   166609.11   800.09
120   166240.30   798.34
121   165869.72   796.57
122   165497.37   794.79
123   165123.23   793.01
124   164747.30   791.22
125   164369.57   789.41
126   163990.03   787.60
127   163608.67   785.79
128   163225.48   783.96
129   162840.45   782.12
130   162453.59   780.28
131   162064.86   778.42
132   161674.28   776.56
133   161281.82   774.69
134   160887.49   772.81
135   160491.26   770.92
136   160093.13   769.02
137   159693.10   767.11
138   159291.15   765.20
139   158887.28   763.27
140   158481.46   761.33
141   158073.71   759.39
142   157664.00   757.44
143   157252.33   755.47
144   156838.68   753.50
145   156423.06   751.52
146   156005.44   749.53
147   155585.82   747.53
148   155164.19   745.52
149   154740.54   743.50
150   154314.86   741.47
151   153887.14   739.43
152   153457.37   737.38
153   153025.54   735.32
154   152591.64   733.25
155   152155.66   731.17
156   151717.59   729.08
157   151277.43   726.98
158   150835.15   724.87
159   150390.76   722.75
160   149944.24   720.62
161   149495.57   718.48
162   149044.76   716.33
163   148591.79   714.17
164   148136.64   712.00
165   147679.32   709.82
166   147219.81   707.63
167   146758.09   705.43
168   146294.16   703.22
169   145828.00   700.99
170   145359.62   698.76
171   144888.99   696.51
172   144416.10   694.26
173   143940.95   691.99
174   143463.52   689.72
175   142983.80   687.43
176   142501.79   685.13
177   142017.46   682.82
178   141530.82   680.50
179   141041.84   678.17
180   140550.52   675.83
181   140056.85   673.47
182   139560.81   671.11
183   139062.39   668.73
184   138561.59   666.34
185   138058.38   663.94
186   137552.76   661.53
187   137044.73   659.11
188   136534.25   656.67
189   136021.33   654.23
190   135505.96   651.77
191   134988.11   649.30
192   134467.78   646.82
193   133944.96   644.32
194   133419.64   641.82
195   132891.79   639.30
196   132361.42   636.77
197   131828.51   634.23
198   131293.04   631.68
199   130755.01   629.11
200   130214.39   626.53
201   129671.19   623.94
202   129125.39   621.34
203   128576.97   618.73
204   128025.92   616.10
205   127472.23   613.46
206   126915.89   610.80
207   126356.88   608.14
208   125795.20   605.46
209   125230.82   602.77
210   124663.74   600.06
211   124093.94   597.35
212   123521.41   594.62
213   122946.14   591.87
214   122368.11   589.12
215   121787.31   586.35
216   121203.73   583.56
217   120617.35   580.77
218   120028.17   577.96
219   119436.16   575.13
220   118841.31   572.30
221   118243.61   569.45
222   117643.05   566.58
223   117039.61   563.71
224   116433.28   560.81
225   115824.04   557.91
226   115211.89   554.99
227   114596.80   552.06
228   113978.76   549.11
229   113357.76   546.15
230   112733.79   543.17
231   112106.83   540.18
232   111476.86   537.18
233   110843.87   534.16
234   110207.86   531.13
235   109568.79   528.08
236   108926.66   525.02
237   108281.46   521.94
238   107633.16   518.85
239   106981.75   515.74
240   106327.23   512.62
241   105669.57   509.48
242   105008.76   506.33
243   104344.78   503.17
244   103677.62   499.99
245   103007.26   496.79
246   102333.69   493.58
247   101656.89   490.35
248   100976.85   487.11
249   100293.56   483.85
250   99606.98   480.57
251   98917.12   477.28
252   98223.95   473.98
253   97527.46   470.66
254   96827.64   467.32
255   96124.46   463.97
256   95417.91   460.60
257   94707.97   457.21
258   93994.64   453.81
259   93277.88   450.39
260   92557.69   446.96
261   91834.05   443.51
262   91106.95   440.04
263   90376.35   436.55
264   89642.26   433.05
265   88904.65   429.54
266   88163.51   426.00
267   87418.81   422.45
268   86670.55   418.88
269   85918.70   415.30
270   85163.25   411.69
271   84404.17   408.07
272   83641.47   404.44
273   82875.10   400.78
274   82105.07   397.11
275   81331.34   393.42
276   80553.91   389.71
277   79772.75   385.99
278   78987.85   382.24
279   78199.19   378.48
280   77406.74   374.70
281   76610.51   370.91
282   75810.45   367.09
283   75006.57   363.26
284   74198.83   359.41
285   73387.22   355.54
286   72571.72   351.65
287   71752.31   347.74
288   70928.98   343.81
289   70101.70   339.87
290   69270.46   335.90
291   68435.23   331.92
292   67596.01   327.92
293   66752.76   323.90
294   65905.47   319.86
295   65054.12   315.80
296   64198.69   311.72
297   63339.17   307.62
298   62475.52   303.50
299   61607.74   299.36
300   60735.80   295.20
301   59859.68   291.03
302   58979.36   286.83
303   58094.82   282.61
304   57206.05   278.37
305   56313.01   274.11
306   55415.70   269.83
307   54514.09   265.53
308   53608.16   261.21
309   52697.88   256.87
310   51783.25   252.51
311   50864.23   248.13
312   49940.81   243.72
313   49012.96   239.30
314   48080.67   234.85
315   47143.91   230.39
316   46202.66   225.90
317   45256.91   221.39
318   44306.62   216.86
319   43351.77   212.30
320   42392.35   207.73
321   41428.34   203.13
322   40459.70   198.51
323   39486.43   193.87
324   38508.49   189.21
325   37525.86   184.52
326   36538.53   179.81
327   35546.46   175.08
328   34549.64   170.33
329   33548.05   165.55
330   32541.65   160.75
331   31530.44   155.93
332   30514.37   151.08
333   29493.44   146.21
334   28467.62   141.32
335   27436.88   136.41
336   26401.20   131.47
337   25360.56   126.51
338   24314.94   121.52
339   23264.30   116.51
340   22208.63   111.47
341   21147.90   106.42
342   20082.09   101.33
343   19011.17   96.23
344   17935.12   91.10
345   16853.91   85.94
346   15767.53   80.76
347   14675.93   75.55
348   13579.11   70.32
349   12477.03   65.07
350   11369.67   59.79
351   10257.00   54.48
352   9139.01   49.15
353   8015.65   43.79
354   6886.91   38.41
355   5752.77   33.00
356   4613.19   27.57
357   3468.15   22.10
358   2317.62   16.62
359   1161.58   11.11
360   0.00   5.57


----jGRASP: operation complete.
*/

import java.text.DecimalFormat;//Allows Decimal Formatting
import javax.swing.*; //JoptionPane

public class Purchase {

    public static void main(String[] args) {
        double principal=0;
        int term[] = {7 ,15, 30};
        double rate[] = {5.35, 5.5, 5.75};
        double monthly_payment[] = {0, 0, 0};
      
        String principal_enter;
      
        principal_enter = JOptionPane.showInputDialog("Enter the principal amount");
        principal = Double.parseDouble(principal_enter);
      
        for (int i = 0; i < 3; ++i){
                    term[i]= term[i] * 12;
            rate[i] = rate[i]/1200;
       monthly_payment[i] = (principal*rate[i])/(1.0-Math.pow(rate[i] + 1, -term[i]));

            for ( int j = 1; j <= term[i]; ++j)
            {
                if(j%20==0){
                try{
                Thread.sleep(1);
                }//try
                catch(Exception e){
                }//catch
            }//if
                double interest_paid = (principal * rate[i]);
                double princ_paid = monthly_payment[i] - interest_paid;
                principal = principal - princ_paid;
                System.out.println(j + " " + round(principal) + " " + round(interest_paid));
            }
          
            System.out.println(" ");
            principal = Double.parseDouble(principal_enter);

        }      
    }
    public static String round(double num) //Method to round numbers
    {
        DecimalFormat round = new DecimalFormat("0.00"); //only two zeroes after
        return round.format(num);
      
    }//end of round method
  
}//end Mortgage_Array

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote