title: Fibonacci Trace
body: <p>I just want to be upfront that this is a homework assignment.  I have attempted the problem in so many different ways that I am just out of ideas for why I am not getting the desired output.</p>
  
  <h2>Problem</h2>
  
  <p>Write a program that will trace how Fibonacci numbers are generated recursively (for any N) and display the trace in the following way :</p>
  
  <p>Example (N=4):</p>
  
  <pre class="lang-none prettyprint-override"><code>Entering level 0
  Entering level 2
  Entering level 4
  Exiting level 4
  Entering level 3
  Exiting level 3
  Exiting level 2
  Entering level 1
  Entering level 3
  Exiting level 3
  Entering level 2
  Entering level 4
  Exiting level 4
  Entering level 3
  Exiting level 3
  Exiting level 2
  Exiting level 1
  Exiting level 0
  </code></pre>
  
  <hr>
  
  <p>My Main:</p>
  
  <pre><code>public class A5main {
  
  public static void main(String[] args) {
      //n holds user input
      //level is the current level of the tree
      //fibonacci is a5class object
      int n;
      int level=0;
      a5class fibonacci= new a5class();
      Scanner keyboard = new Scanner(System.in);
  
      //Ask user for input
  
     System.out.println("Enter a number up to which Fibonacci series to print: ");
     n = keyboard.nextInt();
     System.out.println("Fibonacci trace of: " + n);
  
      //Pass input to fibonacci.trace method with arguments n, level.
     fibonacci.trace(n,level);
  
  }
  }
  </code></pre>
  
  <p>My class:  </p>
  
  <pre><code> package a5main;
   public class a5class {
      int fibWork;
     public a5class()     
  { 
  }
  public int trace(int t, int level)
  {
      //Accepts t and level as an argument.
      //Lets uer know what level they are entering 
      System.out.println("Now entering level " + level);
      //If t&lt;=1 just return the value
          if (t&lt;=1)
          {
              System.out.println ("\tNow exiting level " + level);
              return t;
  
          }
          //Else use recurssion to figure out the fibonacci sequence
          //and determine what level you are on.
          else
          {
              fibWork = trace(t-1, level+1) + trace(t-2, level+1);
              System.out.println ("\tNow exiting level " + level);
              return t; 
          }
  }
  }
  </code></pre>
  
  <p>(I put the \t there so I can see where it is exiting easier for now)</p>
  
  <p>My Output:</p>
  
  <pre><code>Enter a number up to which Fibonacci series to print: 
  4
  Fibonacci trace of: 4
  Now entering level 0
  Now entering level 1
  Now entering level 2
  Now entering level 3
       Now exiting level 3
  Now entering level 3
       Now exiting level 3
       Now exiting level 2
  Now entering level 2
       Now exiting level 2
       Now exiting level 1
  Now entering level 1
  Now entering level 2
       Now exiting level 2
  Now entering level 2
       Now exiting level 2
       Now exiting level 1
       Now exiting level 0
  </code></pre>
  
  <p>I've also attempted to not pass 'level into the method and have it equal 0 in my public a5class when the object is created but have had no luck with it so far.</p>
  
  <p>While mine might not be so far off I notice that it exits level 3 and then re-enters it in the next sequence which doesn't seem logically correct either.</p>
  
  <p>I appreciate any help or guidance.  Even just a pointer in the right direction is appreciated.  I take my programming seriously and want to actually comprehend it.  I don't want to 'fake' my way through it and have a worthless degree.</p>
  
  <p>Thank you!</p>