1 Replies - 10003 Views - Last Post: 14 September 2010 - 04:23 PM Rate Topic: -----

#1 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Loan Amortization Table

Posted 07 September 2010 - 04:07 PM

I have to create an Amortization table that asks for the user's Initial Loan Principal, APR, and Monthly Payment. The directions are this:

Assignment: Write a program that creates a loan amortization table. The user of the program will supply values for Initial Loan Principal, Annual Percentage Rate and Monthly Payment. The program should print out the appropriate amortization table including the number of Monthly Payments and the Total Interest paid for the life of the loan. The program should allow multiple runs of the table process and should allow the user to compare runs in a tabular format (at least 4 different versions) at the end of the program execution.

I just have several problems with my code:

1.It goes past the loan principal after it is paid off.

2. It is calculating the interest wrong, but I don't know wheere or how to fix it.

3. The Total Interest gets the last value of the interest instead of adding them up.

Here is the code.

/**
 * @(#)AmortizationTable.java
 *
 *
 *@Daniel Maclin
 *Homework #1
 * 
 */
import java.util.Scanner;
import java.lang.Math;

public class AmortizationTable 
{

    public static void main(String[] args) 
    {
    	//int ctrl;
    	
			Scanner input = new Scanner(System.in);//Creates Scanner input
			
			//Prompt the user for Loan Amount
			System.out.print("Enter the loan Amount: ");
			double LoanAmt = input.nextDouble();
			
			//Prompt for Number of Years
			System.out.print("Enter the number of years for the loan: ");
			int numOfYears = input.nextInt();
			
			//Prompt for APR
			System.out.print("Enter Annual Percentage Rate(APR) like 8.25: ");
			double APR = input.nextDouble();
			
			//Prompt for Monthly Payments
			System.out.print("Enter your monthly payments: ");
			double MonthlyPayment = input.nextDouble();
			
			//Create the monthly interest rate(APR is a yearly rate)
			double Mrate = (APR / 1200);
			System.out.println("Mrate "+Mrate);
			
			//Give Balance the value of the Loan Amount
			double principal = LoanAmt;
			double TotInterest = 0;//Creates the total interest
			double principalPay;//The principal loan that changes
			double interest;//The interest of the loan
			
			//The header display
			System.out.println("Payment #\tPrincipal \t  Payment \tAPR \tMR \tInterest Payment \tPrincipal Payment");
			int i;//a counter for the loop
			for(i = 0; i <= numOfYears * 12; i++)
			{
				interest = (int) (Mrate * principal * 100) / 100;
				
				principalPay = (int) ((MonthlyPayment - interest) * 100) / 100;
				principal = (int) ((principal - principalPay) * 100) / 100;
				TotInterest += interest;
				System.out.println(i + "\t\t\t" + principal
        			+ "\t\t\t" + MonthlyPayment + "\t" + APR + "\t" + Mrate + "\t\t" +TotInterest 
        			+ "\t\t" + principalPay);
			}
			System.out.println("The total interest over the life of the loan is: "+TotInterest);
			
			
			
			
			
			
			
			
    }
    
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: Loan Amortization Table

#2 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Loan Amortization Table

Posted 14 September 2010 - 04:23 PM

So I did change up the program from that post. What I need is to incorporated arrays into the program. I want to know how to prompt the user for the values in one run, instead of separately (for example, I want the user to input the principal, apr, and the monthly payment in a row instead of it asking for the principal 5 times in a row, then the apr 5 times in a row, etc).

/**
 * @(#)AmortizationTable.java
 *
 *
 *@Daniel Maclin
 *Homework #1
 * 
 */
import java.util.Scanner;
import java.lang.Math;
/******************************************
 *Remember to make Pseudocode for it!!!!!!*
 *										  *
 ******************************************/
//Need to display total Interest paid and total Principal Payment
public class AmortizationTable 
{

    public static void main(String[] args) 
    {
    	//Creates the control variable for the infinite looping
    		boolean flag = true;
    		Scanner input = new Scanner(System.in);//Creates Scanner input
    		
			
			
			while(flag)
			{
				
				double[] principal = new double[5];
				double[] APR = new double[5];
				double[] MonthlyPayment = new double[5];
				double[] TotInterest = new double[5];
				int[] r = new int[5];
				
				//Prompt the user for Loan Amount
				System.out.print("Enter the loan Amount: ");
				principal[5] = input.nextDouble();
		
				//Prompt for APR
				System.out.print("Enter Annual Percentage Rate(APR) like 8.25: ");
				APR[5] = input.nextDouble();
			
				//Prompt for Monthly Payments
				System.out.print("Enter your monthly payments: ");
				MonthlyPayment[5] = input.nextDouble();
				
				//Create the monthly interest rate (APR is a yearly rate)
				double Mrate = APR / 1200;
				
				//Give Balance the value of the Loan Amount
				double TotInterest = 0;//The total interest
				double principalPay;//The monthly payment loan that changes
				double interest;//The interest of the loan
				double TotalprincipalPay = 0.0;//The total principal payment of the loan
			
				//The header display
				System.out.println("Payment #\tPrincipal \t  Payment \tAPR \tInterest Payment \tPrincipal Payment");
				System.out.println("-----------------------------------------------------------------------------");
				int i;//a counter for the loop
				//The loop calculates the life of the loan
					for(i = 1; i < 1 * 12; i++)
					{
						//Calculates the interest by multiplying the Monthly rate by the principal
						interest =  (Mrate * principal);
						if( MonthlyPayment > principal + interest)
        				{
        					MonthlyPayment = principal + interest;
        				}
						principalPay = (MonthlyPayment - interest);
						//To calculate the total interest of the loan by adding the new interest to it.
						TotInterest += interest;
				
						//To calculate the total principal payment				
						TotalprincipalPay += principalPay;
				
						//Displays the data in a column format
						System.out.printf("%3d          %4.2f      %2.2f    %2.2f        %2.2f            %2.2f\n", i, 
							principal, MonthlyPayment, APR, interest, principalPay);
					
					  //Calculates the principal through the loan. Must be here to show the initial loan in Payment #1
        				principal =  (principal - principalPay);			
					}
			//Displays the Total Interest and the Total Principal Payment below the table
				System.out.printf("The total interest over the life of the loan is: %2.2f\n", TotInterest);
				System.out.printf("The total principal payment paid over the life of the loan is: %3.2f\n", TotalprincipalPay);
				
				System.out.print("Do you want to run it again? Yes/No  ");
				String option = input.nextLine();
				option = input.nextLine();
				System.out.println("Option is:  **" + option + "***");
    			if(option == "Yes" || option == "yes")
					{
						flag = true;
						System.out.println("True");
					}
				else 
					{
						flag = false;
						System.out.println("False");
					}
			}
	}

}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1