for loop help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    for loop help.

    My assignment is

    LoanTable

    Background:

    When buying a home, a very important financial consideration that many buyers face is obtaining a qualifying loan from a financial institution. Interest rates can be fixed or variable and there are service charges called 'points' for taking out a loan. One 'point' is equal to 1% of the loan amount (called principal) borrowed. Taking out a loan of $150,000 with a 2 point charge will amount to a cost of $3,000 for obtaining the loan - before you ever make your first mortgage payment! Some banks and financial lending institutions offer lower interest rates but require higher points, and vice versa. Usually, the more po ints you pay, the lower the interest rate. It is helpful to know what the monthly mortgage payment will be for a given loan amount with different interest rates.

    The monthly payment on a loan is determined using three inputs:

    1. The amount of the loan (principal).

    2. The number of years for the loan to be paid off.

    3. The annual interest rate of the loan.

    The formula for determining payments is:

    a = (p * k * c)(c - 1)

    p = principal, amount borrowed
    k = monthly interest rate (annual rate/12.0)
    n = number of monthly payments (years * 12)
    c = (1 + k)n
    a = monthly payment (interest and principal paid)


    Assignment:

    1. Write a program that prompts the user for the following information:

    a. The amount of the loan
    b. The length of the loan in years
    c. A low interest rate in %
    d. A high interest rate in %

    2. Print out the monthly payment for the different interest rates from low to high, incremented by 0.25%.


    3. A sample run output is given below:

    Mortgage problem

    Principal = $100000.00
    Time = 30 years
    Low rate = 11%
    High rate = 12%

    Annual Interest Rate Monthly Payment

    11.00 952.32
    11.25 971.26
    11.50 990.29
    11.75 1009.41
    12.00 1028.61

    4. Your program should make use of the built-in pow method located in the Math class.

    5. Your program must make use of separate methods for the data input section and the printing section of the assignment.




    My code is

    import java.util.Scann er;

    public class LoansTable {

    public static void main(String args[])
    {
    Scanner myScanner = new Scanner(System. in);
    double principalAmout;
    double lowRate;
    double highRate;
    int years;
    double monthlyPayment;
    double k;
    double n;
    double c;

    System.out.prin tln("Enter the amount of the loan you would like to take."); //User Inputs
    principalAmount = myScanner.nextD ouble();
    System.out.prin tln("Enter the length of the loan in years.");
    years = myScanner.nextI nt();
    System.out.prin tln("Enter a low interest rate in %.");
    lowRate = myScanner.nextD ouble();
    System.out.prin tln("Enter a high interest rate in %.");
    highRate = myScanner.nextD ouble();

    System.out.prin tln("Principal = $" + principalAmount );
    System.out.prin tln("Time = " + years + " years");
    System.out.prin tln("Low Rate = " + lowRate + "%");
    System.out.prin tln("High Rate = " + highRate + "%");

    for (double lowRate; lowRate <= highRate; lowRate = lowRate + 0.25)
    {
    k = lowRate / 12.0;
    n = years * 12;
    c = Math.pow((1 + k), n);
    monthlyPayment = (principalAmoun t * k * c) / (c - 1);
    System.out.prin tln("Your Annual Interest Rate is " + lowRate);
    System.out.prin tln("Your Monthly Payment is " + monthlyPayment) ;
    }
    }
    }

    the error that shows is that it cannot find symbol principalAmount in the code.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Compilers are quite picky when it comes to 'principalAmout ' vs 'principalAmoun t'.

    kind regards,

    Jos

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #3
      Originally posted by JosAH
      Compilers are quite picky when it comes to 'principalAmout ' vs 'principalAmoun t'.

      kind regards,

      Jos
      thank you very much..it was a silly mistake

      Comment

      • Energizer100
        New Member
        • Nov 2007
        • 60

        #4
        I have another problem. It says that

        my for loop thing is not a statement

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #5
          nvr mind
          i got it to work

          Comment

          • Energizer100
            New Member
            • Nov 2007
            • 60

            #6
            I have another problem. I'm trying to make the double round off to 2 decimal places.

            import java.util.Scann er; //Imports Input

            public class LoansTable //Creates Class
            {
            public static void main(String args[]) //Main Method
            {
            Scanner myScanner = new Scanner(System. in); //Creates Scanner
            double principalAmount ; //Variables
            double lowRate;
            double annualRate; //I added annualRate in here because lowRate had to be assigned to something in the for loop. And it makes more sense.
            double highRate;
            int years;
            double monthlyPayment;
            double k; //k, n, c are the variables given in the assignment. I didn't want the calculation to look like a mess, so I did each part seperately.
            double n;
            double c;

            System.out.prin tln("Enter the amount of the loan you would like to take."); //User Inputs
            principalAmount = myScanner.nextD ouble(); //principalAmount is assigned to the first number the user inputs

            System.out.prin tln("Enter the length of the loan in years.");
            years = myScanner.nextI nt(); //years is assigned to the second number the user inputs

            System.out.prin tln("Enter a low interest rate in %.");
            lowRate = myScanner.nextD ouble(); //lowRate is assigned to the third number the user inputs. I was a bit confused when the assignment said to ask the user to input the lowRate in %, but after seeing the sample sets, I decided against it.

            System.out.prin tln("Enter a high interest rate in %.");
            highRate = myScanner.nextD ouble(); //highRate is assigned to the fourth number the user inputs.

            System.out.prin tln();
            System.out.prin tln("Mortgage Problem");
            System.out.prin tln();
            System.out.prin tln("Principal = $" + principalAmount ); //First off, the program will output what the user just inputted
            System.out.prin tln("Time = " + years + " years");
            System.out.prin tln("Low Rate = " + lowRate + "%");
            System.out.prin tln("High Rate = " + highRate + "%");
            System.out.prin tln(); //Prints a blank line.
            System.out.prin tln("Annual Interest Rate Monthly Payment"); //Prints out Annual Interest Rate and Monthly Payment
            System.out.prin tln();

            for(annualRate = lowRate; annualRate <= highRate; annualRate += 0.25) //A for loop is the most logical thing to use here. annualRate had to be assigned to lowRate in order for the loop to work correctly.
            {
            k = annualRate * 0.01 / 12.0; //Here is where I made my calculations seperately. First off is the k calculation
            n = years * 12; //And then the n
            c = Math.pow((1 + k), n); //And then the c, which uses a Math.pow
            monthlyPayment = (principalAmoun t * k * c) / (c - 1); //This main calculation looks much neater than putting all of them together
            System.out.prin t(" " + annualRate);
            System.out.prin tln(" " + monthlyPayment) ; //These last two lines will output the monthly payment and the annual interest rate in table form. There was a lot of spacing in between.
            }
            }
            }

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Energizer100
              I have another problem. I'm trying to make the double round off to 2 decimal places.
              Have a look at the DecimalFormat class or use the printf() method
              from the PrintStream class (it basically uses a same DecimalFormat object to
              do the hard work).

              kind regards,

              Jos

              Comment

              • Energizer100
                New Member
                • Nov 2007
                • 60

                #8
                Originally posted by JosAH
                Have a look at the DecimalFormat class or use the printf() method
                from the PrintStream class (it basically uses a same DecimalFormat object to
                do the hard work).

                kind regards,

                Jos
                I looked it up, both of them. But I still don't understand how to actually use it in my program.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Energizer100
                  I looked it up, both of them. But I still don't understand how to actually use it in my program.
                  Did you actually read the API documentation for those classes and methods?
                  Armed with one of them you can easily print a floating point number rounded
                  to any number of decimal positions.

                  kind regards,

                  Jos

                  Comment

                  Working...