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

I did the whole thing... only thing that is messing up is the white space after

ID: 3723777 • Letter: I

Question

I did the whole thing... only thing that is messing up is the white space after the System.out.printf("The total amount paid back after %d years is $%,.2f%n. <--- this period wont work the whitespace brings it to another line. I need to have it on the same line

3.19 Lab 5 - Mortgage Demo

Write a program that computes the monthly mortgage payments and the total amount paid back given the principal, annual interest rate, and the length of the loan in years.

import java.util.Scanner;

public class Mortgage {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.print("Enter principal, rate, and number of years: ");

double princ=input.nextDouble();

double rate=input.nextDouble();

int nyrs=input.nextInt();

if(rate>=1.0)

rate=rate/100.0;

double mrate=rate/12.0;

int nmonths=nyrs*12;

double pay=princ*mrate/(1.0-Math.pow(1.0+mrate, -nmonths));

double total=pay*nmonths;

System.out.printf("Monthly mortgage payment: $%,.2f%n",pay);

System.out.printf("The total amount paid back after %d years is $%,.2f%n.",nyrs,total);

input.close();

}

}

Explanation / Answer

import java.util.Scanner;

public class Mortgage {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.print("Enter principal, rate, and number of years: ");

double princ=input.nextDouble();

double rate=input.nextDouble();

int nyrs=input.nextInt();

if(rate>=1.0)

rate=rate/100.0;

double mrate=rate/12.0;

int nmonths=nyrs*12;

double pay=princ*mrate/(1.0-Math.pow(1.0+mrate, -nmonths));

double total=pay*nmonths;

System.out.printf("Monthly mortgage payment: $%,.2f%n",pay);

System.out.printf("The total amount paid back after %d years is $%,.2f.",nyrs,total);

input.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote