|
| 1 | +import java.text.DecimalFormat; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class CompoundInterest { |
| 5 | +/* |
| 6 | + When a bank account pays compound interest, it pays interest not only on the principal amount that was deposited |
| 7 | + into the account, but also on the interest that has accumulated over time. Suppose you want to deposit some money |
| 8 | + into a savings account, and let the account earn compound interest for a certain number of years. The formula for |
| 9 | + calculating the balance of the account after a specified number of years is: |
| 10 | +
|
| 11 | + A=P(1+rn)nt |
| 12 | +
|
| 13 | + The terms in the formula are: |
| 14 | +
|
| 15 | + A is the amount of money in the account after the specified number of years. |
| 16 | + P is the principal amount that was originally deposited into the account. |
| 17 | + r is the annual interest rate. |
| 18 | + n is the number of times per year that the interest is compounded. |
| 19 | + t is the specified number of years. |
| 20 | +
|
| 21 | + Write a program that makes the calculation for you. The program should ask the user to input the following: |
| 22 | +
|
| 23 | + - The amount of principal originally deposited into the account |
| 24 | + - The annual interest rate paid by the account |
| 25 | + - The number of times per year that the interest is compounded |
| 26 | + (For example, if interest is compounded monthly, enter 12. If interest is compounded quarterly, enter 4.) |
| 27 | + - The number of years the account will be left to earn interest |
| 28 | +
|
| 29 | + Once the input data has been entered, the program should calculate and display the amount of money that will be in |
| 30 | + the account after the specified number of years. |
| 31 | +
|
| 32 | + Note: The user should enter the interest rate as a percentage. For example, 2 percent would be entered as 2, |
| 33 | + not as .02. The program will then have to divide the input by 100 to move the decimal point to the correct position. |
| 34 | +*/ |
| 35 | + private double principalAmount; |
| 36 | + private double annualInterestRate; |
| 37 | + private double numberOfCompoundedTimes; |
| 38 | + private double numberOfYears; |
| 39 | + |
| 40 | + private static final Scanner input = new Scanner(System.in); |
| 41 | + private static final DecimalFormat format = new DecimalFormat("#,###.##"); |
| 42 | + |
| 43 | + public double calculateCompoundInterest() { |
| 44 | + double A; |
| 45 | + double P = getPrincipalAmount(); |
| 46 | + double r = getAnnualInterestRate() / 100; |
| 47 | + double n = getNumberOfCompoundedTimes(); |
| 48 | + double t = getNumberOfYears(); |
| 49 | + |
| 50 | + A = P * (1 + r * n) * n * t; |
| 51 | + |
| 52 | + return A; |
| 53 | + } |
| 54 | + |
| 55 | + public double getNumberOfYears() { |
| 56 | + return numberOfYears; |
| 57 | + } |
| 58 | + |
| 59 | + public double getNumberOfCompoundedTimes() { |
| 60 | + return numberOfCompoundedTimes; |
| 61 | + } |
| 62 | + |
| 63 | + public double getAnnualInterestRate() { |
| 64 | + return annualInterestRate; |
| 65 | + } |
| 66 | + |
| 67 | + public double getPrincipalAmount() { |
| 68 | + return principalAmount; |
| 69 | + } |
| 70 | + |
| 71 | + public void setPrincipalAmount(double inputPrincipalAmount) { |
| 72 | + principalAmount = inputPrincipalAmount; |
| 73 | + } |
| 74 | + |
| 75 | + public void setAnnualInterestRate(double inputAnnualInterestRate) { |
| 76 | + annualInterestRate = inputAnnualInterestRate; |
| 77 | + } |
| 78 | + |
| 79 | + public void setNumberOfCompoundedTimes(double inputNumberOfCTimes) { |
| 80 | + numberOfCompoundedTimes = inputNumberOfCTimes; |
| 81 | + } |
| 82 | + |
| 83 | + public void setNumberOfYears(double inputNumberOfYears) { |
| 84 | + numberOfYears = inputNumberOfYears; |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) { |
| 88 | + CompoundInterest compoundInterest = new CompoundInterest(); |
| 89 | + |
| 90 | + System.out.println( "Input the amount of principal originally deposited into the account. Ex. 10000"); |
| 91 | + compoundInterest.setPrincipalAmount(input.nextDouble()); |
| 92 | + |
| 93 | + System.out.println( "Input the annual interest rate paid by the account. Ex. 2 for 2%"); |
| 94 | + compoundInterest.setAnnualInterestRate(input.nextDouble()); |
| 95 | + |
| 96 | + System.out.println( "Input the number of times per year that the interest is compounded. Monthly = 12. " + |
| 97 | + "Quarterly = 4."); |
| 98 | + compoundInterest.setNumberOfCompoundedTimes(input.nextDouble()); |
| 99 | + |
| 100 | + System.out.println( "Input the number of years the account will be left to earn interest. Ex. 35 for 35 years"); |
| 101 | + compoundInterest.setNumberOfYears(input.nextDouble()); |
| 102 | + |
| 103 | + double estimatedAmount = compoundInterest.calculateCompoundInterest(); |
| 104 | + |
| 105 | + System.out.println( "In " + compoundInterest.getNumberOfYears() + " years you will have a estimated " + |
| 106 | + "account balance of $" + format.format(estimatedAmount) + "."); |
| 107 | + } |
| 108 | +} |
0 commit comments