LoanCalculation

Updated Dani 1 Tallied Votes 195 Views Share

A program similar to ComputeMortgage.java utilizing a mathematical formula and multiple methods to calculate a loan payment.

package loancalculation;
public class LoanCalculation
{
    // Main method
    public static void main(String[] args)
    {
        double annualInterestRate;
        int numOfYears;
        double loanAmount;
        // Enter monthly interest rate
        System.out.println(
        "Enter yearly interest rate, for example 8.25: ");
        annualInterestRate = MyInput.readDouble();
        // Obtain monthly interest rate
        double monthlyInterestRate = annualInterestRate/1200;
        // Enter number of years
        System.out.println(
        "Enter number of years as an integer, for example 5: ");
        numOfYears = MyInput.readInt();
        // Enter loan amount
        System.out.println("Enter loan amount, for example 120000.95: ");
        loanAmount = MyInput.readDouble();
        // Calculate payment
        double monthlyPayment = loanAmount*monthlyInterestRate/
        (1 - (Math.pow(1/(1 + monthlyInterestRate), numOfYears*12)));
        double totalPayment = monthlyPayment*numOfYears*12;
        // Display results
        System.out.println("The monthly payment is " + monthlyPayment);
        System.out.println("The total payment is " + totalPayment);
        // Pause
        System.out.println("Press Ctrl+C to close this window ...");
        MyInput.readInt();
    }
}