
-----------------------------------
LOL123
Tue Mar 23, 2010 9:47 pm

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. 

class mortgage
{
  public static void main (String args 

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.

-----------------------------------
TheGuardian001
Tue Mar 23, 2010 11:49 pm

Re: Calculations not working
-----------------------------------
In your loop, you always perform calculations based on the values of "mortgage" and "INTEREST", IE:

[code]
intMortgage = mortgage + INTEREST;
[/code]

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
Wed Mar 24, 2010 4:23 pm

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
Wed Mar 24, 2010 5:16 pm

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
Wed Mar 24, 2010 6:00 pm

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
Wed Mar 24, 2010 6:12 pm

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:


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
Wed Mar 24, 2010 6:15 pm

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:


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
Wed Mar 24, 2010 8:09 pm

Re: Calculations not working
-----------------------------------

printf works in java as well its the easiest way to round imho.


What is printf? What is imho?

-----------------------------------
DemonWasp
Wed Mar 24, 2010 9:30 pm

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
Wed Mar 24, 2010 9:37 pm

RE:Calculations not working
-----------------------------------
Or.. urbandictionary(dot)com

-----------------------------------
LOL123
Wed Mar 24, 2010 9:48 pm

Re: Calculations not working
-----------------------------------

class mortgageProj
{
  public static void main (String args 

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
Fri Mar 26, 2010 9:38 pm

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: 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
Sat Mar 27, 2010 3:33 pm

Re: Calculations not working
-----------------------------------
Thankyou for your reply it really helped!
