473,508 Members | 4,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for loop help.

60 New Member
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.Scanner;

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.println("Enter the amount of the loan you would like to take."); //User Inputs
principalAmount = myScanner.nextDouble();
System.out.println("Enter the length of the loan in years.");
years = myScanner.nextInt();
System.out.println("Enter a low interest rate in %.");
lowRate = myScanner.nextDouble();
System.out.println("Enter a high interest rate in %.");
highRate = myScanner.nextDouble();

System.out.println("Principal = $" + principalAmount);
System.out.println("Time = " + years + " years");
System.out.println("Low Rate = " + lowRate + "%");
System.out.println("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 = (principalAmount * k * c) / (c - 1);
System.out.println("Your Annual Interest Rate is " + lowRate);
System.out.println("Your Monthly Payment is " + monthlyPayment);
}
}
}

the error that shows is that it cannot find symbol principalAmount in the code.
Nov 4 '07 #1
8 2884
JosAH
11,448 Recognized Expert MVP
Compilers are quite picky when it comes to 'principalAmout' vs 'principalAmount'.

kind regards,

Jos
Nov 4 '07 #2
Energizer100
60 New Member
Compilers are quite picky when it comes to 'principalAmout' vs 'principalAmount'.

kind regards,

Jos
thank you very much..it was a silly mistake
Nov 4 '07 #3
Energizer100
60 New Member
I have another problem. It says that

my for loop thing is not a statement
Nov 4 '07 #4
Energizer100
60 New Member
nvr mind
i got it to work
Nov 4 '07 #5
Energizer100
60 New Member
I have another problem. I'm trying to make the double round off to 2 decimal places.

import java.util.Scanner; //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.println("Enter the amount of the loan you would like to take."); //User Inputs
principalAmount = myScanner.nextDouble(); //principalAmount is assigned to the first number the user inputs

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

System.out.println("Enter a low interest rate in %.");
lowRate = myScanner.nextDouble(); //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.println("Enter a high interest rate in %.");
highRate = myScanner.nextDouble(); //highRate is assigned to the fourth number the user inputs.

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

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 = (principalAmount * k * c) / (c - 1); //This main calculation looks much neater than putting all of them together
System.out.print(" " + annualRate);
System.out.println(" " + 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.
}
}
}
Nov 5 '07 #6
JosAH
11,448 Recognized Expert MVP
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
Nov 5 '07 #7
Energizer100
60 New Member
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.
Nov 5 '07 #8
JosAH
11,448 Recognized Expert MVP
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
Nov 5 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2547
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes...
0
1410
by: Tim::.. | last post by:
Hi can someone please give me some help with this little problem I am having with the following loop ...:: CODE ::. < 'Load XM set xml = Server.CreateObject("Microsoft.XMLDOM" xml.async = fals...
3
2657
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
6
11058
by: Shill | last post by:
I have several questions. In C, AFAIU, a for loop is just syntactic sugar for a while loop. for (i1; i2; i3) i4; is equivalent to i1 while (i2) {
8
7574
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
32
2554
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
3490
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
3
33331
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
8
6463
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
4
6812
by: joaotsetsemoita | last post by:
hello everyone. Im trying to time out a loot after a certain time. Probably 5 to 10 minutes. I have the following function Private Sub processFileCreation(ByVal source As Object, ByVal e As...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.