package recursion; /** * * @author Mayelin */ public class Recursion { public static void main(String[] args) { int value = methodOne(4); System.out.println("The value returned by methodOne is: " + value + "\n"); message(4); System.out.println("Factorial of 5 is: " + factorial(5) + "\n"); System.out.println("Factorial of 5 is: " + factorialWithLoop(5) + "\n"); int[] arrayOfInts = {5, 7, 5, 6, 4, 2, 3, 8, 4, 5, 2, 1, 8}; System.out.println("The sum for the index range 2 - 6 is: " + rangeSum(arrayOfInts, 2, 6) + "\n"); System.out.println("The sum for the index range 2 - 5 is: " + rangeSum2(arrayOfInts, 2, 5) + "\n"); System.out.println("The 8th number in the Fibonacci series is: " + fib(8) + "\n"); /* boolean result = isPalindrome("Madam, I'm Adam"); System.out.println("Is the phrase \"Madam, I'm Adam\" a palindrome? " + result); result = isPalindrome("A man, a plan, a canal—Panama!"); System.out.println("Is the phrase \"A man, a plan, a canal—Panama!\" a palindrome? " + result); System.out.println("The phrase \"Hello World\" reversed is: " + stringReverser("Hello World")); System.out.println("The phrase \"srats\" reversed is: " + stringReverser("srats")); */ } public static int methodOne(int n) { System.out.println("The value of the n parameter in methodOne is: " + n); return methodTwo(n + 1) * 2; } public static int methodTwo(int n) { System.out.println("The value of the n parameter in methodTwo is: " + n); return n * 4; } public static void message(int n) { if (n > 0) { System.out.println("This is a recursive method."); message(n - 1); } } public static int factorial(int n) { //System.out.println("The value of n is: " + n); if (n <= 1) return 1; else return n * factorial(n - 1); } public static int factorialWithLoop(int n) { int result = 1; for (int i = n; i > 0; i--) { result *= i; } return result; } public static int rangeSum(int[] array, int start, int end) { if (start > end) return 0; else return array[start] + rangeSum(array, start + 1, end); } public static int rangeSum2(int[] array, int start, int end) { if (start == end) return array[start]; else if (start > end) return 0; else return array[start] + rangeSum2(array, start + 1, end - 1) + array[end]; } /* The Fibonacci Series - after the second number, each number is the sum of the two previous numbers. 0 1 1 2 3 5 8 13 21 ... This method calculates the nth number in the series. */ public static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n - 1) + fib(n - 2); } public static boolean isPalindrome(String text) { if( text == null ) return false; int length = text.length(); // Separate case for shortest strings. if (length <= 1) return true; // Get first and last characters, converted to lowercase. char first = Character.toLowerCase(text.charAt(0)); char last = Character.toLowerCase(text.charAt(length - 1)); String shorterText; if (Character.isLetter(first) && Character.isLetter(last)) { // Both are letters. if (first == last) { // Remove both first and last character. shorterText = text.substring(1, length - 1); return isPalindrome(shorterText); } else { return false; } } else if (!Character.isLetter(last)) { // Remove last character. shorterText = text.substring(0, length - 1); return isPalindrome(shorterText); } else { // Remove first character. shorterText = text.substring(1); return isPalindrome(shorterText); } } public static String stringReverser(String text) { if (text.length() <= 1) { return text; } else { return text.substring(text.length() - 1) + stringReverser(text.substring(0, text.length() - 1)); } } }