Question: Hello, this is for Programming 2 with the java language. This is about recursion. Its pretty darn confusing and we already have a test coming
public class PrintCall
{
public static void main(String[] args)
{
printMe(1);
}
public static void printMe(int n)
{
if ( (n != 1) && (n != 5) )
{
for (int i = 1; i
System.out.print(' ');
System.out.println("This was written by call number " + n + ".");
}
// Add an if stmt here with a recursive stmt in its body
// along with a for loop similar to the one above
}
}
OUTPUT
This was written by call number 2.
This was written by call number 3.
This was written by call number 4.
This ALSO was written by call number 4.
This ALSO was written by call number 3.
This ALSO was written by call number 2.
This ALSO was written by call number 1.
///////////////////////////////////////////////////////////////////
public class Puzzle
{
public static void main(String[] args)
{
System.out.println("puzzle(9) = " + puzzle(9));
}
private static int puzzle (int n)
{
if ( (n % 3) == 2 )
return 1;
else if ( (n % 3) == 1 )
return ( puzzle (n + 1) + 2 );
else
return ( puzzle (n / 3) + 1 );
}
}
OUTPUT:
puzzle(9) = 5
CSC 205 Lab 9 : Recursion Goals After completing this lab, you should be able to: Identify recursive methods, and understand the role and power of recursion Trace through recursive methods using tree diagrams that display all method calls and the values of the current parameters at each call Write your own recursive method in place of an iterative method . Lab Startup Change into your Labs directory, and let's create and change into a tabs directory. Now, let's copy over some files by typing: ep /pub/digh CSC205/Lab9/. Identifying Recursion Label each of the following methods as recursive or iterative. private static int fib (int n) f if (n0 11 (n1)) return 1: else return fib (n-1) + fib (n-2)): 2. private static int product (int n) t if (n0) return 1: else int x1: for (int i 1: i ni itt) x++i return x: 3 private static int count (intl] a, int key) int num " 0; for (int i-l; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
