home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 2.1 KB | 72 lines |
- import java.io.*;
-
- /*
- * Ammortize.class calculates monthly payment given
- * loan amount, interest, and the number of years of the loan
- */
- class Ammortize {
- public static void main (String args[]) {
- double loanAmount=0, interest=0, years=0;
-
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
-
- loanAmount = inputDouble ("Enter the loan amount in dollars > ", in);
-
- interest = inputDouble ("Enter the interest rate in percent > ", in);
-
- years = inputDouble ("Enter the number of years > ", in);
-
- System.out.print ("The payment is $");
- System.out.println (payment (loanAmount, interest, years));
- } // end of main ()
-
- /*
- * inputDouble method prints a prompt and reads a double
- * BufferedReader
- */
- static double inputDouble (String prompt, BufferedReader in) {
- boolean error;
- String input="";
- double value=0;
-
- do {
- error = false;
- System.out.print (prompt);
- System.out.flush ();
- try {
- input = in.readLine ();
- } catch (IOException e) {
- System.out.println ("An input error was caught");
- System.exit (1);
- }
- try {
- value = Double.valueOf (input).doubleValue();
- } catch (NumberFormatException e) {
- System.out.println ("Please try again");
- error = true;
- }
- } while (error);
- return value;
- } // end of inputDouble ()
-
- /*
- * payment method does the magic calculation
- * double A loan amount
- * double I interest rate, as a percentage
- * double Y number of years
- */
- static double payment (double A, double I, double Y) {
-
- /*
- * call the exponentiation and natural log functions as
- * static methods in the Math class
- */
- double top = A * I / 1200;
- double bot = 1 - Math.exp (Y*(-12) * Math.log (1 + I/1200));
-
- return top / bot;
- } // end of payment ()
- }
-
-
-