File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Chapter8 (Classes and Objects)/Interest Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ package Chapter8 .Interest ;
2
+
3
+ import java .math .BigDecimal ;
4
+ import java .text .NumberFormat ;
5
+
6
+ // Compound-interest calculations with BigDecimal.
7
+ public class Interest {
8
+ public static void main (String [] args ) {
9
+ // initial principal amount before interest
10
+ BigDecimal principle = BigDecimal .valueOf (1000.0 );
11
+ BigDecimal rate = BigDecimal .valueOf (0.05 ); // interest rate
12
+
13
+ // display headers
14
+ System .out .printf ("%s%20s%n" , "Year" , "Amount on deposit" );
15
+
16
+ // calculate amount on deposit for each of ten years
17
+ for (int year = 1 ; year <= 10 ; year ++) {
18
+ // calculate new amount for specified year
19
+ BigDecimal amount = principle .multiply (rate .add (BigDecimal .ONE ).pow (year ));
20
+
21
+ // display the year and the amount
22
+ System .out .printf ("%4d%20s%n" , year , NumberFormat .getCurrencyInstance ().format (amount ));
23
+ }
24
+ }
25
+
26
+ }
You can’t perform that action at this time.
0 commit comments