Java:Write a program that computes the monthly loan payment, given the loan amou
ID: 3784374 • Letter: J
Question
Java:Write a program that computes the monthly loan payment, given the loan amount (a
double value), the interest, as a percent % (a double value), and the number of years to
repay the loan (an int value). The calculation is to be performed in a method that is
called from main(). In main(), the calling method, you are to solicit the following values:
1. Loan amount, which is used for all calculations
2. The starting number of years to repay the loan, ending number of years to repay
the loan, and the incremental years.
3. The starting interest rate, as a %, the ending interest rate, as a %, and the
incremental interest rate, as a %.
You will the use for loops to create tables of monthly payments based on the number of
years to repay the loan, the table containing interest rate and the monthly loan payment.
The outer for loop will start with the starting number of years, continuing until the ending
year is reached or exceeded, incrementing then number of years each time through the
loop by the specified number of incremental year. For each year selected, display the
number of years, then follow with a table of monthly payments using interest rates
starting at the starting interest rate, continuing until the ending interest rate is met or
exceeded, incrementing the interest rate by the specified incremental interest. The table
will thus contain two labeled columns, interest rate and monthly payment. Refer to
Textbook Example Mortgage.java for soliciting User inputs and performing the monthly
payment calculation.
Sample Run for Problem 2
Enter the loan amount: 275000
Enter the starting number of years to repay the loan: 10
Enter the ending number of years to repay the loan: 20
Enter the years increment between tables: 5
Enter the starting loan yearly interest rate, %: 6.25
Enter the ending loan yearly interest rate, %: 7.25
Enter the increment interest rate, %: 0.5
Principle: $275000.0
Years to repay: 10
Interest Monthly
Rate Payment
-------- -------
6.25 3087.7
6.75 3157.66
7.25 3228.53
Principle: $275000.0
Years to repay: 15
Interest Monthly
Rate Payment
-------- -------
6.25 2357.91
6.75 2433.5
7.25 2510.37
Principle: $275000.0
Years to repay: 20
Interest Monthly
Rate Payment
-------- -------
6.25 2010.05
6.75 2091.0
7.25 2173.53
Explanation / Answer
Java Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CalculateInterest {
Double payment;
void LoanPayment(double amt,int start_year, int end_year, int inc_years, double start_interst, double end_interest, double inc_interest)
{
for( int i=start_year;i<=end_year;i=i+inc_years)
{
System.out.println("Principle:"+"$"+amt);
System.out.println("Years to repay:"+i);
System.out.println("Interest Monthly Rate"+" "+"Payment");
System.out.println("----------------"+" "+"------------");
for( Double y=start_interst;y<=end_interest;y=y+inc_interest)
{
payment=(amt*y)*((Math.pow((1+y), i))/((Math.pow((1+y), i))-1));
System.out.println(y+" "+payment);
}
}
}
public static void main(String args[])
{
CalculateInterest CI=new CalculateInterest();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type the loan amount:");
double Loan= Double.parseDouble(br.readLine());
System.out.println("Type the starting number of years to repay the loan:");
int start_years= Integer.parseInt(br.readLine());
System.out.println("Type the ending number of years to repay the loan:");
int end_years= Integer.parseInt(br.readLine());
System.out.println("Type the years increment between tables:");
int inc_years= Integer.parseInt(br.readLine());
System.out.println("Type the starting loan yearly interest rate, %:");
double start_interest= Double.parseDouble(br.readLine());
System.out.println("Type the ending loan yearly interest rate, %:");
double end_interest= Double.parseDouble(br.readLine());
System.out.println("Type the increment interest rate, %:");
double inc_interest= Double.parseDouble(br.readLine());
CI.LoanPayment(Loan,start_years,end_years,inc_years,start_interest,end_interest,inc_interest);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.