Question

There is a Java program that is missing one recursive function: public class Fibonacci { /*...

There is a Java program that is missing one recursive function:

public class Fibonacci {
  /*          / 0                 when n = 0
   * fib(n) = | 1                 when n = 1
   *          \ fib(n-1)+fib(n-2) otherwise
   */
  public static int fib(int n) {
    return 0;
  }
  /* Fibonacci Test Framework
   *
   * Note, this takes a long time to compute fib(44).  
   */
  public static void main(String[] args) {
    int[] input   = { 11,    22,      33,        44};
    int[] expect  = { 89, 17711, 3524578, 701408733};
    boolean error = false;
    for(int i = 0 ; i < input.length; i++) {
      int answer = fib(input[i]);
      if(answer != expect[i]) {
        System.out.printf("ERROR: fib(%d) returned %d not %d.\n",
                          input[i], answer, expect[i]);
        error = true;
      }
    }

    if(error)
      System.exit(1);
    else
      System.out.println("Good Job!");
  }
}

Homework Answers

Answer #1

The recursive function to calculate the Fibonacci is given below. The comments are provided for easy understanding.

  public static int fib(int n) {
        if((n == 0) || (n == 1))
                // If n = 0 or n =1 return 0 and 1 respectively. This is the exit condition for the recursive function.
                return n;
        else
                //If n > 1, then recursively call the fib function using the formula.
                //Here fib(n-1) will call the same fib function but with n-1 as the parameter.
                //And the fib(n-2) will call the same fib function but with n-2 as the parameter.
                //Finally when the results are retrived both are added and returned to the main function.
                return fib(n-1) + fib(n-2);
  }

The complete program is given below.

public class Fibonacci {
  /*          / 0                 when n = 0
   * fib(n) = | 1                 when n = 1
   *          \ fib(n-1)+fib(n-2) otherwise
   */
  public static int fib(int n) {
        if((n == 0) || (n == 1))
                // If n = 0 or n =1 return 0 and 1 respectively. This is the exit condition for the recursive function.
                return n;
        else
                //If n > 1, then recursively call the fib function using the formula.
                //Here fib(n-1) will call the same fib function but with n-1 as the parameter.
                //And the fib(n-2) will call the same fib function but with n-2 as the parameter.
                //Finally when the results are retrived both are added and returned to the main function.
                return fib(n-1) + fib(n-2);
  }
  
  /* Fibonacci Test Framework
   *
   * Note, this takes a long time to compute fib(44).  
   */
  public static void main(String[] args) {
    int[] input   = { 11,    22,      33,        44};
    int[] expect  = { 89, 17711, 3524578, 701408733};
    boolean error = false;
    for(int i = 0 ; i < input.length; i++) {
      int answer = fib(input[i]);
      if(answer != expect[i]) {
        System.out.printf("ERROR: fib(%d) returned %d not %d.\n", input[i], answer, expect[i]);
        error = true;
      }
    }

    if(error)
      System.exit(1);
    else
      System.out.println("Good Job!");
  }
}

The below are the screen shots of the program and the output. The screenshot if the output is from Windows environment, but the output will be same if it is tried from a Linux / Unix environment.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I have the following program which returns F(n) for the fibonacci sequence both recursively and iteratively:...
I have the following program which returns F(n) for the fibonacci sequence both recursively and iteratively: public class Fibonacci { //Recursive method public int fibRecursive(int n) { //If n is in the 0th or 1st place return n if (n <= 1) return n; return fibRecursive(n - 1) + fibRecursive(n - 2); } //Iterative method public int fibIterative(int n) { if (n <= 1) return n; int fib = 1, prevFib = 1; for (int i = 2; i <...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Please find the BigO function with explanation for each of the programs below: - Recursive binary...
Please find the BigO function with explanation for each of the programs below: - Recursive binary search - Computing the factorial of N - Computing the N-th Fibonacci number. import java.util.Scanner; public class Recursive { public static void main(String args[]) { int arr[]= {7,9,12,15,28,57,92}; Scanner in = new Scanner(System.in); int key; System.out.print("Enter key to search in array : "); key = in.nextInt(); int index = binarySearch(arr,0,arr.length-1,key); if(index==-1)System.out.println(key + " is not found in array\n"); else System.out.println(key + " is found...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
Write a Java program that implements a lexical analyzer, lex, a recursive-descent parser, parse, an error...
Write a Java program that implements a lexical analyzer, lex, a recursive-descent parser, parse, an error handling program, error, and a code generator, codegen, for the following EBNF description of a simple assignment statement using an arithmetic expression language. Implement a symbol table by modifying Evaluate class. Modify the stmt() method, Provide an insert() and retrieve() method and store values in symbol table.   Video Help to get started:  https://www.youtube.com/watch?v=-C_S7Lb4kRk&feature=youtu.be import java.io.*; import java.util.*; public class parser { static String inString =...
I have a recursive Tower of Hanoi program but don't know how to include the count...
I have a recursive Tower of Hanoi program but don't know how to include the count variable. Could you tell me how? Main.java import java.io.*; import java.util.List; public class Main { //int count = 0; /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk.*/...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
C Programming 1. Provide an equivalent of the java class in C class Example { public...
C Programming 1. Provide an equivalent of the java class in C class Example { public static int[][] question4(int n) { int[][] result = new int [n][n]; for(int i=1; i<=n; i+=1) for (int j=1; j<=n; j+=1) result[i][j] = (i*n)+j; return result; } public static void main(String[] args) { int[][] my array = question4(4); } } 2. Rewrite the following function using no loops, and only tail call recursion int min (int n, int[] input) { int i; int result; for...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...