DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Exclamation Precalculated Loans !!URGETN!!

    I have to write a an automobile financing program it is supposed to calculate simple interest, baloon payments charges, and precomputed loans. I wrote most of the program, but i cannot figure out one of the formulas i was given.

    Here is the formula:

    f(m)=d-pm+c(t)((2t-m+1)/(t(t+1)))

    I do not know how to use this formula. I do not know if c is supposed to be the total of another formula or something else:

    Here is my code:



    import java.io.*;
    class calculation{
    private double d;
    public int n;
    private double i;
    private double a;
    private double payment;
    private double ballonp;
    private double f;
    private double p;

    calculation(double borrowed, int months, double interest){
    d = borrowed;
    n = months;
    i = interest;
    a = (1+i/12);
    }
    double round(double x){
    double z=((double)(Math.round(x*100)))/100;
    return z;
    }

    double monthlyp(){
    payment = ((d*i*(Math.pow(a,n)))/(Math.pow(a,n)-1));
    return (round(payment/12));
    }

    double balance(int m){
    double p = payment;
    double b = (d*Math.pow(a,m)-p*(Math.pow(a,m)-1)/i);
    return (round(b));
    }

    double ballonp(int m){
    ballonp = (d-(m-1)+this.balance(m));
    return (round(ballonp));
    }
    double precomputed(double m){
    double c =i;
    int t = 24;
    f = (d-(p*m)+c*(t*((m*(2*t-m+1))/t*(t+1))));
    return f;
    }
    }

    class AutoFinancing{
    public static void main(String args[])throws IOException{
    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Amount Borrowed: ");
    System.out.println("Number of months: ");
    calculation first = new calculation(10000,24,0.06);
    System.out.println(" Monthly Payments = $ "+first.monthlyp());
    System.out.println();
    for(int m =1;m<=first.n;m++){
    System.out.println("Month "+m+": Balance = $ "+ first.balance(m));
    }
    System.out.println(first.ballonp(10)); //calculated ballon payment for given month
    System.out.println();
    System.out.println(first.precomputed(24)); //24th month, I used it for testing


    }
    }


    Thanks in Advance

  2. #2
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    c looks like the constant interest rate, to me.

  3. #3
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    I have found out what the value of c was supposed to be, and i wrote the program, but I do not understand some of my output.

    When I enter some of the options I wrote, the program outputs that plus another part that it was not supposed to. Can somebody please take a look at my code?

    import java.io.*;
    class calculation{
    private double d;
    private double i;
    public int n;
    private double a;
    private double p;
    private double ballonp;
    private double c;
    private int t;
    private double f;

    calculation(double borrowed, double interest, int term){ //class constructor
    d = borrowed;
    i = interest;
    n = term;
    a = (1+i/12);
    }

    double round(double x){ //rounding method
    double z=((double)(Math.round(x*100)))/100;
    return z;
    }

    double monthlyp(){ //monthly payment calculation
    p = ((d*i*(Math.pow(a,n)))/(Math.pow(a,n)-1));
    return (round(p/12)); //call rounding method with parameters of p/12
    }

    double balance(int m){ //simple interest calculation
    double b = (d*Math.pow(a,m)-p*(Math.pow(a,m)-1)/i); //m = current month
    return (round(b)); //call rounding method with parameters of p
    }

    double ballonp(int m){ //baloon payment calculation
    ballonp = (d-(m-1)+this.balance(m)); //calls balance method with parameters of m
    return (round(ballonp));
    }

    double c(int m){ //Rule of 78s calculation
    c =(n-(m-1))/((n*(n+1))/2);
    return round(c);
    }

    double precomputed(int m){ //precomputed loan calculation
    t = n;
    f=(d-(p*m)+(c*t))*(m*(2*t-m+1))/(t*(t+1));
    return round(f); //calls rounding method with parameters of f
    }
    }

    class AutoFinancing{
    public static void main (String[]args)throws IOException{
    double borrowed;
    double interest;
    int term;
    int choice;
    boolean exit = false;
    int baloonpayment;
    BufferedReader userinput = new BufferedReader(new InputStreamReader(System.in)); //creates new instance
    //of BufferedReader
    do{
    System.out.print("Amount of $ borrowed: ");
    borrowed = Double.parseDouble(userinput.readLine());
    System.out.println();
    System.out.print("Interest rate: ");
    interest = Double.parseDouble(userinput.readLine());
    interest /=100;
    System.out.print("Term of months: ");
    term = Integer.parseInt(userinput.readLine());
    System.out.println();
    calculation current = new calculation(borrowed,interest,term); //creates new instance of method
    //calculation with parameters
    //borrowed , interest,and term
    System.out.println("Your calculation options are:");
    System.out.println("1. Simple interest");
    System.out.println("2. Precalculated interest");
    System.out.println("3. Baloon payment");
    System.out.println("4. Exit");
    System.out.print("Your Choice: ");
    choice = Integer.parseInt(userinput.readLine());

    switch(choice){
    case 1:{ //simple interest case
    for(int m =1;m<=current.n;m++){
    System.out.println("Month "+m+": Balance = $ "+ current.balance(m));
    }
    }
    case 2:{ //precomputed interest case
    for(int m =1;m<=current.n;m++){
    System.out.println("Month "+m+": Precomputed loan = $ "+current.precomputed(m));
    System.out.println();
    }
    }
    case 3:{
    System.out.println();
    System.out.print("Month of the baloon payment: ");
    baloonpayment = Integer.parseInt(userinput.readLine());
    System.out.println(current.ballonp(baloonpayment)); //calculated ballon payment for given month
    }
    case 4:{
    exit = false;
    }
    default:{ //alternative case
    System.out.println("Invalid option");
    }
    } //end of switch statement
    }while(!exit); //exit when boolean value exit is true
    }
    }


    Thanks alot

  4. #4
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    you need break statements after each of your case statements. Also changed case #4 to exit = true;

    Code:
    switch (choice) {
            case 1: { //simple interest case
              for (int m = 1; m <= current.n; m++) {
                System.out.println("Month " + m + ": Balance = $ "
                    + current.balance(m));
              }
              break;
            }
            case 2: { //precomputed interest case
              for (int m = 1; m <= current.n; m++) {
                System.out.println("Month " + m + ": Precomputed loan = $ "
                    + current.precomputed(m));
                System.out.println();
              }
              break;
            }
            case 3: {
              System.out.println();
              System.out.print("Month of the baloon payment: ");
              baloonpayment = Integer.parseInt(userinput.readLine());
              //calculated payment for given month
              System.out.println(current.ballonp(baloonpayment));
              break;
            }
            case 4: {
              exit = true;
            }
            default: {
              System.out.println("Invalid option");
            }
          } //end of switch statement

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links