Need help with Java Code

Can someone tell me whats wrong with my Java Code

Page 1 of 1

7 Replies - 1447 Views - Last Post: 09 March 2008 - 03:16 PM Rate Topic: -----

#1 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Need help with Java Code

Post icon  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.

// 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++;
				}

			}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Java Code

#2 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

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 :D
Was This Post Helpful? 0
  • +
  • -

#3 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Re: Need help with Java Code

Posted 09 March 2008 - 09:07 AM

View Postgl3thr0, on 9 Mar, 2008 - 05:59 AM, said:

you want to explain what it does? whats wrong, and put ur code in tags :D

Was This Post Helpful? 0
  • +
  • -

#4 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Re: Need help with Java Code

Posted 09 March 2008 - 09:14 AM

View Postgl3thr0, on 9 Mar, 2008 - 05:59 AM, said:

you want to explain what it does? whats wrong, and put ur code in tags :D


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++;
}

}
}


Was This Post Helpful? 0
  • +
  • -

#5 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

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)

This post has been edited by gl3thr0: 09 March 2008 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Need help with Java Code

Posted 09 March 2008 - 11:37 AM

code tags were almost good, but the closing tag is /code : :code:
Was This Post Helpful? 0
  • +
  • -

#7 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Need help with Java Code

Posted 09 March 2008 - 11:41 AM

View Post1lacca, on 9 Mar, 2008 - 11:37 AM, said:

code tags were almost good, but the closing tag is /code : :code:



haha i saw tht bt when i came back they were fixed i ws all wtf!! :P
Was This Post Helpful? 0
  • +
  • -

#8 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Re: Need help with Java Code

Posted 09 March 2008 - 03:16 PM

View Postgl3thr0, on 9 Mar, 2008 - 11:41 AM, said:

View Post1lacca, on 9 Mar, 2008 - 11:37 AM, said:

code tags were almost good, but the closing tag is /code : :code:



haha i saw tht bt when i came back they were fixed i ws all wtf!! :P


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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1