Question
Aunt Agatha left you some cash. You decide to do the boring thing and deposit the money in a savings account. In this assignment, you'll
Aunt Agatha left you some cash. You decide to do the boring thing and deposit the money in a savings account. In this assignment, you'll write a program that calculates how much money would be in the account as it accrues interest year after year.
The program should take as input:
1. The amount of money Aunt Agatha left you, i.e., the starting balance in the account.
2. The annual interest rate.
3. The number of years to save.
The program should output the amount of money that you'd have in each of the years saved at the given annual rate should interest be compounded either yearly, monthly, or daily.
A sample run
What is the starting balance (in dollars)? 10000
What's the yearly interest rate (in percent)? 5 For how many years will you be saving? 30 Using: starting balance - 10000.0 yearly interest rate - 5.0 years to save - 30
| year | yearly | monthly | daily | | 1|$10500.00|$10511.62|$10512.67| | 2|$11025.00|$11049.41|$11051.63| | 3|$11576.25|$11614.72|$11618.22| | 4|$12155.06|$12208.95|$12213.86| | 5|$12762.82|$12833.59|$12840.03| | 6|$13400.96|$13490.18|$13498.31| | 7|$14071.00|$14180.36|$14190.34| | 8|$14774.55|$14905.85|$14917.84| | 9|$15513.28|$15668.47|$15682.64| | 10|$16288.95|$16470.09|$16486.65| | 11|$17103.39|$17312.74|$17331.88| | 12|$17958.56|$18198.49|$18220.44| | 13|$18856.49|$19129.56|$19154.56| | 14|$19799.32|$20108.26|$20136.56| | 15|$20789.28|$21137.04|$21168.91| | 16|$21828.75|$22218.45|$22254.19| | 17|$22920.18|$23355.19|$23395.11| | 18|$24066.19|$24550.08|$24594.52| | 19|$25269.50|$25806.11|$25855.41| | 20|$26532.98|$27126.40|$27180.96| | 21|$27859.63|$28514.24|$28574.46| | 22|$29252.61|$29973.08|$30039.40| | 23|$30715.24|$31506.56|$31579.44| | 24|$32251.00|$33118.50|$33198.44| | 25|$33863.55|$34812.90|$34900.44| | 26|$35556.73|$36594.00|$36689.70| | 27|$37334.56|$38466.22|$38570.69| | 28|$39201.29|$40434.22|$40548.11| | 29|$41161.36|$42502.91|$42626.91| | 30|$43219.42|$44677.44|$44812.29|
Calculating compound interest
If interest is compounded yearly, the amount of money that you'll have in the account will be the amount that you had last year plus the interest rate times last year's balance. For example, suppose that you had $100 last year, and the interest rate is 5 percent. The amount of interest that you've earned is 0.05*$100 = $5.
If interest is compounded monthly, you'll receive interest payments each month instead of each year. Remember, that the monthly interest rate is the annual rate divided by 12. So, using the previous example of an annual interest rate of 5%, if interest is compounded monthly, each month you'd receive an additional 0.05/12=0.004167 interest payment.
Similarly, if interest were to be compounded daily, each day, you'd add an additional fraction of 0.05/365 to your previous days' balance.
You may already know or can easily look up a formula for calculating compound interest, but you can also calculate the values in the table using the simple way that you learned in grade school along with some loops. At each step, think: the amount of money I have now is the same as the amount I had before, plus the interest I've earned in the meantime.
Formatted output
Though it's not required for the assignment, to format the output as it appears in the sample, you can use the Formatter (Links to an external site.)Links to an external site. class. To use it, you need to:
import java.util.Formatter;
The code used to print a single line in the table is:
System.out.format("|%6d|$%.2f|$%.2f|$%.2f| ", i, yearlyAmt, monthlyAmt, dailyAmt);
where i, yearlyAmt, monthlyAmt, and dailyAmt are the variables representing the number of years the money has accrued interest, and the balance after that many years has elapsed if interest was compounded yearly, monthly, or daily. If you decide that you'd like to use this, replace i, yearlyAmt, monthlyAmt, and dailyAmt with the variables that you've chosen to represent the year, yearly-, monthly-, and daily-compounded totals.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started