Advertisement
binibiningtinamoran

RecursionReview

Jun 3rd, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class RecursionReview {
  5.  
  6.     public static void main(String []args) {
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         //System.out.println(multiply(2,5));
  10.         //String word = "Charlie";
  11.         //System.out.println(word.substring(1));
  12.         //System.out.println(reverseString("Charlie"));
  13.         //int[] sample = {1,2,33,44,55,900};
  14.         //System.out.println(maxElement(sample));
  15.  
  16.         //char[] charArray = {'a','b','c','d','d','d','d','d','d'};
  17.         //System.out.printf("No. of '%c' in array: %,d", 'd',charCount(charArray,'d'));
  18.  
  19.         //System.out.print("Enter a positive number: ");
  20.         //int input = scan.nextInt();
  21.         //System.out.println("Recursive sum of " + input + ": " + recursiveSum(input));
  22.         //System.out.println(recursivePower(4,3));
  23.  
  24.         //String word = "Vince";
  25.         //System.out.println(displayChar(word));
  26.  
  27.         int[] s = new int[]{1,2,300,-4,15};
  28.         reverseArray(s);
  29.         System.out.println(Arrays.toString(s));
  30.  
  31.     }
  32.  
  33.     public static int multiply(int x, int y) {
  34.         if (y == 1) {
  35.             return x;
  36.         } else {
  37.             return x + multiply(x, y - 1);
  38.         }
  39.     }
  40.  
  41.  
  42.     public static String reverseString(String str) {
  43.         if ((null == str) || (str.length() <= 1)) {
  44.             return str;
  45.         } else {
  46.             return reverseString(str.substring(1)) + str.charAt(0);
  47.         }
  48.     }
  49.  
  50.     public static int maxElement(int[] arr) {
  51.         // Iterative version
  52.         /*int max = 0;
  53.         boolean foundMax = false;
  54.         for (int i = 0; i < arr.length; i++) {
  55.             if (arr[i] > max) {
  56.                 max = arr[i];
  57.             }
  58.         }
  59.         return max; */
  60.         return maxElementHelper(arr, arr.length-1);
  61.     }
  62.  
  63.     private static int maxElementHelper(int[] arr, int index) {
  64.         if (index > 0) {
  65.             return Math.max(arr[index], maxElementHelper(arr, index-1));
  66.         } else {
  67.             return arr[0];
  68.         }
  69.     }
  70.  
  71.     // Write a method that uses recursion to count the number of times a specific character
  72.     // occurs in an array of characters. Demonstrate the method in a program.
  73.     public static int charCount(char[] arr, char given) {
  74.         return charCountHelper(arr,arr.length-1, given);
  75.     }
  76.  
  77.     private static int charCountHelper(char[] arr, int index, char given) {
  78.         if (arr[index] == given ) {
  79.             return 1 + charCountHelper(arr,index-1, given);
  80.         } else {
  81.             return 0;
  82.         }
  83.     }
  84.     // Write a method that accepts a String as an argument. The method should use recursion
  85.     // to display each individual character in the String.
  86.     public static String displayChar(String word) {
  87.         return displayCharHelper(word);
  88.     }
  89.     private static String displayCharHelper(String word) {
  90.         if (word.length() == 1) {
  91.             return word;
  92.         } else {
  93.             return (word.charAt(0) + "\n" + displayCharHelper(word.substring(1)));
  94.         }
  95.     }
  96.  
  97.     public static int recursiveSum(int num) {
  98.         if (num == 1) {
  99.             return 1;
  100.         } else {
  101.             return num + recursiveSum(num-1);
  102.         }
  103.     }
  104.  
  105.     public static int recursivePower(int base, int exp) {
  106.         if (exp == 1) {
  107.             return base;
  108.         } else {
  109.             return base * (recursivePower(base,exp-1));
  110.         }
  111.  
  112.     }
  113.  
  114.     public static void reverseArray(int[] array) {
  115.         reverseArrayHelper(array, 0, array.length-1);
  116.     }
  117.     private static void reverseArrayHelper(int[] array, int x, int y) {
  118.         if (x < y) {
  119.             int tmp = array[x];
  120.             array[x] = array[y];
  121.             array[y] = tmp;
  122.             reverseArrayHelper(array, x+1, y-1);
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement