Answer:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class labExercise1
{
public static void main(String[] args)
{
int sum= 0;
double interestRate = 5.0;
double monthlyPayment=0;
double totalPayment=0;
String input = JOptionPane.showInputDialog(null,"Enter the loan amount: ($)","Lab Exercise 1", 2);
double loanAmount = Double.parseDouble(input);
String input1 = JOptionPane.showInputDialog(null,"Enter the loan period(years):","Lab Exercise 1",2);
double numberOfYears = Double.parseDouble(input1);
JOptionPane.showConfirmDialog(null,"The loan amount is $" + loanAmount + ", and the loan duration is " + numberOfYears + "year(s). Continue?","Lab Exercise 1",2);
String s1 = null;
System.out.print("Interest Rate(%) Monthly Payment($) Total Payment($) ");
s1 = "Interest Rate(%) Monthly Payment($) Total Payment($) ";
while (interestRate <= 8.0)
{
double monthlyInterestRate = interestRate / 1200;
monthlyPayment= loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
totalPayment = monthlyPayment * numberOfYears * 12;
s1 = s1 + String.format("%.3f%30.2f %40.2f ",interestRate, monthlyPayment, totalPayment) + " ";
System.out.println(s1);
interestRate = interestRate + 1.0 / 8;
}
//s1 = String.format("%16.3f%19.2f%19.2f ",interestRate, monthlyPayment, totalPayment);
System.out.println(s1);
JOptionPane.showMessageDialog(null,s1,"Lab Exercise 1",3);
}
}
Explanation:
- Take the loan amount and period as inputs from the user.
- Calculate the monthly interest rate by dividing the interest rate with 1200.
- Calculate total payment using the following the formula:
- totalPayment = monthlyPayment * numberOfYears * 12;