6 Replies - 1702 Views - Last Post: 08 April 2009 - 08:18 PM Rate Topic: -----

#1 HanaMizuki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-October 08

Amortization Table

Post icon  Posted 08 April 2009 - 06:39 PM

I'm trying to create an Amortization table using java, however my table just seems to be returning gibberish while running and im not sure what's wrong with it. please help.


public class amortization_calculator {
   public static void main (String[] args) {
	  // Read the unknown choice from the user
	  System.out.println("Please choose one of the following");
	  System.out.println("1: solve for the monthly payment");
	  System.out.println("2: solve for the principle");
	  System.out.println("3: solve for the total number of payments");
	  Scanner in = new Scanner(System.in);
	  int choice = in.nextInt();

	  //initialize the variables
	  double MP=0;
	  double P=0;
	  double R = 0.08/12;
	  double N=0;
	  int month=0;
	  double output;

	 //setup else-if statements to organize the code and follow correct function

	 if (choice == 1)
		output = solve_monthly_payment(MP, P, R, N, month);
	 else if (choice == 2)
		output = solve_principle(MP, P, R, N, month);
	 else if (choice == 3)
		output = solve_number_of_payments(MP, P, R, N, month);
	 else
		System.out.println("invalid entry");

 }

// set up function to solve for monthly payment given the other information
   static double solve_monthly_payment(double MP, double P, double N, double R, int month){

	 //setup scanner and prompt user for principle and total number of payments
	  Scanner keyboard = new Scanner (System.in);
	  System.out.print("Enter the principle:");
	  P = keyboard.nextDouble();

	  System.out.print("Enter total number of payments:");
	  N = keyboard.nextDouble();


	  //perform operation
	  MP= (P*R)/ Math.pow(1-(1+R),-N);

	 //print the table
	 print_table(P, R, month, MP);

	  return MP;



}
// set up function to solve for the principle given the other information
   static double solve_principle(double MP, double P, double N, double R, int month){

	  //setup scanner and prompt user for monthly payment and total number of payments
	  Scanner keyboard = new Scanner (System.in);
	  System.out.print("Enter the monthly payment:");
	  MP = keyboard.nextDouble();

	  System.out.print("Enter total number of payments:");
	  N = keyboard.nextDouble();



	  //perform operation
	  P= (MP*(Math.pow(1-(1+R),-N)))/R;

	 //print the table
	 print_table(P, R, month, MP);

	  return P;


}
// set up function to solve for the total number of payments given the other information
   static double solve_number_of_payments(double MP, double P, double N, double R, int month){

	  //setup scanner and prompt user for principle and monthly payment
	  Scanner keyboard = new Scanner (System.in);
	  System.out.print("Enter the monthly payment:");
	  MP = keyboard.nextDouble();

	  System.out.print("Enter principle:");
	  P = keyboard.nextDouble();


	  //perform operation
	  N= (-Math.log(((-P*R)/MP)-1))/Math.log(1+R);

	 //print the table
	 print_table(P, R, month, MP);

	  return N;


}
// set up function to print amortization table
   static void print_table(double P, double R, int month, double MP)
   {
	   //initialize the variables
	   double balance = P;
	   double total_P_paid=0;
	   double total_I_paid=0;
	   double I = R;

	//print heading of the table
	System.out.printf("month	  P		 I		  P+I	  total P	total I   balance\n");

	//declare while loop to loop until balance is 0
	  while (balance > 0){

	//print table using formatted printing
	System.out.printf("%2d %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f", month, P, I, P+I, total_P_paid, total_I_paid, balance);

	//update the variables
	month ++;

	P = (MP - I);

	I = R*balance;

	total_P_paid = total_P_paid + P;

	total_I_paid = total_I_paid + I;

	balance = balance - P;

}
	System.out.println("Table Complete!");
   }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Amortization Table

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Amortization Table

Posted 08 April 2009 - 06:51 PM

what exactly is the problem???
Was This Post Helpful? 0
  • +
  • -

#3 HanaMizuki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-October 08

Re: Amortization Table

Posted 08 April 2009 - 06:55 PM

View Postmostyfriedman, on 8 Apr, 2009 - 05:51 PM, said:

what exactly is the problem???


when i run the program it just keeps running and running and never stops.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Amortization Table

Posted 08 April 2009 - 07:22 PM

it all looks good, you just need to go over your calculations again i think because the balance doesnt seem to be decreasing
Was This Post Helpful? 0
  • +
  • -

#5 HanaMizuki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-October 08

Re: Amortization Table

Posted 08 April 2009 - 07:34 PM

View Postmostyfriedman, on 8 Apr, 2009 - 06:22 PM, said:

it all looks good, you just need to go over your calculations again i think because the balance doesnt seem to be decreasing


I realized that but after looking everything over, it seems to be correct. is there anyway that the variables just aren't being updated or anything?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Amortization Table

Posted 08 April 2009 - 08:02 PM

View PostHanaMizuki, on 8 Apr, 2009 - 06:34 PM, said:

I realized that but after looking everything over, it seems to be correct. is there anyway that the variables just aren't being updated or anything?

Not if your code say to update them :D
My last compiler bug was in the late 80ies

Your code in a pain to read ... Java conventions:
- class name starts by capital later
- variables name starts by a lower case letter and the first letter of each additional words is uppercase

forget variables like: MP, P, R, N

This post has been edited by pbl: 08 April 2009 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 HanaMizuki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-October 08

Re: Amortization Table

Posted 08 April 2009 - 08:18 PM

View Postpbl, on 8 Apr, 2009 - 07:02 PM, said:

View PostHanaMizuki, on 8 Apr, 2009 - 06:34 PM, said:

I realized that but after looking everything over, it seems to be correct. is there anyway that the variables just aren't being updated or anything?

Not if your code say to update them :D
My last compiler bug was in the late 80ies

Your code in a pain to read ... Java conventions:
- class name starts by capital later
- variables name starts by a lower case letter and the first letter of each additional words is uppercase

forget variables like: MP, P, R, N



Thanks for the info, I'll definitely remember that for my next program.
Another question for you. Do you see some reason that my values calculated in the previous functions aren't being transferred into the print function?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1