Help with Pg 172 #2 interest (using a main routine and one method for
calculating interest on a given balance with a given interest rate)
Step #1:
Create a new class called InterestTable
Create a new method called calcInterestAmt
Use these comments:
// When Investment amount is 3000 and rate is .065 answer should be 195
// When Investment amount is 5 and rate is .065 answer should be .325
// When Investment amount is 3000.99 and rate is ..1 answer should be
300.099
The input parameters:
- Double Investment Amount (ex: 3000)
- Double Interest Rate (ex: .065)
The output parameter is:
Process
- return the investment amount * the interest rate
Compile and verify that this works with all the test cases in the
comments.
To verify, right click on the calcInterestAmt method
Step #2:
Add a new method in InterestTable called test to do the
following:
- Calculate the interest on 3000 at 6.5% interest by calling
calcInterestAmt and passing it 3000 and .065.
- Print that result.
- Calculate the interest on 5 at 6.5% interest by calling
calcInterstAmt
- Print that result.
- Calculate the interest on 3000.99 at 1% interest by calling
calcInterstAmt
- Print that result.
Step 3:
Add a new method in InterestTable called
printOneYearInterest.
Use these comments:
// When you receive in 1990, 2900.99, 100, .065
it should return
3195.099 and print :
// 1990 2900.99 100 195.099 3195.099
// When you receive in 2000, 5, 0, .065 it should return 5.325 and
print:
// 2000 5
0 .325 5.325
The input parameters:
- Int Year
- Double lastAmount
- Double newInvestmentAmount
The output parameter:
Process
- Calculate the interest on lastAmount +
newInvestmentAmount
at 6.5% by calling calcInterestAmt
- Print the following:
- The year (from the input parameter) and a
tab (\t)
- The lastAmount and a tab
- The newInvestmentAmount and a tab
- The interest you calculated above and a tab
- The total amount (which is the lastAmount
+
newInvestmentAmount + interest you calculated)
- Return the total amount
Run the test cases by right clicking on the printOneYearInterest method
Step 4:
Change the main method to print a table:
Add a print line to print the headings :
- year tab (\t) Original Amt \t New Investment \t
Interest \t End of Year
Total
Call the method printOneYearInterest giving it : 2007 as the year, 1000
as the last amount, 0
as the new investment, and capture the end of year amount
returned in a
variable called endAmt.
Loop 24 times always calling printOneYearInterest, giving it:
- one more than 2007 each time as the year
- the last end of year amount (starting with
endAmt from before the loop started, and then always using the one from
the last loop)
- 100 as the new investment
Test this by running the main routine. You should see a table with 25
lines.