Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008, 08:31 AM
sivasayanth's Avatar
Member
 
Join Date: Dec 2007
Posts: 20
sivasayanth is on a distinguished road
i could not get the recursion in java
I have just studied one java source code. That entirely belongs to recursion in java. However, I could not understand what happened in the execution sequence.
Code:
public class MainClass { public static void main(String[] args) { double x = 5.0; System.out.println(x + " to the power 4 is " + power(x, 4)); System.out.println("7.5 to the power 5 is " + power(7.5, 5)); System.out.println("7.5 to the power 0 is " + power(7.5, 0)); System.out.println("10 to the power -2 is " + power(10, -2)); } // Raise x to the power n static double power(double x, int n) { if (n > 1) return x * power(x, n - 1); // Recursive call else if (n < 0) return 1.0 / power(x, -n); // Negative power of x else return x; } }
Please help me.
What is the return value When first execution happened?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 09:23 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Initially x = 5.0

Code:
double x = 5.0;
Call the power() with x = 5.0 and n = 4. Those parameters are moving on the loop as follows.
Code:
on if clause - still n > 1(n = 4) so, return 5.0 * power( 5.0, 3) on if clause - still n > 1(n = 3) so, 5.0 * return 5.0 * power( 5.0, 2) on if clause - still n > 1(n = 3) so, 5.0 * 5.0 * return 5.0 * power( 5.0, 1) skip else-if clause - n is not a negative on else clause 5.0 * 5.0 * 5.0 * return 5.0
So the answer is 625.0 for the first println(). Hope it's help
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-23-2008, 09:34 AM
sivasayanth's Avatar
Member
 
Join Date: Dec 2007
Posts: 20
sivasayanth is on a distinguished road
Thanks a lot Eranga.

if we take the visual of above code.it is chain loop. is it Eranga?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-23-2008, 10:08 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, kind of a chain loop. Simply do the same process(what we called looping) until certain condition is satisfied and keep track of certain data.

Here you can see that the value of x behave like that.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Recursion andrew777 New To Java 1 03-29-2008 02:51 PM
recursion kdeighan New To Java 3 01-25-2008 11:48 PM
Recursion bozovilla Advanced Java 3 01-07-2008 06:53 PM
Help with recursion scts102 New To Java 1 11-20-2007 12:07 AM
Recursion in java lenny Advanced Java 1 08-07-2007 08:23 AM


All times are GMT +3. The time now is 10:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org