The Coin Return Foundational Fundraiser is here! Please donate!

Java Amortization Program

Pocket Size AsianPocket Size Asian Registered User regular
edited January 2010 in Help / Advice Forum
Okay, I need help with my code.

I have two different codes, one you can pull up in a web browser and the other you can't.

Two questions...

1. How can I make the one I can pull up in a web browser into an amortization code? (First code shown)

2. How can I make the other one be shown in a web browser. (second code shown)

Yes, I know they look different. We're working on a team project and I can't figure out how to merge the two, so I'm trying to turn one into the other...

Thanks for all your help!!

<BODY>
<SCRIPT LANGUAGE="JavaScript">
// calculates the payments and updates for fields to
// display the result
// the next 9 lines is where I specify the equation for the calculation

function morgagepayment() {
var amt = document.frm.amt.value;
var ir = document.frm.ir.value / 1200;
var term = document.frm.term.value * 12;
var total=1;

for (i=0;i<term;i++) {
total = total * (1 + ir);
}
mp = amt * ir / ( 1 - (1/total));

// use Math object to chop all numbers after 2 digits
document.frm.payment.value = Math.round(mp*100)/100;
document.frm.total.value = Math.round(mp * term *100)/100 ;
}
</script>

<!--//The below code is used for making the table and specifying its style and look for the user interface>

<h2 style="text-align: center; color: black">Morgage Calculator</h2>
<h3 style="text-align: center; color: black">By: Erika Mahaney</h3>
<hr/>
<center>
<form name="frm">
<table bgcolor="white" cellpadding="2"><tr><td>
<table bgcolor="wheat" cellspacing="10">

<!--//type in the morgage amount>
<tr>
<td>Mortgage amount</td>
<td><input type="text" name="amt" value="200000" /></td>
</tr>


<!--//type in the interest rate>
<tr>
<td>Yearly interest rate</td>
<td><input type="text" name="ir" value="5.75" /></td>
</tr>

<!--//type in the term in years>
<tr>
<td>Term (in years)</td>
<td><input type="text" name="term" value="30" /></td>
</tr>
<tr><td colspan="2"><hr/></td></tr>

<!--//displays your monthy payment>
<tr>
<td>Monthly payment:</td>
<td><input type="text" name="payment" /></td>
</tr>

<!--//displays your total payment>
<tr>
<td>Total payments:</td>
<td><input type="text" name="total" /></td>
</tr>
<tr >
<td colspan="2" width="100%">
<input type="button" value="Calculate Monthly Payment"
onclick="morgagepayment();" />
</td></tr>
</table>
</td></tr></table>
</form>
</center>
</BODY>



<HTML>
<BODY>


import java.util.Scanner;
import java.text.DecimalFormat;



public class amortization {
public static void main(String args[]) {
// Make sure we use types that hold decimal places
double new_balance = 0.0, ending_balance = 0.0;
double interest_paid = 0.0, annual_rate = 0.0, principle_paid = 0.0, payment = 0.0;

Scanner input = new Scanner(System.in);

System.out.print("Please enter the original loan amount $ (-1 to end the program): ");
ending_balance = input.nextDouble();
System.out.print("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/20 of loan
payment = (ending_balance / 20.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++;
}

}
}


</BODY>
<HTML>

---Pocket Size Asian---
Pocket Size Asian on

Posts

  • Jimmy KingJimmy King Registered User regular
    edited January 2010
    Well, for one, Java and JavaScript are not at all related in any way. You're not going to be able to copy and paste from one into the other and have anything other than a big ass broken mess.

    If the second one, which is in Java, does what you need then you need to learn JavaScript well enough to implement the same logic written in JavaScript. Alternately, you need to learn Java well enough to take that Java code and turn it into a jsp or servlet, get access to Tomcat, and get it working as a servlet or jsp. I assure you, the first option is a lot less work, especially if your experience level is where it appears to be (very beginning of learning this stuff).

    Jimmy King on
  • Pocket Size AsianPocket Size Asian Registered User regular
    edited January 2010
    Okay. Thanks... I appreciate your help I suppose.

    Either way, i figured it out. :) Asked one of the developers I work with.

    Please close this thread.

    Pocket Size Asian on
    ---Pocket Size Asian---
Sign In or Register to comment.