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.
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,...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class BankCharges { public static void main(String[] args) { //Variable declarations int numChecks; double perCheckFee; double totalFee; double totalFeesAllAccounts = 0; //accumulator int numAccounts;    //Constant declarations for base fee and per check fees final double BASE_FEE = 10.0; final double LESS_THAN_20_FEE = 0.10; final double TWENTY_TO_THIRTYNINE_FEE = 0.08; final double FORTY_TO_FIFTYNINE_FEE = 0.06; final double SIXTY_OR_MORE_FEE = 0.04;    // Create a Scanner...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;        for (int i = 2; i < n; i++){          if (n % i == 0)             return false; }        return true; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT