Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

method call  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I am trying to understand and get the answer to the following program below from a java class I am doing.



with method f using n=3 into n+1 which is 3+1=4
f=4 so method f is 4 which is passed into method g
it gets confusing here, not sure what method g prints out (f(n)+f(n-1)) , 4+ (4-1) = 4 + 3
which is 7

is the output 7 not sure if this is correct, any help would be appreciated.

thanks



 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it gets confusing here, not sure what method g prints out (f(n)+f(n-1)) , 4+ (4-1) = 4 + 3
which is 7


Here, it should be

(f(n)+f(n-1))
= f(4) + f(3)
= (4+1)+(3+1) = 9

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:it gets confusing here, not sure what method g prints out...

I think Omkar's explained it, but you could do yourself a lot of help by spacing things out, for example:
  g( f(3) );
and
  System.out.println( f(n) + f(n-1) );
and improving the indentation so that braces ({}) line up.

I understand that the questions you get may not be written that way - this is often done deliberately to confuse you - but if need be copy it out for yourself on paper with the proper spacing and indentation.

HIH

Winston
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for the help.

carolin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!