Question
Objectives: More practice designing and writing classes Use a class constant Use a Java for loop structure Write boolean expression(s) Background Most Americans will have
Objectives:
- More practice designing and writing classes
- Use a class constant
- Use a Java for loop structure
- Write boolean expression(s)
Background
Most Americans will have one or more loans during their lifetime. A loan has the following parameters:
- An initial balance
- An interest rate
- A length to pay the loan back
- The number of times in a year to compound the interest
Before taking out a loan (for a car, school or a home), it is wise to know more about what you're getting into. Some things to know are:
- How much is each payment?
- How much of my first payment goes towards the balance?
- How much of each payment goes towards the balance?
- What is the balance after each payment?
- What is the total amount of interest that I'll have to pay?
These questions can be answered by performing some math calculations in a computer program. First, the formula for calculating a monthly payment is:
J M = P * (J + --------------) ( 1 + J)N*T - 1
or
M = P * (J + (J / ((1+J)N*T - 1)))
where: P is the initial amount of the loan R is the annual interest rate N is the number of compounding periods in a year (e.g., 12 for monthly) J is the monthly interest rate (= R / N) T is the number of years To calculate the interest for a given month, multiply the monthly interest rate by the current balance (J * balance). Whatever is above and beyond the interest goes to paying down the balance. This is called the principal. To calculate the principal for a given month, subtract the monthly interest from the monthly payment. Use this amount to update the balance for that month.
Assignment
For this project, you get to design and write a class named Main that stores loan terms and has methods that use those terms. Your Main class should store the following loan terms:
- Initial Balance
- Interest Rate (the Annual Percentage Rate, stored as a rate not a percentage, for example 5% is 0.05)
- Years (the life of the loan)
Additionally, your class should store the following class constants (following standard coding convention of capitalizing the letters):
- The number of periods per year
- An invalid dollar amount (set to -1.0)
Additionally, store the following calculated loan values:
- Required monthly payment (see M above)
- Total Interest amount
- Total Payment amount
- First month's interest
- First month's principal
- First month's balance
- Last month's interest
- Last month's principal
- Last month's balance
Provide the following methods in your class:
- Constructor that take the initial balance (as a double), the interest rate (as a double) and the number of years (as a double)
- getMonthlyPayment() returns the required monthly payment (rounded to 2 decimal places) as a double
- getAmount() takes two Strings as arguments. The first is the month and can have the following values:
- "first" (meaning the first month)
- "last" (meaning the last month)
- "interest"
- "principal"
- "balance"
- getTotalInterest() returns the total amount of interest paid (rounded to 2 decimal places) as a double
- getTotalPayments() returns the total amount of payments paid (rounded to 2 decimal places) as a double
You must use a for loop to calculate the monthly interest, principal and balance amounts. To simply things for this project, store each item as a double and use the full precision value in each calculation. Only round when displaying the value or returning it to code outside of the class.
Example
Let's assume that you're trying to buy a $10,000 car and you're looking at a 2 year loan at 5% annual interest. So, P = $10,000.00 R = 0.05 (notice, it's a rate, not a percentage) J = (0.05) / 12 = 0.0042 N = 12 payments / year T = 2 years The monthly payment is the same each month for the entire life of the loan. It is calculated as: M = $10,000 * (0.0042 + (0.0042 / ((1+0.0042)(2*12) - 1))) = $438.71389734068595 The amount of interest charged each month changes and is calculated as J times the current balance. The principal amount also changes each month because it is the monthly payment amount minus the interest for that month. The balance for that month is then updated to be the previous month's balance minus the amount of principal paid in the current month. So, for our auto loan, the interest for the first month is J * $10,000.00 = $41.666666666666664. Therefore, the principal is $438.71389734068595 - $41.666666666666664 = $397.04723067401926 and giving us a new balance of $10,000.00 - $397.04723067401926 = $9,602.95276932598. The interest for the second month is J * $9,602.95276932598 = $40.012303205524915, the principal is then $438.71389734068595 - $40.012303205524915 = $398.70159413516103 and giving us a new balance of $9,602.95276932598 - $398.70159413516103 = $9,204.25117519082. Given the parameters above, a full loan table (with values rounded to two decimal places):
Loan Information Initial Balance: $10,000.00 APR: 0.05 (5%) Years: 2.0 Monthly Payment: $438.71 Month Int. Princ. Balance 0 $10,000.00 1 $41.67 $397.05 $9,602.95 2 $40.01 $398.70 $9,204.25 3 $38.35 $400.36 $8,803.89 4 $36.68 $402.03 $8,401.86 5 $35.01 $403.71 $7,998.15 6 $33.33 $405.39 $7,592.76 7 $31.64 $407.08 $7,185.69 8 $29.94 $408.77 $6,776.91 9 $28.24 $410.48 $6,366.44 10 $26.53 $412.19 $5,954.25 11 $24.81 $413.90 $5,540.34 12 $23.08 $415.63 $5,124.71 13 $21.35 $417.36 $4,707.35 14 $19.61 $419.10 $4,288.25 15 $17.87 $420.85 $3,867.41 16 $16.11 $422.60 $3,444.81 17 $14.35 $424.36 $3,020.45 18 $12.59 $426.13 $2,594.32 19 $10.81 $427.90 $2,166.41 20 $9.03 $429.69 $1,736.73 21 $7.24 $431.48 $1,305.25 22 $5.44 $433.28 $871.97 23 $3.63 $435.08 $436.89 24 $1.82 $436.89 $0.00 Total Interest: $529.13
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