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!");
}
}
Amortization Table
Page 1 of 16 Replies - 1386 Views - Last Post: 08 April 2009 - 08:18 PM
#1
Amortization Table
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.
Replies To: Amortization Table
#2
Re: Amortization Table
Posted 08 April 2009 - 06:51 PM
what exactly is the problem???
#3
Re: Amortization Table
Posted 08 April 2009 - 06:55 PM
#4
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
#5
Re: Amortization Table
Posted 08 April 2009 - 07:34 PM
mostyfriedman, 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?
#6
Re: Amortization Table
Posted 08 April 2009 - 08:02 PM
HanaMizuki, 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
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
#7
Re: Amortization Table
Posted 08 April 2009 - 08:18 PM
pbl, on 8 Apr, 2009 - 07:02 PM, said:
HanaMizuki, 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
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?
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|