Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the code taken by right clicking and viewing the Page Source View of the

ID: 3835422 • Letter: H

Question

Here is the code taken by right clicking and viewing the Page Source View of the JavaScript code. I am asked to Port this code to Java.

The end result should look like the picture below with the button and text boxes.

<HTML>

<HTML>

<FORM NAME="loandata"> <TABLE> <TR><TD COLSPAN=3><BIG><B>Enter Loan Information:</B></BIG></TD></TR> <TR> <TD>1)</TD> <TD>Amount of the Loan (Any Currency):</TD> <TD><INPUT TYPE=text NAME=principal SIZE=12></TD> </TR> <TR> <TD>2)</TD> <TD>Annual Percentage Rate of Interest:</TD> <TD><INPUT TYPE=text NAME=interest SIZE=12></TD> </TR> <TR> <TD>3)</TD> <TD>Repayment Period in Years:</TD> <TD><INPUT TYPE=text NAME=years SIZE=12></TD> </TR> <TR><TD COLSPAN=3> <BIG><B> <INPUT TYPE=button VALUE="Compute"> <BR>Payment Information: </B></BIG> </TD></TR> <TR> <TD>4)</TD> <TD>Your Monthly Payment will be:</TD> <TD><INPUT TYPE=text NAME=payment SIZE=12</TD> </TR> <TR> <TD>5)</TD> <TD>Your Total Payment will be:</TD> <TD><INPUT TYPE=text NAME=total SIZE=12</TD> </TR> <TR> <TD>6)</TD> <TD>Your Total Interest Payments will be:</TD> <TD><INPUT TYPE=text NAME=totalinterest SIZE=12</TD> </TR> </TABLE> </FORM> <SCRIPT LANGUAGE="JavaScript"> function calculate() { var principal = document.loandata.principal.value; var interest = document.loandata.interest.value /100 /12; var payments = document.loandata.years.value *12; var x = Math.pow(1 + interest, payments); var monthly = (principal * x * interest)/(x-1); if ((!isNaN(monthly)) && (monthly != Number.POSITIVE_INFINITY) && (monthly != Number.NEGATIVE_INFINITY) ) { document.loandata.payment.value=(monthly); document.loandata.total.value=(monthly * payments); document.loandata.totalinterest.value=((monthly * payments) - principal); } else { document.loandata.payment.value=""; document.loandata.total.value=""; document.loandata.totalinterest.value=""; } } </SCRIPT> </HTML> Enter Loan Information: 1) Amount of the Loan (Any Currency): 2) Annual Percentage Rate of Interest 3) Repayment Period in Years Compute Payment Information 4) Your Monthly Payment will be: 5) Your Total Payment will be: 6) Your Total Interest Payments will be:

Explanation / Answer

import java.text.NumberFormat;
import java.util.Scanner;

public class MortgagePaymentCalculator {

public static double calculateMonthlyPayment(
int loanAmount, int termInYears, double interestRate) {

// Convert interest rate into a decimal
// eg. 6.5% = 0.065

interestRate /= 100.0;

// Monthly interest rate
// is the yearly rate divided by 12

double monthlyRate = interestRate / 12.0;

// The length of the term in months
// is the number of years times 12

int termInMonths = termInYears * 12;

// Calculate the monthly payment
// Typically this formula is provided so
// we won't go into the details

// The Math.pow() method is used calculate values raised to a power

double monthlyPayment =
(loanAmount*monthlyRate) /
(1-Math.pow(1+monthlyRate, -termInMonths));

return monthlyPayment;
}
  
public static void main(String[] args) {

// Scanner is a great class for getting
// console input from the user

Scanner scanner = new Scanner(System.in);

// Prompt user for details of loan

System.out.print("Enter loan amount: ");
int loanAmount = scanner.nextInt();

System.out.print("Enter loan term (in years): ");
int termInYears = scanner.nextInt();

System.out.print("Enter interest rate: ");
double interestRate = scanner.nextDouble();

// Display details of loan

double monthlyPayment =
calculateMonthlyPayment(loanAmount, termInYears, interestRate);


// NumberFormat is useful for formatting numbers
// In our case we'll use it for
// formatting currency and percentage values

NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
NumberFormat interestFormat =
NumberFormat.getPercentInstance();

// Display details of the loan

System.out.println("Loan Amount: "+
currencyFormat.format(loanAmount));
System.out.println("Loan Term: "+
termInYears+" years");
System.out.println("Interest Rate: "+
interestFormat.format(interestRate));
System.out.println("Monthly Payment: "+
currencyFormat.format(monthlyPayment));

}

}