Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3

JAVA: amortization table:

Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments.

Please help!

Thank you!!

//start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization { /* * */ public static void main(String [] args) { //declarling necessary values Scanner s = new Scanner(System.in); PrintWriter outputStream = outputStream = null;

System.out.println("Hello! This is a loan calculator."); System.out.println("It will calculate your monthly payments and" + " your payments towards your incurred interest and print it to a csv. file"); System.out.println("If you would like to continue please enter" + "'y' for yes or 'n' for no"); //try-catch statement try { outputStream = new PrintWriter (new FileOutputStream("mortgagecalculator.csv")); } catch (FileNotFoundException e) { System.out.println("Error opening csv file"); System.exit(0); }

char choice = s.next().charAt(0); //while loop loops until user enters n when given choice while ((choice == 'y') || (choice == 'Y')) { //capture input values from user getAmortizationTableDetails(s,outputStream);

//ask user if calculate another mortgage System.out.println("Would you like to enter another mortgage payment?"); System.out.println("Please enter [y] for yes or [n] for no.");

//call to testing method //testingVariables choice = s.next().charAt(0); } //closing scanner and PrintWriter s.close(); outputStream.close(); }

/* Method printDetails formats results to 2 decimals and prints it * to file. * The loop will ensure that each result will be * formatted in the way just described and print it to the file. * The comma after the first statement will ensure that the file seperates * the results and formats it in a table. It will be possible to open * the file in Excel this way. * * @parameters: * Scanner s * PrintWriter outputStream * int month[] * double principal[] * double monthlyPayment[] * double interestPayment[] * double remainingBalance[] * int n * no return statement */ public static void printDetails(Scanner s, PrintWriter outputStream, int month[], double principal [], double monthlyPayment [], double interestPayment [], double remainingBalance[], int n ){ outputStream.println ("Month, Principal, Monthly Payment, Interest Payment, Remaining Balance"); for (int i = 0; i < n; i++) { //print to file outputStream.print(month[i] + ", "); outputStream.printf("%.2f,",principal [i]); outputStream.printf("%.2f,",monthlyPayment[i]); outputStream.printf("%.2f,",interestPayment[i]); outputStream.printf("%.2f ",remainingBalance[i]); } }

public static void printDetailsToConsole(Scanner s, int month[], double principal [], double monthlyPayment [], double interestPayment [], double remainingBalance[], int n ){ System.out.println ("Month, Principal, Monthly Payment, Interest Payment, Remaining Balance"); for (int i = 0; i < n; i++) { //print to file System.out.print(month[i] + ", "); System.out.printf("%.2f,",principal [i]); System.out.printf("%.2f,",monthlyPayment[i]); System.out.printf("%.2f,",interestPayment[i]); System.out.printf("%.2f ",remainingBalance[i]);

if (n < 6) { // System.out.print(month[i] + ", "); System.out.printf("%.2f,",principal [i]); System.out.printf("%.2f,",monthlyPayment[i]); System.out.printf("%.2f,",interestPayment[i]); System.out.printf("%.2f ",remainingBalance[i]);

} else { //printing the first 3 payments and the last 3 payments System.out.print(month[i] + ", "); System.out.printf("%.2f,",principal [i]); System.out.printf("%.2f,",monthlyPayment[i]); System.out.printf("%.2f,",interestPayment[i]); System.out.printf("%.2f ",remainingBalance[i]); }

} }

/* * */ public static void getAmortizationTableDetails (Scanner s, PrintWriter outputStream) { //asking user for input System.out.println("Enter your loan amount."); double loanAmount = s.nextDouble(); System.out.println("Enter your annual interest rate (in percent)."); double interestRate = s.nextDouble(); //in percent; interestRate = interestRate / 100; //to convert to decimal interestRate = interestRate/12; System.out.println("Enter the term of your loan in years"); int termOfLoan = s.nextInt(); int n = termOfLoan * 12; //n = number of payments int size = (int) (termOfLoan * 12); //size for array

//creating and declaring arrays double principal [] = new double [size]; double monthlyPayment[] = new double [size]; double interestPayment[] = new double [size]; int month[] = new int [size]; double remainingBalance [] = new double [size]; double startingBalance = loanAmount;

for (int i = 0; i < n; i++) { month[i] = i+1; monthlyPayment[i] = calculateMonthlyPayment(loanAmount, interestRate, n);

//calling method to fill interestPayment array interestPayment[i] = calculateInterest(startingBalance, interestRate); principal [i] = startingBalance; startingBalance = startingBalance - (monthlyPayment[i] - interestPayment[i]); remainingBalance[i] = startingBalance; } //call to method affordableCheck affordableCheck(loanAmount, interestRate, n);

//call to method printDetails printDetails (s, outputStream, month, principal, monthlyPayment, interestPayment, remainingBalance, n); printDetailsToConsole (s, month, principal, monthlyPayment, interestPayment, remainingBalance, n); }

/* * */ public static void affordableCheck (double loanAmount, double interestRate, int n){ Scanner s = new Scanner(System.in); System.out.println("If you would like to know if you can afford this loan" + " please enter your annual income."); double income = s.nextDouble(); double annualPayment = (calculateMonthlyPayment(loanAmount, interestRate, n) * 12); if (annualPayment > income) { System.out.println("You won't be able to afford this loan."); } else { System.out.println("Congratulations! You can afford this loan."); } }

/* Method calculateMonthlyPayment calculates the monthly payment of the loan * for the user. * * @parameters: double loanAmount * double interestRate * int n * @return: double monthlyPayment */ public static double calculateMonthlyPayment(double loanAmount, double interestRate, int n) { double monthlyPayment; monthlyPayment = (loanAmount * (interestRate * (Math.pow((1 + interestRate),n))))/ ((Math.pow((1 + interestRate), n)) - 1);

//returns solution for monthlyPayment return monthlyPayment; }

/* * */ public static double calculateInterest(double startingBalance, double interestRate) { double interestAmount; interestAmount = startingBalance * interestRate; return interestAmount; }

/* testingVariables tests if methods used in program will * return correct values. * * Method testingVariables tests if the calculation for the interest and the * monthlyPayment produce the accurate output. * no parameters * no return statement */ public static void testingVariables() { System.out.printf("The interest should be $10. %.2f " , calculateInterest(1000,0.01)); System.out.printf("The monthly payment should be $13.22. %.2f ", calculateMonthlyPayment(1000, 0.1, 120)); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions

Question

3. Compare and contrast an NAP and a MAE.

Answered: 1 week ago