CPS 151 Self-tracing recursive methods (functions) In class we looked at having a recursive method trace itself to show the calls being made and the values being returned from each call (with appropriate indentation). Your task is to add this capability to a recursive function calculating Fibonacci numbers. We can add a parameter to a recursive function to tell it what its depth of recursion is. The top level call is at depth (or level) 0. When a function executing at level k calls itself, that function call is at level k + 1. The trace messages are written to the standard output with an indentation proportional to the call level. I have given an example adding the self-tracing feature to a function computing n! (n factorial). See RecFact.java posted on Isidore. You are also provided with the code for a method computing the Fibonacci numbers. Your task is to modify the Fibonacci number computing program to add the self-tracing feature. See how it was done for the factorial function, and do the same for the Fibonacci function.

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter8: Advanced Method Concepts
Section: Chapter Questions
Problem 8RQ
icon
Related questions
Question

Java code:

 final static Scanner cin = new Scanner(System.in); public static void main(String[] args) { out.println("CPS 151 In-class assignment 3 by ___ your name here ___\n"); out.print("Enter n: "); int n = cin.nextInt(); long f = fib(n); out.println("\nfib(" + n + ") = " + f); out.println("\nCPS 151 In-class assignment 3 complete"); } // end main // standard fibonacci number computation using recursion static long fib(int n) { if (n < 0) terminate("Negative argument value not allowed, program ended"); if (n <= 1) return n; else return fib(n - 1) + fib (n - 2); } // end fib private static void indent(int level) { for(int Lcv = 1; Lcv <= level; Lcv++) out.print(" "); } // end indent private static void terminate(String errorMessage) { out.println(errorMessage); System.exit(1); } // end terminate

Sample output from the finished program follows:
unor
Output - ICA03_KEY (run) X
A
D
CPS 151 In-class assignment 3 by
Enter n: 5
Entering fib: n = 5
Entering fib: n = 4
Entering fib: n = 3
Entering fib: n = 2
Entering fib: n = 1
Exiting fib: n = 1 return value = 1
Entering fib: n = 0
Exiting fib: n = 0 return value = 0
Exiting fib: n = 2 return value = 1
Entering fib: n = 1
Exiting fib: n = 1 return value = 1
Exiting fib: n = 3 return value = 2
Entering fib: n = 2
Entering fib: n = 1
Exiting fib: n = 1 return value = 1
Entering fib: n = 0
Exiting fib: n = 0 return value - 0
Exiting fib: n = 2 return value = 1
Exiting fib: n = 4 return value - 3
Entering fib: n = 3
Entering fib: n = 2
KEY
Entering fib: n = 1
Exiting fib: n = 1 return value
Entering fib: n = 0
Exiting fib: n = 0 return value = 0
1
Exiting fib: n 2 return value
Entering fib: n = 1
Exiting fib: n = 1 return value = 1
Exiting fib: n = 3 return value = 2
Exiting fib: n = 5 return value = 5
fib (5) = 5
w
Sater
CPS 151 In-class assignment 3 complete
BUILD SUCCESSFUL (total time: 11 seconds)
X
Transcribed Image Text:Sample output from the finished program follows: unor Output - ICA03_KEY (run) X A D CPS 151 In-class assignment 3 by Enter n: 5 Entering fib: n = 5 Entering fib: n = 4 Entering fib: n = 3 Entering fib: n = 2 Entering fib: n = 1 Exiting fib: n = 1 return value = 1 Entering fib: n = 0 Exiting fib: n = 0 return value = 0 Exiting fib: n = 2 return value = 1 Entering fib: n = 1 Exiting fib: n = 1 return value = 1 Exiting fib: n = 3 return value = 2 Entering fib: n = 2 Entering fib: n = 1 Exiting fib: n = 1 return value = 1 Entering fib: n = 0 Exiting fib: n = 0 return value - 0 Exiting fib: n = 2 return value = 1 Exiting fib: n = 4 return value - 3 Entering fib: n = 3 Entering fib: n = 2 KEY Entering fib: n = 1 Exiting fib: n = 1 return value Entering fib: n = 0 Exiting fib: n = 0 return value = 0 1 Exiting fib: n 2 return value Entering fib: n = 1 Exiting fib: n = 1 return value = 1 Exiting fib: n = 3 return value = 2 Exiting fib: n = 5 return value = 5 fib (5) = 5 w Sater CPS 151 In-class assignment 3 complete BUILD SUCCESSFUL (total time: 11 seconds) X
CPS 151
B
In-class Assignment 3
+ + 80%
1 / 2
2022-09-08
Self-tracing recursive methods (functions)
In class we looked at having a recursive method trace itself to show the calls being made and the values being returned
from each call (with appropriate indentation). Your task is to add this capability to a recursive function calculating
Fibonacci numbers.
We can add a parameter to a recursive function to tell it what its depth of recursion is. The top level call is at depth (or
level) 0. When a function executing at level k calls itself, that function call is at level k + 1. The trace messages are
written to the standard output with an indentation proportional to the call level.
I have given an example adding the self-tracing feature to a function computing n! (n factorial). See RecFact.java posted
on Isidore. You are also provided with the code for a method computing the Fibonacci numbers.
Your task is to modify the Fibonacci number computing program to add the self-tracing feature. See how it was done for
the factorial function, and do the same for the Fibonacci function.
Note: Do not forget to have your name as comments in the Java source code. You should also edit the output
identification line at the beginning of the main method to include your name.
Submit the source code and screen shot in one document as before.
Show the results for 5 as the input (as in the sample output).
Note: Each function entry message should show the value of the parameter n. Each function exit message should show
the value of the parameter n, and the value of the function being returned, again suitably indented.
Note: Both recursive calls within the code for function fib (with arguments n-1 and n-2) are made at the same recursion
level (recursion depth) and it is 1 more than the level at function entry.
Transcribed Image Text:CPS 151 B In-class Assignment 3 + + 80% 1 / 2 2022-09-08 Self-tracing recursive methods (functions) In class we looked at having a recursive method trace itself to show the calls being made and the values being returned from each call (with appropriate indentation). Your task is to add this capability to a recursive function calculating Fibonacci numbers. We can add a parameter to a recursive function to tell it what its depth of recursion is. The top level call is at depth (or level) 0. When a function executing at level k calls itself, that function call is at level k + 1. The trace messages are written to the standard output with an indentation proportional to the call level. I have given an example adding the self-tracing feature to a function computing n! (n factorial). See RecFact.java posted on Isidore. You are also provided with the code for a method computing the Fibonacci numbers. Your task is to modify the Fibonacci number computing program to add the self-tracing feature. See how it was done for the factorial function, and do the same for the Fibonacci function. Note: Do not forget to have your name as comments in the Java source code. You should also edit the output identification line at the beginning of the main method to include your name. Submit the source code and screen shot in one document as before. Show the results for 5 as the input (as in the sample output). Note: Each function entry message should show the value of the parameter n. Each function exit message should show the value of the parameter n, and the value of the function being returned, again suitably indented. Note: Both recursive calls within the code for function fib (with arguments n-1 and n-2) are made at the same recursion level (recursion depth) and it is 1 more than the level at function entry.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning