Question
Recursion (Please solve on Java netbeans 8.1 or 8.2) I solve parts of this problem ( which is between 1-5) Solve from 6 Create the
Recursion (Please solve on Java netbeans 8.1 or 8.2)
I solve parts of this problem( which is between 1-5)
Solve from 6
Create the following methods and test them using a TestRecursion class
1. A recursive method to draw rectangles like just like in the figure below.
2. A recursive method to calculate the factorial of a number
3. A recursive method to calculate the power of a number raised to another number ( power(x,n) is xn )
4. A recursive method to reverse a string.
5. A recursive method Vowels (st) that returns the number of vowels in a string st.
6. A recursive method to compute numbers in the Fibonacci sequence. This infinite sequence starts with 0 and 1,
which we'll think of as the zeroth and first Fibonacci numbers, and each succeeding number is the sum of the
two preceding Fibonacci numbers. Thus, the second number is 0 + 1 = 1. And to get the third Fibonacci
number, we'd sum the first (1) and the second (1) to get 2. And the fourth is the sum of the second (1) and the
third (2), which is 3. And so on.
n: 0 1 2 3 4 5 6 7 8 9 10 11
nth Fibonacci: 0 1 1 2 3 5 8 13 21 34 55 89
7. A recursive method MaxR() to find the maximum of a numbers list ( Array, ArrayList and Linked List).
8. A recursive method SumR() to find the sum of a numbers list ( Array, ArrayList and Linked List).
9. A recursive method to convert from decimal to binary.
10. A recursive algorithm to multiply two positive integers m and n using repeated addition. Specify the
base case and the recursive case.
11. A recursive method to determine if a string is a Palindrome or not. (A Palindrome is a string that is the
same when read forwards or backwards after removing spaces).
Examples (Madam I m Adam) (Race car).
12. A method EX(x, n) to calculate the value of ex = 1 + (x / 1!) + (x2 / 2!) + (x3 / 3!) + + (xn / n!), Where n
and x are supplied by the user. You must call 2 recursive methods (1 for factorial and another one for
power (do not use pow method available in java).
---------------------------------------------------------------------------------------------------------
here is my code
package Problem;
public class Problem {
public static int factorial(int n) { if (n == 1) { // when to stop return 1; } return n * factorial(n - 1); // small operation + repeat }
public static int power(int x, int n) { if (n == 1) { return x; } return x * power(x, n - 1); }
public static String reverse(String s) { if (s.length() == 0) { return ""; } char c = s.charAt(s.length() - 1); return c + reverse(s.substring(0, s.length() - 1));
}
public static int vowels(String str) {
if (str.length() == 0) { return 0; }
if (isVowel(str.charAt(0))) { return 1 + vowels(str.substring(1)); } else { return 0 + vowels(str.substring(1)); }
}
public static boolean isVowel(char c) {
if (c == 'u' || c == 'a' || c == 'e' || c == 'i' || c == 'o') { return true;
} return false; }
public static void main(String[] args) { System.out.println("Factorial of 3 is " + factorial(3)); System.out.println("2^3 is " + power(2, 3)); System.out.println("the reverse of abcd " + reverse("ABCD")); System.out.println("Number of vowels in uaeuni " + vowels("uaeuni")); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started