HW 4 Ex 1 (Use this)
Use above to solve below:
HW 4 Ex 2 (Use this)
Use above to solve below:
6 public class Hw4_Ex1 7{ public static void main(String[] args) double loan = 50000; // total loan amount double monthly = 1000; // monthly payment amount double rate = 0.005; // monthly interest rate int months = 12; // number of months to print double balance = loan; int month = 0; double payment = 0; double unpaid = 0; double interest = 0; System.out.printf("%65 %105 %15s %10 %15s ", "Month", "Payment", "Amount Unpaid", "Interest", "New Balance"); for (int i=1; i monthly) payment = monthly; else payment = balance; 35 unpaid = balance - payment; interest = unpaid * rate; balance = unpaid + interest; month++; System.out.printf("%d %10.2f %15.2f %10.2f %15.2f ", month, payment unpaid, interest, balance); 39 ) 40 41 } Let us revisit Homework 4 - Exercise 1 (see the previous homework for details) and implement the same program to output the first several months' loan payoff process using a method with no return value. Save your program as Hw5_Ex1.java. Create a method that: Takes four parameters: (1) the loan amount, (2) the monthly payment amount, (3) the monthly interest rate, and (4) the number of months to print; Does NOT return any value. In your main method, Prompt the user to enter four values: (1) the loan amount, (2) the monthly payment amount, (3) the monthly interest rate, and (4) the number of months to print; Call the method you just created (passing in the four values as the arguments), the method will print out the given number of months' loan payoff process. Your output should look like this: on. Command Prompt C:\test>javac Hw5_Exi.java C:\test>java Hw5_Ex1 Loan amount: 50000 Monthly payment: 1000 Monthly interest rate: 0.005 Number of months to print: 12 Month Payment Amount Unpaid 1999.00 49000.00 1000.00 48245.00 1999.00 47486.23 1000.00 46723.66 1000.00 45957.27 1000.00 45187.06 1000.00 44413.00 1000.00 43635.06 1000.00 42853.24 1000.00 42067.50 11 1000.00 41277.84 1000.00 49484.23 WN TOON Interest 245.00 241.23 237.43 233.62 229.79 225.94 222.06 218.18 214.27 210.34 206.39 202.42 New Balance 49245.00 48486.23 47723.66 46957.27 46187.06 45413.00 44635.06 43853.24 43067.50 42277.84 41 484.23 40686.65 10 C:\test> 6 import java.util.Scanner; 8 public class Hw4_Ex2 9{ 10 public static void main(String[] args) 11 Scanner input = new Scanner(System.in); System.out.print("Loan amount: "); double loan = input.nextDouble(); System.out.print("Monthly payment: "); double monthly = input.nextDouble(); System.out.print("Monthly interest rate: "); double rate = input.nextDouble(); double balance = loan; int month = 0; double payment = 0; double unpaid = 0; double interest = 0; while (balance > 0) if (balance > monthly) payment = monthly; else payment = balance; unpaid = balance - payment; interest = unpaid * rate; balance = unpaid + interest; month++; 42 43 44 45 System.out.printf("Months it takes to pay off the loan: %d", month); 46 } Let us revisit Homework 4 - Exercise 2 (see the previous homework for details) and implement the same program to show the total number of months it will take to pay off the loan. Save your program as Hw5_Ex2.java. Create a method that: Takes three parameters: (1) the loan amount, (2) the monthly payment amount, and (3) the monthly interest rate; Returns an integer: the total number of months it will take to pay off the given loan. In your main method, Prompt the user to enter three values: (1) the loan amount, (2) the monthly payment amount, and (3) the monthly interest rate; Call the method you just created (passing in the three values as the arguments); the method will return the total number of months needed to pay off the loan; save this return value to a variable; Display the result. Your output should look like the following: . Command Prompt C:\test>javac Hw5_Ex2.java C:\test>java Hw5_Ex2 Loan amount: 50000 Monthly payment: 1000 Monthly interest rate: 0.005 Months it takes to pay off the loan: 58 C:\test>