Author |
Message |
LOL123
|
Posted: Tue Mar 23, 2010 9:47 pm Post subject: Calculations not working |
|
|
Hello I have to make a program that calculates when a presons mortgage will be paid off
A homeowner takes a mortgage for $120 000 at 10 3/4% per year. At the end of each year an amount of $24 000 is paid. Write a program to show how the mortgage is paid off, year by year until the mortagage is paid off.
Quote:
class mortgage
{
public static void main (String args [])
{
int year = 2010;
double mortgage = 120000;
int yearPay = 24000;
final double INTREST = 10.75;
double payedOff = 0;
double intMortgage = 0;
double newMortgage = 0;
System.out.println ("The Original Mortgage is $" + mortgage);
System.out.println ("The rate of intrest is " + INTREST);
System.out.println ("The mortgage was started in " + year);
//
do
{
// This selection checks if the mortgage is equal to 0
if (mortgage >= (payedOff))
{
System.out.println (" ");
System.out.println (year++);
intMortgage = mortgage + INTREST;
newMortgage = intMortgage - yearPay;
System.out.print ("After the yearly payment the mortgage ");
System.out.println ("is equal to $" + newMortgage);
}
else if (mortgage == (payedOff))
{
System.out.println ("The mortgage is paid off.");
}
}
while (mortgage >= payedOff);
}
}
I have the program running and on a loop but the calculations each time are the same how would II get the calculation to work properly.
Any help you can offer is appreciated. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
TheGuardian001
|
Posted: Tue Mar 23, 2010 11:49 pm Post subject: Re: Calculations not working |
|
|
In your loop, you always perform calculations based on the values of "mortgage" and "INTEREST", IE:
code: |
intMortgage = mortgage + INTEREST;
|
But since those two values never change, intMortgage is given the same value each time, meaning that all your calculations give the same value each time. You put the new value int "newMortgage", but never use that value in future calculations. |
|
|
|
|
 |
LOL123
|
Posted: Wed Mar 24, 2010 4:23 pm Post subject: RE:Calculations not working |
|
|
Yes I figured that out too, but I dont get how to make the program use the the new value in "newMortgage" would I have to use an if elese if? wait that wouldnt work either would it... |
|
|
|
|
 |
TheGuardian001
|
Posted: Wed Mar 24, 2010 5:16 pm Post subject: Re: Calculations not working |
|
|
Do you ever use the original value of "mortgage" again? Why not just assign to that instead of to intMortgage and newMortgage? Or, if you want to keep using mortgage, why not just set the initial value of "intMortgage" to the initial value of "mortgage" and then use that. (newMortgage isn't really needed.) |
|
|
|
|
 |
LOL123
|
Posted: Wed Mar 24, 2010 6:00 pm Post subject: RE:Calculations not working |
|
|
Ok thankyou for your help! I have finished the program and it works, but while you are here in the output I need to have the numbers rounded to 2 decimal places. Do you know how to do this?
Oh I would give you +karma for your great help but I dont have enough posts sorry  |
|
|
|
|
 |
TheGuardian001
|
Posted: Wed Mar 24, 2010 6:12 pm Post subject: Re: Calculations not working |
|
|
I'm not sure if there's any built in way to round to a specific number of decimal places (There probably is though...), however you can round to two decimal places by following these steps:
Quote:
1)Multiply by 100. This gives you all of the numbers you want to keep in front of the decimal (IE: 12.543 * 100 = 1254.3)
2)round. This gets rid of all the decimal places you don't want to keep. (Math.Round(1254.3) = 1254)
3)Divide by 100. This restores the decimal place to its original location, giving you two decimal places (1254/100 = 12.54)
You may need to convert it to a String afterward, since I think double/floats always output to maximum precision, even if it's all 0s. |
|
|
|
|
 |
syntax_error

|
Posted: Wed Mar 24, 2010 6:15 pm Post subject: Re: Calculations not working |
|
|
TheGuardian001 @ Wed Mar 24, 2010 6:12 pm wrote: I'm not sure if there's any built in way to round to a specific number of decimal places (There probably is though...), however you can round to two decimal places by following these steps:
Quote:
1)Multiply by 100. This gives you all of the numbers you want to keep in front of the decimal (IE: 12.543 * 100 = 1254.3)
2)round. This gets rid of all the decimal places you don't want to keep. (Math.Round(1254.3) = 1254)
3)Divide by 100. This restores the decimal place to its original location, giving you two decimal places (1254/100 = 12.54)
You may need to convert it to a String afterward, since I think double/floats always output to maximum precision, even if it's all 0s.
printf works in java as well its the easiest way to round imho. |
|
|
|
|
 |
LOL123
|
Posted: Wed Mar 24, 2010 8:09 pm Post subject: Re: Calculations not working |
|
|
Quote:
printf works in java as well its the easiest way to round imho.
What is printf? What is imho? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Wed Mar 24, 2010 9:30 pm Post subject: RE:Calculations not working |
|
|
printf = "print formatted", I think. It's lifted from C, where it's a common way of outputting readable strings to the console. Another common way is fprintf.
IMHO stands for "In My Humble Opinion". This is called an "acronym", so you can Google it yourself in the future. |
|
|
|
|
 |
SNIPERDUDE

|
Posted: Wed Mar 24, 2010 9:37 pm Post subject: RE:Calculations not working |
|
|
Or.. urbandictionary(dot)com |
|
|
|
|
 |
LOL123
|
Posted: Wed Mar 24, 2010 9:48 pm Post subject: Re: Calculations not working |
|
|
Quote:
class mortgageProj
{
public static void main (String args [])
{
int year = 1;
double mortgage = 120000;
int yearPay = 24000;
final double INTEREST = 0.1075;
double paidOff = 0;
double intMortgage = 0;
final int SHIFT = 100;
System.out.println ("The Original Mortgage is $" + mortgage);
System.out.println ("The rate of intrest is " + INTEREST);
System.out.println ("The mortgage was started in year " + year);
//
do
{
// This selection checks if the mortgage is equal to 0
if (mortgage >= paidOff)
{
System.out.println (" ");
intMortgage = mortgage * INTEREST;
mortgage = mortgage + intMortgage;
mortgage = mortgage - yearPay;
mortgage = Math.round (mortgage*SHIFT);
mortgage = mortgage / SHIFT;
System.out.println ("Year = "+year++);
System.out.println ("INTEREST = "+INTEREST+"%");
System.out.println ("Yearly Payment = $"+ yearPay);
System.out.println ("Mortgage = $"+ mortgage);
}
else if (mortgage < paidOff)
{
System.out.println ("Congratulations the mortgage is paid off");
}
}
while (mortgage >= paidOff);
System.out.println ("Congratulations the mortgage is paid off!");
}
}
I have the program working and rounding to two decimal places, but when the program is run and when mortgage is paid off it goes into negative numbers instead of stoping at zero like it should. How can I fix this? |
|
|
|
|
 |
Barbarrosa

|
Posted: Fri Mar 26, 2010 9:38 pm Post subject: Re: Calculations not working |
|
|
Please look at my signature.
Also:
Don't set mortgage to what you rounded, just call Math.round(whatever) inside of System.out.println(). That way, you keep your value. Alternatively, there's a class specifically for formatting numbers: http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html
I don't know how you got negative numbers, but your "zero" paidOff variable isn't exactly zero. It's 0.00...(random stuff). Try using an integer instead - that's just a floating point thing.
Something more aesthetic:
I recommend eliminating the "else if" after you find you don't need a marker for when the program loops too many times.
Edit:
You're getting negative numbers because the last iteration subtracts "mortgage" to less than zero. There's a problem with the last year's payment.
2nd Edit:
You can make a variable just for displaying the number, as well. Then use whatever method to find and assign it, then display the amount using System.out.println(). |
|
|
|
|
 |
LOL123
|
Posted: Sat Mar 27, 2010 3:33 pm Post subject: Re: Calculations not working |
|
|
Thankyou for your reply it really helped! |
|
|
|
|
 |
|