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

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
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...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
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