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

Java Programming 3-3: Recursion Practice Activities Lesson Obiectives: Create li

ID: 3851980 • Letter: J

Question

Java Programming 3-3: Recursion Practice Activities Lesson Obiectives: Create linear recursive methods Create non-linear recursive methods Compare the pros and cons of recursion Vocabulary: Identify the vocabulary word for each definition below Is the process of recursively calling a copy of the same method until a base case is reached In some cases, like the Fibonacci problem, two values may trigger alternate base cases that return values in non-linear recursion. In non-linear recursion, the base case may need to accommodate multiple return values Is the process of calling one and only one copy of the same method within a method Is the process of calling two or more copies of the same method within a method. As a rule, the calls are separated by an operator in an algorithm Is the process of backing into a problem by recursively calling a copy of the method until you arrive at a base case, and then returning the values up the chain to resolve a problem The last case processed by a recursive program, which may also be processed for the last couple values. This is true when resolving a Fibonacci sequence for the first two values of the sequence. They are the last values calculated when recursively resolving the problenm Is the alternative to the base case, and run when the base case criteria isn't met. It runs when the program needs to call yet another copy or set of copies of itself to resolve a problem

Explanation / Answer

Here is the code for the question. Output is also shown. Please rate the answer if it helped. Thank you very much.

Question 1

public class Linear {

   public static double factorial(double n)

   {

       if(n <= 1)

           return 1;

       else

           return n * factorial(n-1);

   }

  

   public static void main(String[] args) {

       double d;

       if(args.length == 0) //check for command line argument

           d = 5;

       else

           d = Double.parseDouble(args[0]);

       System.out.printf("Factorial [%.1f] of [%.1f] ", factorial(d), d); //%.1f formats to 1 decimal place

   }

}

output (passing 7 as command line arugment)

Factorial [5040.0] of [7.0]

Question 2:

public class NonLinear {

   public static double fibonacci(double d)

   {

       if(d < 2)

       {

          

           if(d == 1)

               return 1;

           else

               return 0;

       }

       else

           return fibonacci(d - 1) + fibonacci(d - 2);

   }

   public static void main(String[] args) {

       double d;

       if(args.length == 0) //check for command line argument

           d = 5;

       else

           d = Double.parseDouble(args[0]);

      

       for(float i = 0; i < d; i++)

       {

           System.out.printf(" Fibonacci index [%.1f] value [%.1f] ", i,fibonacci(i));

       }

   }

}

output

Fibonacci index [0.0] value [0.0]

Fibonacci index [1.0] value [1.0]

Fibonacci index [2.0] value [1.0]

Fibonacci index [3.0] value [2.0]

Fibonacci index [4.0] value [3.0]

Question 3

factorial(7) = 7 * factorial (6)   
= 7 * 6 * factorial(5)
= 7 * 6 * 5 * factorial(4)
= 7 * 6 * 5 * 4 * factorial(3)
= 7 * 6 * 5 * 4 * 3 * factorial(2)
= 7 * 6 * 5 * 4 * 3 * 2 * factorial(1) //base case
= 7 * 6 * 5 * 4 * 3 * 2 * 1
= 5040

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote