Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a method in a class call itself?

 
Dinesh Kumar Tadepalli
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to code a factorial program. I have a doubt regarding if a method in a class can call itself? For example:



And in another test class.. I make an object of this class and ask the object's method for computing the factorial. Like:



Will this above strategy work ? I am beginner and when i ran this style of code, it was giving lots of exceptions. I need some help regarding this.

Thanks, Dinesh
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh Kumar Tadepalli wrote:



compute method compiled?

Dinesh Kumar Tadepalli wrote:
And in another test class.. I make an object of this class and ask the object's method for computing the factorial. Like:



Will this above strategy work ?


you can do this definitly. this relationship is known as Composition . try to debug yourself
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This concept of a method calling itself is called Recursive function. You can do this but you need to be a little careful. Your compute method is doing wrong this. First of all, there is no return statement. If there was, then it would infinitely keep calling itself resulting in a StackOverflowError. You need to put a condition in your method body which will stop it from endlessly calling itself

 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read a joke once about a programmer who died of starvation in the shower. They found a bottle of shampoo in his hand with the directions "Lather. Rinse. Repeat".

computers would actually work that way. They lather, they rinse, they repeat - which tells them to lather, then rinse, then repeat - which tells them to lather, then rinse, then repeat - which tells them to lather...

The idea behind recursion is that each call doesn't try and SOLVE the problem, but it tries to break the problem into a smaller problem which CAN be solved. One (or potentially both) of those problem may need to be broken down again into even smaller components.

Eventually, you get to some trivial case that can be easily solved, so you then wind your way back up. So in Englisgh...

5! might be too tough to handle. But, you know that

5! = 5 * 4!. So now, if you know 4! , the rest is EASY. so, call a method to compute 4!.

Well, 4! is too hard on it's own, but you know that 4! = 4 * 3!. so, let's compute 3!.

3! is too hard on it's own, but you know it's equal to 3 * 2!.

2! is equal to 2 * 1!, so let's compute 1!.

1! is trivial. it's 1. so, now we go back up...

2! = 2 * 1!, and we know 1!, so we can compute the answer and get 2. pass that back up.

3! = 3 * 2! (which we now know), so we get 3 * 2 or 6. pass that back up...

4! = 4 * 3! = 4 * 6 = 24. pass that back up....

5! = 5 * 4! = 5 * 24 = 120. Now we're done.
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't he die because he washed the top of his head away?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic