// java package
import java.text.DecimalFormat;
import java.util.*;
// This will calculate a 30yr mortgage monthly payment
public class DavisMortgageCal2
{
public static void main(String[] args)
{
// declare variables
double intrest,payment;
int ammount,term;
DecimalFormat decimalPlaces=new DecimalFormat("0.00");
intrest=.0575;
ammount=200000;
term=30;
payment=(ammount*((intrest/12)/(1-Math.pow((1+(intrest/12)),-(term*12)))));
// Calculator outputs
System.out.println("Principal="+ammount);
System.out.println("intrest rate="+intrest*100);
System.out.println("Years="+term);
System.out.println("Number of payments="+term*12);
System.out.print("Monthly Payment=");
System.out.println(decimalPlaces.format(payment));
}
}
public class Scanner
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the original loan amount $ (-1 to end the program): ");
ending_balance = input.nextDouble();
System.out.println("Enter the interest rate on your loan %: ");
annual_rate = input.nextDouble();
System.out.println();
DecimalFormat f = new DecimalFormat("$#,###,###.00");
// Setup a counter to count payments
int count = 1;
// Get our standard payment which is 1/360 of loan
payment = (ending_balance / 360.0);
while (ending_balance > 0.0) {
new_balance = ending_balance;
// Calculate interest by multiplying rate against balance
interest_paid = new_balance * (annual_rate / 12.0);
// Subtract interest from your payment
principle_paid = payment - interest_paid;
// Subtract final payment from running balance
ending_balance = new_balance - principle_paid;
// If the balance remaining plus its interest is less than payment amount
// Then print out 0 balance, the interest paid and that balance minus the interest will tell us
// how much principle you paid to get to zero.
if ((new_balance + interest_paid) < payment) {
System.out.println(count + ". Payment: " + f.format(new_balance + interest_paid) + " Interest: " + f.format(interest_paid) + " Principle: " + f.format(new_balance - interest_paid) + " Loan Balance is: $0.00");
}
else {
// Lets show the table, loan, interest, and payment made towards principle
System.out.println(count + ". Payment: " + f.format(payment) + " Interest: " + f.format(interest_paid) + " Principle: " + f.format(principle_paid) + " Loan Balance is: " + f.format(ending_balance));
}
count++;
}
}
}
Need help with Java CodeCan someone tell me whats wrong with my Java Code
Page 1 of 1
7 Replies - 1190 Views - Last Post: 09 March 2008 - 03:16 PM
#1
Need help with Java Code
Posted 09 March 2008 - 05:47 AM
Wuz up everybody! Can someone please help me with my Java Amortization code for my Java class. Its due tomorrow and I am stuck. The code is below.
Replies To: Need help with Java Code
#2
Re: Need help with Java Code
Posted 09 March 2008 - 05:59 AM
you want to explain what it does? whats wrong, and put ur code in tags
#3
Re: Need help with Java Code
Posted 09 March 2008 - 09:07 AM
#4
Re: Need help with Java Code
Posted 09 March 2008 - 09:14 AM
gl3thr0, on 9 Mar, 2008 - 05:59 AM, said:
you want to explain what it does? whats wrong, and put ur code in tags 
Sorry abut that. The code is to amortize a 30 yr loan at interest rate of 5.75%. When I compile the code I get several errors.
// java package
import java.text.DecimalFormat;
import java.util.*;
// This will calculate a 30yr mortgage monthly payment
public class DavisMortgageCal2
{
public static void main(String[] args)
{
// declare variables
double intrest,payment;
int ammount,term;
DecimalFormat decimalPlaces=new DecimalFormat("0.00");
intrest=.0575;
ammount=200000;
term=30;
payment=(ammount*((intrest/12)/(1-Math.pow((1+(intrest/12)),-(term*12)))));
// Calculator outputs
System.out.println("Principal="+ammount);
System.out.println("intrest rate="+intrest*100);
System.out.println("Years="+term);
System.out.println("Number of payments="+term*12);
System.out.print("Monthly Payment=");
System.out.println(decimalPlaces.format(payment));
}
}
public class Scanner
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the original loan amount $ (-1 to end the program): ");
ending_balance = input.nextDouble();
System.out.println("Enter the interest rate on your loan %: ");
annual_rate = input.nextDouble();
System.out.println();
DecimalFormat f = new DecimalFormat("$#,###,###.00");
// Setup a counter to count payments
int count = 1;
// Get our standard payment which is 1/360 of loan
payment = (ending_balance / 360.0);
while (ending_balance > 0.0) {
new_balance = ending_balance;
// Calculate interest by multiplying rate against balance
interest_paid = new_balance * (annual_rate / 12.0);
// Subtract interest from your payment
principle_paid = payment - interest_paid;
// Subtract final payment from running balance
ending_balance = new_balance - principle_paid;
// If the balance remaining plus its interest is less than payment amount
// Then print out 0 balance, the interest paid and that balance minus the interest will tell us
// how much principle you paid to get to zero.
if ((new_balance + interest_paid) < payment) {
System.out.println(count + ". Payment: " + f.format(new_balance + interest_paid) + " Interest: " + f.format(interest_paid) + " Principle: " + f.format(new_balance - interest_paid) + " Loan Balance is: $0.00");
}
else {
// Lets show the table, loan, interest, and payment made towards principle
System.out.println(count + ". Payment: " + f.format(payment) + " Interest: " + f.format(interest_paid) + " Principle: " + f.format(principle_paid) + " Loan Balance is: " + f.format(ending_balance));
}
count++;
}
}
}
#5
Re: Need help with Java Code
Posted 09 March 2008 - 11:37 AM
okay.. well u have alot of problems
1st i dont no if this was on purpose bt u named your class Scanner? thats the same as the Scanner class that ur using for input ?
i renamed it Scanner2 bt w/e
next u hvnt declared the types of ANY of ur variables inthe Scanner class
they have a name and a value BUT no type (int,char,String....) which in ur case happens to be double .. so go through and add a type to every nd u should be fine..
*edit*
and if u want more the 1 class in a file only one can be public :P (the one with the same name as the file)
1st i dont no if this was on purpose bt u named your class Scanner? thats the same as the Scanner class that ur using for input ?
i renamed it Scanner2 bt w/e
next u hvnt declared the types of ANY of ur variables inthe Scanner class
they have a name and a value BUT no type (int,char,String....) which in ur case happens to be double .. so go through and add a type to every nd u should be fine..
*edit*
and if u want more the 1 class in a file only one can be public :P (the one with the same name as the file)
This post has been edited by gl3thr0: 09 March 2008 - 11:38 AM
#6
Re: Need help with Java Code
Posted 09 March 2008 - 11:37 AM
code tags were almost good, but the closing tag is /code :
#7
Re: Need help with Java Code
Posted 09 March 2008 - 11:41 AM
#8
Re: Need help with Java Code
Posted 09 March 2008 - 03:16 PM
gl3thr0, on 9 Mar, 2008 - 11:41 AM, said:
Thanks allot! I added and declared the varables as double and when I compiled it I got no errors. Againg thanks!
But one more quesrion? When I run the program all I get is alot of wording but nothing about the mortgage calculator. Can you explain what that mean?
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|