Question

In: Computer Science

Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call...

Create a class called FibGenerator with 3 methods:

  1. public int nthFib(int n). This method should call computeFibRecurse(n).
  2. private int computeFibRecurse(int n), which should recurse (that is, call itself) unless n is 1 or 2. If n is 1 or 2, the method should return 1.
  3. A main method that prints “STARTING”, then constructs a FibGenerator generator and then calls nthFib(), passing in interesting values.

To look into this problem, you’re going to use software to analyze software. Add an instance variable: private int[] callCounter. In nthFib(n), initialize callCounter so that its length is n+1. In computeFibRecurse(n), increment callCounter[n]. Add a printCallCounter() method that prints like this:

0 calls to fib(0)

2584 calls to fib(1)

4181 calls to fib(2)

2584 calls to fib(3)

1597 calls to fib(4)

How would I frame this argument? My current code is:

package lab5workspace;

public class FibGenerator {

public int nthFib(int n) {

return computeFibRecurse(n);

}

private int computeFibRecurse(int n) {

if (n==1 || n== 2) {

return 1;

}

private int callCounter(int x) {

callCounter(x+1);

}

int result = computeFibRecurse(n-1) + computeFibRecurse(n-2);

System.out.println("The value is " + result);

return result;

}

public static void main(String[]args) {

System.out.println("starting");

FibGenerator fg = new FibGenerator();

int result = fg.nthFib(20);

}

}

Solutions

Expert Solution

FibGenerator.java

public class FibGenerator {

    private int[] callCounter;

    private int computeFibRecurse(int n) {
        callCounter[n]++;
        if (n == 1 || n == 2) {
            return 1;
        }
        return computeFibRecurse(n - 1) + computeFibRecurse(n - 2);
    }

    private void printCallCounter() {
        for (int i = 0; i < callCounter.length; i++) {
            System.out.println(callCounter[i] + " calls to fib(" + i + ")");
        }
    }

    public int nthFib(int n) {
        callCounter = new int[n + 1];
        int nfib = computeFibRecurse(n);
        printCallCounter();
        return nfib;
    }

    public static void main(String[] args) {
        System.out.println("STARTING");
        FibGenerator fg = new FibGenerator();
        int result = fg.nthFib(20);
    }
}

Output:

STARTING
0 calls to fib(0)
2584 calls to fib(1)
4181 calls to fib(2)
2584 calls to fib(3)
1597 calls to fib(4)
987 calls to fib(5)
610 calls to fib(6)
377 calls to fib(7)
233 calls to fib(8)
144 calls to fib(9)
89 calls to fib(10)
55 calls to fib(11)
34 calls to fib(12)
21 calls to fib(13)
13 calls to fib(14)
8 calls to fib(15)
5 calls to fib(16)
3 calls to fib(17)
2 calls to fib(18)
1 calls to fib(19)
1 calls to fib(20)

Process finished with exit code 0

Related Solutions

In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use next(), and should not use this._array.forEach! Take note of what should happen if you call forEach twice on the same Iterator instance. The estimated output is commented below In javascript please class Iterator { constructor(arr){ this[Symbol.iterator] = () => this this._array = arr this.index = 0 } next(){ const done = this.index >= this._array.length const value = this._array[this.index++] return {done, value} } //YOUR WORK...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Create a class called FileSumWrapper with a method that has the signature public static void handle(String...
Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound). Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. I'm unsure at how to incorporate the FileSum.read() method to even start anything. I also don't know...
for java Welcome to a classic homework problem! Create a public class called Last8. You should...
for java Welcome to a classic homework problem! Create a public class called Last8. You should exposed two public methods: add: adds a value, does not return a value last: returns an array containing the last 8 values that were added, in any order. You do not need a constructor, but you can add an empty one if you need. Until 8 values have been added you should return 0s in their place. For example, here's how an instance of...
You are to create a class called Person. You should create the definition of the class...
You are to create a class called Person. You should create the definition of the class Person in a file person.h and the functions for Person in person.cpp. You will also create a main program to be housed in personmain.cpp. A Person will have attributes of Height (in inches) Weight (in pounds to 2 decimal places) Name (string) Gender (‘m’ or ‘f’) Ethnicity/Race (could be more than one word) Occupation (also more than a single word) A Person will have...
Design a class named ArraySort and it contains the following method: Public int search(int target): if...
Design a class named ArraySort and it contains the following method: Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1; Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1; Public int minimum(): Return the minimum number in the array if the array is nonempty,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT