Question

Recursion Intro Assignment. YOU HAVE TO DO IT USING "HELPER METHOD" AND NO IMPORTS ARE ALLOWED. You may not use for or while. Any looping must be accomplished using recursion. You may not declare static or non-static member fields – only local or parameter variables allowed. Thanks!

Implement the method public static int fibby (int n). fibby is mathematically defined for nonnegative values in the following way: fibby(0)1 fibby(n) - fibby(ln/4]) fibby(13n/4]) where n > 0 and x means the floor of x (round down) HINT: recall Javas integer division behavior. Table of examples: 4 7 10 20 100 Part 2b. Sparse table generation (20 pts) Notice that for many values i, fibby(i) - fibby(i+1). Implement the method public static void printsparsetable(int start, int end). Output using System.out.println all consecutive values of n and fibby(n) (just the two numeric values separated by a space) where n 2 start andn s end However, skip a line of output if the previous row printed has the same fibby value

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//This method returns the value fibby(n) for n >= 0 using recursion

//It returns -1 for n < 0

public static int fibby(int n)

{

       //Return -1 if n is negative

       if(n < 0)

             return -1;

       //Return 1 for fibby(0)

       if(n == 0)

             return 1;

       //Otherwise return fibby(n / 4) + fibby(3n / 4) with

       //floor values of n / 4 and 3n / 4

       return fibby((int)(n / 4)) + fibby((int)(3 * n / 4));

}

//This method is a helper method for printsparsetable() method

//It prints the pairs n, fibby(n) which are unique except the very first pair

//from start to end

//Example, it prints 6, 8; 8, 11 for printsparsetable(5, 10)

//5, 6 is printed by printsparsetable()

public static void printremainingsparsetable(int start, int end)

{

       //Stop the recursion if start crossed end

       if(start <= end)

       {

             //Print the n, fibby(n) pair if fibby(n) == fibby(n - 1)

             if(fibby(start) != fibby(start - 1))

                    System.out.println(start + " " + fibby(start));

             //Again call printremainingsparsetable() for range start + 1 to end

             printremainingsparsetable(start + 1, end);

       }

}

//This method prints the first n, fibby(n) pair

//and then calls printremainingsparsetable() for range start + 1 to end

//Example, it prints 5, 6 for printsparsetable(5, 10)

public static void printsparsetable(int start, int end)

{

       //Proceed only if start and end values are valid

       if(start <= end)

       {

             //Print the first pair

             System.out.println(start + " " + fibby(start));

             //Call printremainingsparsetable() for the remaining pairs

             printremainingsparsetable(start + 1, end);

       }

}

The ouputs of the sample data given in the question are verified and the functions are functioning without any errors.

public static void main(String[] args)

{

       for(int i = 1; i <= 10; i++)

             System.out.println(i + " " + fibby(i));

       System.out.println("20 " + fibby(20));

       System.out.println("100 " + fibby(100));

}

Output:

1 2
2 3
3 4
4 6
5 6
6 8
7 8
8 11
9 11
10 11
20 24
100 111

public static void main(String[] args)

{

       printsparsetable(5, 10);

}

Output:

5 6
6 8
8 11

Kindly give a thumbs up, if found useful. Comment for queries. :)

Add a comment
Know the answer?
Add Answer to:
Recursion Intro Assignment. YOU HAVE TO DO IT USING "HELPER METHOD" AND NO IMPORTS ARE ALLOWED....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...

    What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • I currently have this but it doesn't work for the three item arrays public class Quicksort...

    I currently have this but it doesn't work for the three item arrays public class Quicksort extends Partitioner { public static void quicksort(int[] values) { if (values == null || values.length < 2) { return; } quicksort(values, 0, values.length - 1); } private static void quicksort(int[] values, int start, int end) { System.out.println(values); System.out.println(start); System.out.println(end); if (values == null || Math.abs(start - end) == 1) { return; } if (end > start) { int pivotValueIndex = partition(values, start, end); quicksort(values,...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • I just need to add comment for the code blow. Pleas add comment for each method...

    I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. *    * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT