473,796 Members | 2,482 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.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.
Nov 4 '07 #1
8 2902
JosAH
11,448 Recognized Expert MVP
Compilers are quite picky when it comes to 'principalAmout ' vs 'principalAmoun t'.

kind regards,

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

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.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.
}
}
}
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
2559
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 it makes however I would like the entire script to loop 34 times and on each of those 30 loops also write the product and the price (only) onto the page building a compleate list of all the products selected over the 34 loops ending with a total...
0
1425
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 xml.load(Server.MapPath("ProjectTEST.xml") Dim sDate(200,100
3
2672
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 value of y (works well). create table test (a integer) @ create table debug1 (a integer) @ create trigger test_1 after insert on test referencing new as ins for
6
11079
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
7599
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 looping function the window becomes unmovable and cannot close under its own direction (the upper right "close 'X'") My first attempt to solve the problem was to have the looping function execute as its own thread, the idea being this would...
32
2608
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 memories are of but I think I just said loop somewhere inside the loop and it immediately jumped back to the start of the loop and began again. I can't seem to do that in .net. I this functionality available?
16
3542
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 instructions which can sure be optimized away is the check for the break condition, at least within the time where it is known that the loop will not reach it. Any idea how to write such a loop? e.g.
3
33372
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 code is here: foreach ( string propertyName in ht.Keys ) { this.setProperty( propertyName, ht );
8
6498
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 using a code loop and an INSERT INTO query. About 800,000 records of raw text. Later, I can then loop through and parse these 800,000 strings into usable data using more code. The problem I have is that the conversion of the text file, using a...
4
6832
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 System.IO.FileSystemEventArgs) Dim strFilename As String = "c:\web\example.mdb"
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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 we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.