Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class RecursionReview {
- public static void main(String []args) {
- Scanner scan = new Scanner(System.in);
- //System.out.println(multiply(2,5));
- //String word = "Charlie";
- //System.out.println(word.substring(1));
- //System.out.println(reverseString("Charlie"));
- //int[] sample = {1,2,33,44,55,900};
- //System.out.println(maxElement(sample));
- //char[] charArray = {'a','b','c','d','d','d','d','d','d'};
- //System.out.printf("No. of '%c' in array: %,d", 'd',charCount(charArray,'d'));
- //System.out.print("Enter a positive number: ");
- //int input = scan.nextInt();
- //System.out.println("Recursive sum of " + input + ": " + recursiveSum(input));
- //System.out.println(recursivePower(4,3));
- //String word = "Vince";
- //System.out.println(displayChar(word));
- int[] s = new int[]{1,2,300,-4,15};
- reverseArray(s);
- System.out.println(Arrays.toString(s));
- }
- public static int multiply(int x, int y) {
- if (y == 1) {
- return x;
- } else {
- return x + multiply(x, y - 1);
- }
- }
- public static String reverseString(String str) {
- if ((null == str) || (str.length() <= 1)) {
- return str;
- } else {
- return reverseString(str.substring(1)) + str.charAt(0);
- }
- }
- public static int maxElement(int[] arr) {
- // Iterative version
- /*int max = 0;
- boolean foundMax = false;
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] > max) {
- max = arr[i];
- }
- }
- return max; */
- return maxElementHelper(arr, arr.length-1);
- }
- private static int maxElementHelper(int[] arr, int index) {
- if (index > 0) {
- return Math.max(arr[index], maxElementHelper(arr, index-1));
- } else {
- return arr[0];
- }
- }
- // Write a method that uses recursion to count the number of times a specific character
- // occurs in an array of characters. Demonstrate the method in a program.
- public static int charCount(char[] arr, char given) {
- return charCountHelper(arr,arr.length-1, given);
- }
- private static int charCountHelper(char[] arr, int index, char given) {
- if (arr[index] == given ) {
- return 1 + charCountHelper(arr,index-1, given);
- } else {
- return 0;
- }
- }
- // Write a method that accepts a String as an argument. The method should use recursion
- // to display each individual character in the String.
- public static String displayChar(String word) {
- return displayCharHelper(word);
- }
- private static String displayCharHelper(String word) {
- if (word.length() == 1) {
- return word;
- } else {
- return (word.charAt(0) + "\n" + displayCharHelper(word.substring(1)));
- }
- }
- public static int recursiveSum(int num) {
- if (num == 1) {
- return 1;
- } else {
- return num + recursiveSum(num-1);
- }
- }
- public static int recursivePower(int base, int exp) {
- if (exp == 1) {
- return base;
- } else {
- return base * (recursivePower(base,exp-1));
- }
- }
- public static void reverseArray(int[] array) {
- reverseArrayHelper(array, 0, array.length-1);
- }
- private static void reverseArrayHelper(int[] array, int x, int y) {
- if (x < y) {
- int tmp = array[x];
- array[x] = array[y];
- array[y] = tmp;
- reverseArrayHelper(array, x+1, y-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement