Results 1 to 3 of 3

Thread: Recursion

  1. #1
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default Recursion

    Hi, i want to know how this recursion is executing

    XML Code:
    class P5PartA {  
       public static void main(String args[]) {    
       String data = new String("01101101");    
       System.out.println("Conversion:  " + conversion(data));  
       }  
       public static double conversion(String data) {   
       return conversion(data, 0);  
        }  
       public static double conversion(String data, int index) {    
       double result;   
       int bit = Integer.parseInt(data.substring(data.length() - 1));    
       bit = bit * 2;    
       if (data.length() == 1) {      
       return Math.pow(bit, index);    
       }    
       result = conversion(data.substring(0, data.length() - 1), index + 1);     
       if (bit != 0) {     
       result = result + Math.pow(bit, index);   
       }    
    return result;  
    }
    }
    Can someone please step through its execution showing the recursive process and the state of the system as it changes.
    Last edited by kathyla18; 04-08-2009 at 10:21 PM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    10

    Default

    What is stopping you from doing it yourself?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    23

    Default

    Take a piece of paper and write-down what each line of this code do. If you really stuck on something ask your question at the time. We'll help you to find the solution.

    This is not a bulky code lol. Just have a try first and show your effort.

Similar Threads

  1. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 01:52 PM
  2. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 02:13 AM
  3. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 09:03 AM
  4. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 12:41 PM
  5. Recursion
    By Zosden in forum Algorithms
    Replies: 4
    Last Post: 05-05-2008, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •