Write this program in JAVA: 1 . Allow the user to input the followings: a. Month
ID: 3788141 • Letter: W
Question
Write this program in JAVA:
1. Allow the user to input the followings:
a. Monthly Interest Rate
b. Loan Amount
c. Monthly Payment
2. Include the following methods:
a. readInput(): will ask the user to input interest rate, loan amount, and monthly payment and stores them in the following member variables interestRate, loanAmount, and monthlyPayment, respectively.
b. computeInterest(): this method will perform the required computation to find the number of months and the total amount of interest paid. This method should store the number of months in a member variable called months and store the total collected interest in member variable called totatInterest.
c. Display(): this method will output the number of months and total interest paid over those months as shown in example 1.
NOTE: In case the payment amount is less than the interest you should display a message as shown in example 2.
Example 1:
Example 2:
Enter Loan Amount: 1000 Enter monthly payment 50 Enter interest rate: 0.012 It will take you 25.0 months to pay off your 1000.0 debt. You will have paid $150.34 in interest.Explanation / Answer
import java.util.Scanner;
public class InterestCalculator {
private static double interestRate;
private static double loanAmount;
private static double monthlyPayment;
private static double totalinterest = 0;
private static double months = 1;
public static void readInput()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter loan amount: ");
loanAmount = sc.nextDouble();
System.out.println("Enter monthly payment: ");
monthlyPayment = sc.nextDouble();
System.out.println("Enter interest rate: ");
interestRate = sc.nextDouble();
sc.close();
}
public static void computeInterest()
{
double loan = loanAmount;
while(loan > 0)
{
double interest = loan*interestRate;
totalinterest += interest;
months++;
loan = loan + interest - monthlyPayment;
}
}
public static void display()
{
System.out.printf("It will take you %.1f to pay off your %.1f " +
" debt. You will have paid $%.2f in interest. ",
months, loanAmount, totalinterest);
}
public static void main(String[] args)
{
readInput();
if (monthlyPayment < loanAmount*interestRate)
{
System.out.println("Monthly payment must be more than the interest!");
return;
}
computeInterest();
display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.