-
Help! Simple Java Program.
Here is the problem: 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 (up to 4 different versions) of the table process and should allow the
user to compare runs in a tabular format.
This is what I have so far and should work one I completely debug it. I am really new to Java, and I thinks it is somthing to do with the Array. I recently switched from C++ to Java, so I don't think I have the syntax down correctly. Thanks.
Code:
import javax.swing.JOptionPane;
public class Program_01
{
public static void main(String [] args)
{
int Runs;
do{
String Run_Question = JOptionPane.showInputDialog(null, "How many times,would you like to run" +
" the program? (Up to four runs only!)", "Run Question", JOptionPane.QUESTION_MESSAGE);
Runs = Integer.parseInt(Run_Question);
if( Runs < 0 || Runs > 4)
{
JOptionPane.showMessageDialog(null, "The number is not valid, try again!", "Error",
JOptionPane.INFORMATION_MESSAGE);
}
}while(Runs < 0 || Runs > 4);
Questions(Runs);
}
public static void Questions(int Runs)
{
for(int index = 0;index < Runs;index ++)
{
String Loan_Principle_String = JOptionPane.showInputDialog(null, "What is the Loan Principle?", "Loan Amount",
JOptionPane.QUESTION_MESSAGE);
String Percentage_Rate_String = JOptionPane.showInputDialog(null, "What is the Annual Percentage Rate?",
"Interest Rate", JOptionPane.QUESTION_MESSAGE);
String Monthly_Payment_String = JOptionPane.showInputDialog(null, "What is the Monthly Payment", "Payment",
JOptionPane.QUESTION_MESSAGE);
double Loan_Principle = Double.parseDouble(Loan_Principle_String);
double Percentage_Rate = Double.parseDouble(Percentage_Rate_String);
double Monthly_Payment = Double.parseDouble(Monthly_Payment_String);
Calculate(Loan_Principle, Percentage_Rate, Monthly_Payment);
}
}
public static void Calculate (double Loan, double Percentage, double Monthly)
{
double[] Principle = new double[]{0,0};
double[] Interest = new double[]{0,0};
double[] Remain = new double[]{0,0};
double Monthly_Percent = Percentage/12;
double MPR = ((int)((Monthly_Percent + .005) * 100.) / 100.);
boolean Over = true;
double Borrowed = Loan;
int Month = 0;
for (int index = 0; Over != false; index++)
{
double Rate = MPR * Loan;
double decimalRate = ((int)((Rate + .005) * 100.) / 100.);
Interest[index] = decimalRate;
double Prince = Monthly - decimalRate;
double decimalPrince = ((int)((Prince + .005) * 100.) / 100.);
Principle[index]= decimalPrince;
double Rmn = Borrowed - decimalPrince;
Borrowed = Rmn;
double decimalRmn = ((int)((Rmn + .005) * 100.) / 100.);
Remain[index] = decimalRmn;
if(Monthly > Remain[index] && Remain[index] > 0)
{
Monthly = Remain[index];
Over = false;
}
Month += 1;
}
System.out.println("I am here!" + Month);
System.out.println("Payment #" + " " + "Principle" + " " + "Payment" + " " + "APR" + " " + "MPR" +
" " + "Interest Payment" + " " + "Principle Payment");
System.out.println("------------------------------------------------------------------------");
for(int index = 0; index < Month; index++)
{
System.out.println((index + 1) + "\t" + Remain[index] + "\t" + Percentage + "\t" + MPR + "\t" + Interest[index]
+ "\t" + Principle[index]);
}
}
}
-
Hi SlickWilly440, do you get any error messages? what part of the code works? Can you give a little insigh as to what happends when you run the code and what is expected to happen.
-
Here is my updated code. I am haveing trouble I am having trouble outputting my formate to align with each category.
Code:
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Program_01
{
public static void main(String [] args)
{
int Runs;
do
{
String Run_Question = JOptionPane.showInputDialog(null, "How many times would you like to run" +
" the program? (Up to four runs only!)", "Run Question", JOptionPane.QUESTION_MESSAGE);
Runs = Integer.parseInt(Run_Question);
if( Runs < 0 || Runs > 4)
{
JOptionPane.showMessageDialog(null, "The number is not valid, try again!", "Error",
JOptionPane.INFORMATION_MESSAGE);
}
}while(Runs < 0 || Runs > 4);
Questions(Runs);
}
public static void Questions(int Runs)
{
for(int index = 0;index < Runs;index ++)
{
String Loan_Principle_String = JOptionPane.showInputDialog(null, "What is the Loan Principle?", "Loan Amount",
JOptionPane.QUESTION_MESSAGE);
String Percentage_Rate_String = JOptionPane.showInputDialog(null, "What is the Annual Percentage Rate?",
"Interest Rate", JOptionPane.QUESTION_MESSAGE);
String Monthly_Payment_String = JOptionPane.showInputDialog(null, "What is the Monthly Payment", "Payment",
JOptionPane.QUESTION_MESSAGE);
double Loan_Principle = Double.parseDouble(Loan_Principle_String);
double Percentage_Rate = Double.parseDouble(Percentage_Rate_String);
double Monthly_Payment = Double.parseDouble(Monthly_Payment_String);
Calculate(Loan_Principle, Percentage_Rate, Monthly_Payment);
}
}
public static void Calculate (double Loan, double Percentage, double Monthly)
{
double[] Principle = new double[100];
double[] Interest = new double[100];
double[] Remain = new double[100];
double[] Payment = new double[100];
DecimalFormat Decimal = new DecimalFormat(".00");
double Total_Interest = 0;
double T_Interest;
double Monthly_Percent = (Percentage/12)/100;
double MPR = Monthly_Percent;
boolean Over = true;
double Borrowed = Loan;
double Deposit = Monthly;
int Month = 0;
for (int index = 0; Over = true; index++)
{
double Rate = MPR * Borrowed;
double decimalRate = round(Rate,2);
Interest[index] = decimalRate;
Remain[index] = Borrowed;
Month += 1;
if(Monthly > Remain[index ] && Remain[index ] > 0)
{
for(int count = 0; count <= Month; count++)
{
Total_Interest += Interest[count];
}
T_Interest = round(Total_Interest,2);
Payment[index] = T_Interest;
Deposit = T_Interest;
}
else
{
Payment[index] = Deposit;
}
double Prince = Deposit - decimalRate;
double decimalPrince = round(Prince,3);
Principle[index]= decimalPrince;
System.out.println("I am here!" + decimalPrince);
double Rmn = Borrowed - decimalPrince;
double decimalRmn = round(Rmn,2);
Borrowed = decimalRmn;
if(Monthly > Remain[index ] && Remain[index ] > 0)
{
Over = false;
break;
}
}
System.out.println("Payment #" + " " + "Principle" + " " + "Payment" + " " + "Percentage" + " " + "MPR" +
" " + "Interest Payment" + " " + "Principle Payment");
System.out.println("----------------------------------------------------------------------------");
for(int index = 0; index < Month; index++)
{
System.out.println(" " + (index + 1) + "\t " + Decimal.format(Remain[index]) + " " +
Decimal.format(Payment[index]) + " " + Decimal.format(Percentage) + " " + MPR +
"\t" + Decimal.format(Interest[index]) + "\t\t" + Decimal.format(Principle[index]));
}
T_Interest = ((int)((Total_Interest + .005) * 100.) / 100.);
System.out.println("\n\t\t\t" + "Total Interest Paid \t" + T_Interest + "\n");
for(int index = 0; index < Month; index++)
{
System.out.printf("%f %f\n", Principle[index],Remain[index]);
}
}
public static double round(double val, int places) {
long factor = (long)Math.pow(10,places);
// Shift the decimal the correct number of places
// to the right.
val = val * factor;
// Round to the nearest integer.
long tmp = Math.round(val);
// Shift the decimal the correct number of places
// back to the left.
return (double)tmp / factor;
}
}
Similar Threads
-
By jareivy05 in forum Java
Replies: 9
Last Post: 12-07-2005, 09:11 PM
-
By blumoon02k in forum Java
Replies: 4
Last Post: 03-24-2005, 04:39 PM
-
By Lori Piquet in forum Talk to the Editors
Replies: 114
Last Post: 10-10-2002, 06:01 AM
-
Replies: 0
Last Post: 03-21-2001, 04:01 PM
-
By arun chakravarty in forum Java
Replies: 1
Last Post: 12-01-2000, 10:59 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks