• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding  RSS feed

 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,
I'm very new to Java Programming and I require some help understanding few things ;)
I'm learning Java with a book called "Introduction to Java Programming" - Comprehensive Version by Y. Daniel Liang and I just had a topic to help me understand Software Development Process and in that I had an example code to calculate a Loan after x years with x interest rate.

This is my code
The thing is, I don't really understand how line no. 24 works


Any help would be much appreciated.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how line no. 24 works 


That is an assignment statement that evaluates the expression that is to the right of the = and assigns its value to the variable to the left of the =.  The statement also defines the variable: monthlyPayment as a double.

To see what the expression is doing, break it up into simple single operator expressions that save their values in separate variables.
For example with this expression:
x = y * (x - 2)
it could be broken into these 2 simple statements:
int part2 = (x - 2);
x = y * part2;
 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what this line of code does.
I don't really get the Math.pow and the way why is there (1 - 1) before it and what does Math.pow do.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does Math.pow

Look at the API doc for the Math class's pow() method to see what it does:  http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double-

why is there (1 - 1) 

That must be what the formula requires for it to get the correct answer.    I don't know what that formula is for and can not help you understand why it is coded the way it is.
Do an internet search on the topic of computing monthly payments to find a better answer.
 
Hannah Leitheiser
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This (image if it works) seems to be the formula in math notation.  I'm new to Java, but I'd be very surprised if the order of precedence is much different than C++, so Java's doing the

1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)

before the subtraction.
Image-0027.png
[Thumbnail for Image-0027.png]
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grzegorz Skawinski wrote:I know what this line of code does.
I don't really get the Math.pow and the way why is there (1 - 1) before it and what does Math.pow do.

The formula is a well known one from elementary financial maths.
I wrote something about it in this topic:

(explanation of some financial formulas)
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you.
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Order of Precedence
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!