Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I had a question answered by a tutor on this recursion problem, and it was not a

ID: 3662068 • Letter: I

Question

I had a question answered by a tutor on this recursion problem, and it was not anwered correctly. The section in bold is not recursion. Can someone please see if they could find out what is wrong with it. It is supposed to print the Iteration Method and then the recursion method. Ex:10 20 30 10 20 30 Here is the code.

Code:

public class List

{

class Node

{

public int info;

public Node next = null;

public Node(int el, Node ref)

{

info = el;

next = ref;

}

}

private Node head = null;

public void addToHead(int el)

{

head = new Node(el, head);

}

public static void printIteration(Node tmp)

{

while(tmp != null){

System.out.print(tmp.info + " ");

tmp = tmp.next;

}

System.out.println();

}

//printRecursion method

public static void printRecursion(Node node)

{

int countprintVal=0;

        if(node != null)

        {

         if(countprintVal == 8)

          System.out.println();

         else if(countprintVal%2 == 0)

          System.out.println("          ");

         else if(countprintVal%2 == 1)

          System.out.println("               ");

            printRecursion(node.next);   

        }

}

public static void displayName()

{

System.out.println("This program is written by YOURNAME. ");

}

// DO NOT MODIFY MAIN METHOD

public static void main(String[] args)

{

displayName();

List list = new List();

list.addToHead(30);

list.addToHead(20);

list.addToHead(10);

printIteration(list.head);

printRecursion(list.head);

System.out.println("Good Bye...");

}

}

Explanation / Answer

Well printRecursion method is implemented incorrectly,correct way to do that is:

  public static void printRecursion (Node node)    {      if (node == null)        return;      else {        System.out.println(node.value);        printRecursion(node.next);      };    }