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

import recursion.Recursion; // Uncomment this block of code to test Activity 4 /

ID: 3883753 • Letter: I

Question

import recursion.Recursion; // Uncomment this block of code to test Activity 4 /* System.out.println(" Activity 4:"); // Test factorial System.out.println("Factorial:"); for (int ii = 1; ii<10; ii ++) { System.out.print(Recursion.fact(ii) + " "); } // Test fibonacci System.out.println(" Fibonacci:"); for (int ii = 1; ii<10; ii ++) { System.out.print(Recursion.fib(ii) + " "); } // Test Euclid's GCD algorithm System.out.println(" GCD:"); System.out.println ("GCD of 96 and 60 is " + Recursion.gcd(96, 60)); System.out.println ("GCD of 30 and 10 is " + Recursion.gcd(30, 10)); System.out.println ("GCD of 96 and 120 is " + Recursion.gcd(96, 120)); // Uncomment out this block of code to test Project 4 System.out.println(" Test Project 4:"); System.out.println("Power:"); for (int ii = 1; ii<4; ii ++) { for (int jj = 0; jj<6; jj ++) { System.out.print(ii + "^" + jj + "=" + Recursion.power(ii,jj) + " "); } System.out.println(); } */ 1) Download the source code from https:/lgithub.com/CGCC-CS/205activity4.git. The source code has a driver class called Activity4.java that calls the methods below. Uncomment the test code. Your recursive methods should work with the test driver without making any changes. 2) Write a recursive Java method fact (n) to find n! 3) Write a recursive Java method fib (n) that finds the nth Fibonacci number 4) Write a recursive method that gcd (num1, num2) implements the GCD algorithm defined below. You should not implement any other GCD algorithm. gcd(num1, num2) = num2 if num2

Explanation / Answer

class Recursion

{

public static int gcd(int num1,int num2)

{

if(num2 <= num1 && num1%num2 == 0)

return num2;

else if( num1 < num2)

return gcd(num2,num1);

else

return gcd(num2, num1%num2); //recursive call until n2 = 0

}

public static int fact(int n)

{

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

return n*fact(n-1);//recursive calls   

}

public static int fib(int n)

{

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

return fib(n-1)+fib(n-2);//recursive calls

}

public static int power(int x,int n)

{

if(x == 0 )

return 0;

else if(n==0)

return 1;

else if(n == 1)

return x;

else

return x*power(x,n-1);

}

public static void main (String[] args)

{

// Uncomment this block of code to test Activity 4

System.out.println(" Activity 4:");

// Test factorial

System.out.println("Factorial:");

for (int ii = 1; ii<10; ii ++)

{

System.out.print(Recursion.fact(ii) + " ");

}

// Test fibonacci

System.out.println(" Fibonacci:");

for (int ii = 1; ii<10; ii ++)

{

System.out.print(Recursion.fib(ii) + " ");

}

// Test Euclid's GCD algorithm

System.out.println(" GCD:");

System.out.println ("GCD of 96 and 60 is " + Recursion.gcd(96, 60));

System.out.println ("GCD of 30 and 10 is " + Recursion.gcd(30, 10));

System.out.println ("GCD of 96 and 120 is " + Recursion.gcd(96, 120));

// Uncomment out this block of code to test Project 4

System.out.println(" Test Project 4:");

System.out.println("Power:");

for (int ii = 1; ii<4; ii ++) {

for (int jj = 0; jj<6; jj ++) {

System.out.print(ii + "^" + jj + "=" + Recursion.power(ii,jj) + " ");

}

System.out.println();

}

}

}

Output:

Activity 4:
Factorial:
1 2 6 24 120 720 5040 40320 362880

Fibonacci:
1 1 2 3 5 8 13 21 34

GCD:
GCD of 96 and 60 is 12
GCD of 30 and 10 is 10
GCD of 96 and 120 is 24


Test Project 4:
Power:
1^0=1 1^1=1 1^2=1 1^3=1 1^4=1 1^5=1
2^0=1 2^1=2 2^2=4 2^3=8 2^4=16 2^5=32
3^0=1 3^1=3 3^2=9 3^3=27 3^4=81 3^5=243