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
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....
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 =...
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...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
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.*/...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
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); } }
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...