Menu

[Solved]Laboratory 14 1 Write Program Equivalent C14e1java Copy Recursion3 2 Following Method Retu Q37039015

#3
Laboratory 14 1. Write a program equivalent to C14e1.java (a copy of Recursion3 i 2. The following method returns the ith numLaboratory 14 1. Write a program equivalent to C14e1.java (a copy of Recursion3 i 2. The following method returns the ith number in the Fibonacci sequence: 1,1 implement numbersToOne using a while loop instead of recursion.14.5), 8, 13,. Each number after the first two is the sum of the two preceding numbu fib(0) returns 1; fib(1) returns 1; fib(2) returns 2, fb(3 returns 5, fib(5) returns 8, and so on. public static long fib(long i) turn if (i< 2) return 1; return fib(i 1)fib(i 2); Each time you call this method and pass it a value greater than or equal to 2, it calls it. self twice because it has two recursive calls (fib(i 1) andfib(i 2)).Asa result, the total number of calls can become excessive, making this method extraordinarily inefficient. Write a program that includes this method. Your program should prompt for and read in an integer. It should then call fib passing it the integer read. For what integer passed is the program’s run time 10 seconds? Double the integer passed and rerun your program. Does the run time also double? Again double the integer passed and rerun. What is the relation between the integer passed and run time? 3. Implement the fib method in Lab Exercise 2 using a loop instead of recursion. Per- form the same type of timing measurements that you performed in Lab Exercise 2. Is the loop implementation more efficient? 4. Rewrite the reverse method in Fig. 14.6. Reverse a string by placing its last character at the beginning of the reverse of the substring that contains the entire string except for its last character. Test your program 5. Rewrite the reverse method in Fig. 14.6. Use the reverse method in the String- Buffer class. 6. Write a method that does multiplication on nonnegative integers using recursion Test your method by using it to compute the product of 0 and 0, the product of 0 and 5, the product of 5 and 0, and the product of 5 and 10. Hint: ахь ax(b 1) + a for b > 0 for b 0 Write a recursive method that returns the number of occurrences of A’ in the it is passed. Test your method with the following strings ·いA.,’B”, “BCA”,”ABC and “ABACAD Show transcribed image text Laboratory 14 1. Write a program equivalent to C14e1.java (a copy of Recursion3 i 2. The following method returns the ith number in the Fibonacci sequence: 1,1 implement numbersToOne using a while loop instead of recursion.14.5), 8, 13,. Each number after the first two is the sum of the two preceding numbu fib(0) returns 1; fib(1) returns 1; fib(2) returns 2, fb(3 returns 5, fib(5) returns 8, and so on. public static long fib(long i) turn if (i 0 for b 0 Write a recursive method that returns the number of occurrences of A’ in the it is passed. Test your method with the following strings ·いA.,’B”, “BCA”,”ABC and “ABACAD

Expert Answer


Answer to Laboratory 14 1. Write a program equivalent to C14e1.java (a copy of Recursion3 i 2. The following method returns the it… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *