Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need Help Completing a Loan Table

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Need Help Completing a Loan Table

    I'm continuing to try and solve the problem on my own but if any people have tips I would appreciate it.... Focus on the generateLoanTable() method because everything else is good. When I run the program in my main method (not included) it shows the loan amount entered multiplied by the rate (not as a percentage) and year number instead of the total payment for the loan period and rate associated in the table. Also, I don't know the code to put in to highlight reserved words on the thread posts so if I could get that info too that would help

    import java.text.*;
     
    class Loan {
     
         //---------------------
        //DATA MEMBERS
       //----------------------
        private final int MONTHS_IN_YEAR = 12;
        private double  loanAmount;
        private double  monthlyInterestRate;
        private int        numberOfPayments;
     
        private static final int BEGIN_YEAR = 5;
        private static final int END_YEAR    = 30;
        private static final int YEAR_INCR   = 5;
     
        private static final double BEGIN_RATE = 6.0;
        private static final double END_RATE    = 10.0;
        private static final double RATE_INCR   = 0.25;
     
        //Constructor
        public Loan(double amount, double rate, int period) {
            setAmount(amount);
            setRate(rate);
            setPeriod(period);
        }
     
        //Returns the loan amount
        public double getAmount() {
            return loanAmount;
        }
     
        //Returns the loan period in number of years
        public int getPeriod() {
            return numberOfPayments / MONTHS_IN_YEAR;
        }
     
        //Returns the loan's annual interest rate
        public double getRate() {
            return monthlyInterestRate * 100.0 * MONTHS_IN_YEAR;
        }
     
        //Returns the loan's monthly payment amount
        public double getMonthlyPayment() {
            double monthlyPayment;
     
            monthlyPayment = (loanAmount * monthlyInterestRate)
                                  /
                             (1 - Math.pow(1/(1 + monthlyInterestRate), 
                                                numberOfPayments));
     
            return monthlyPayment;
        }
     
        //Returns the total payment amount for the loan
        public double getTotalPayment() {
            double totalPayment;
     
            totalPayment = getMonthlyPayment() * numberOfPayments;
     
            return totalPayment;
        }
     
        //Generates loan table
        public void generateLoanTable(double loanAmt) {
            DecimalFormat df = new DecimalFormat("0.00");
     
            System.out.print("\t");
     
            for(int colLabel = 5; colLabel <= 30; colLabel += 5){            
                System.out.format("%11d", colLabel);
            }
     
            System.out.println("\n");
     
            for(double rate = BEGIN_RATE; rate <= END_RATE; rate += RATE_INCR){
     
                System.out.print(df.format(rate) + "%    ");
     
                for(int year = BEGIN_YEAR; year <= END_YEAR; year += YEAR_INCR){
                    loanAmt = getAmount() * rate * year;
                    System.out.print("   " + df.format(loanAmt));
                }
     
                System.out.println();
            }
     
            System.out.println();
        }
     
        //Sets the loan amount
        public void setAmount(double amount) {
            loanAmount = amount;
        }
     
        //Sets the annual interest rate
        public void setRate(double annualRate) {
            monthlyInterestRate = annualRate / 100.0 / MONTHS_IN_YEAR;
        }
     
        //Sets the loan period
        public void setPeriod(int periodInYears) {
            numberOfPayments = periodInYears * MONTHS_IN_YEAR;
        }
    }
    Last edited by C++kingKnowledge; July 12th, 2012 at 01:27 PM.


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Need Help Completing a Loan Table

    I found your errors.
    First at the beginning of the program where you defined your BEGIN_RATE, END_RATE AND RATE_INCR, you should change that to percentage value. It is because you have not changed the rate into percentage anywhere else. Rate is always in percentage.
    Make it 0.06, 0.1 and 0.0025 respectively.
    Then coming down to your generateLoan() method this is what you needed there. loanAmt = getAmount() + getAmount() * rate * year. It is because if you wanted to calculate the total payable amount during that period, then it should be the interest plus the principle amount.
    Your program generates outputs without converting the rate into percentage and also without adding the principle amount.
    It should solve your problem. Good luck

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need Help Completing a Loan Table

    Thanks for the input... The code you suggested still gave me a few bugs due to the other methods I defined but I finally got it worked out. I had to use the equation for computing the total payment on a loan.... Here's the working method if you just wanted to see how I fixed it

    //Generates loan table
        public void generateLoanTable(double loanAmt) {
            DecimalFormat df = new DecimalFormat("0.00");
     
            System.out.print("\t");
     
            for(int colLabel = 5; colLabel <= 30; colLabel += 5){            
                System.out.format("%11d", colLabel);
            }
     
            System.out.println("\n");
     
            for(double rate = BEGIN_RATE; rate <= END_RATE; rate += RATE_INCR){
     
                System.out.print(df.format(rate) + "%    ");
     
                 //----------------
                //FIXED CODE
               //-----------------
                for(int year = BEGIN_YEAR; year <= END_YEAR; year += YEAR_INCR){
                    loanAmt = getAmount() * (rate/100/MONTHS_IN_YEAR)
                                          /
                            (1 - Math.pow(1/(1 + (rate/100/MONTHS_IN_YEAR)),
                                                           year*MONTHS_IN_YEAR))
                                          *
                            year * MONTHS_IN_YEAR;
                    System.out.print("   " + df.format(loanAmt));
                }
     
                System.out.println();
            }
     
            System.out.println();
        }

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Need Help Completing a Loan Table

    looks great. Good work

Similar Threads

  1. Having trouble completing tester class!
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2012, 12:25 PM
  2. Loan Repayment
    By Langolier in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 18th, 2012, 01:29 PM
  3. Loan Payments
    By clarafeb1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 04:36 AM
  4. problem completing beginners' gui tutorial
    By mechnik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2011, 04:53 PM
  5. savings and loan society
    By 5723 in forum Java Theory & Questions
    Replies: 9
    Last Post: November 14th, 2009, 12:01 AM