Menu

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

#4
Laboratory 14 1. Write a program equivalent to C14e1.java (a copy of Recursion3 i 2. The following method returns the ith numlmethod, reverse, that reverses a string, The or i1 is just the given string. Thus, if we pass reverse ld simply retur reversLaboratory 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.2a 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 pr fib(0) returns 1; fib(1) returns 1; fib(2) returns 2, fib(3) return returns 5, fib(5) returns 8, and so on. public static long fib(long i) receding numbers s 3, fib(4) 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: axb ax (b 1)+ a for b> 0 axb0 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 and “ABACAD “, “BCA,”ABC lmethod, reverse, that reverses a string, The or i1 is just the given string. Thus, if we pass reverse ld simply retur reverse lll is 0 ul is o or 1, reverse should simply return it: I s.length)s 1 it:r we pass return s; Otherwise, we return the following tail reverse (s.substring(1, s.length)8.substringto,1) tail reversed by recursive call first character This complicated expression represents the st the recursive call con of the reverse method is in Fig. 14.6 ring consisting of the tail of s reversed by catenated to the first character of s. The complete implementation 1 class Recursion4 public static void main(Stringt] args) 3 II null string System.out.println(reverse(“*)); System.out.printin(reverse(“A)) I1 1-char string System.out.printin(reverse(ABCDEF))I multi-char 10 public static String reverse (String s) 12 if (s.length) 1) 13 14 15 16 return s; return reverse(s.substring(1, s.length()))+.substring(0, 1) Figure 14.6 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.2a 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 pr fib(0) returns 1; fib(1) returns 1; fib(2) returns 2, fib(3) return returns 5, fib(5) returns 8, and so on. public static long fib(long i) receding numbers s 3, fib(4) if (i 0 axb0 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 and “ABACAD “, “BCA,”ABC
lmethod, reverse, that reverses a string, The or i1 is just the given string. Thus, if we pass reverse ld simply retur reverse lll is 0 ul is o or 1, reverse should simply return it: I s.length)s 1 it:r we pass return s; Otherwise, we return the following tail reverse (s.substring(1, s.length)8.substringto,1) tail reversed by recursive call first character This complicated expression represents the st the recursive call con of the reverse method is in Fig. 14.6 ring consisting of the tail of s reversed by catenated to the first character of s. The complete implementation 1 class Recursion4 public static void main(Stringt] args) 3 II null string System.out.println(reverse(“*)); System.out.printin(reverse(“A)) I1 1-char string System.out.printin(reverse(ABCDEF))I multi-char 10 public static String reverse (String s) 12 if (s.length) 1) 13 14 15 16 return s; return reverse(s.substring(1, s.length()))+.substring(0, 1) Figure 14.6

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 *