Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CS 209 – Java Programming – Assignment #22
- Chapter 18: Recursion - Hand in this work (Turn in word processor output for the exercises - include program listings and sample output): Exercise #s 1, 2, and 3 below– Total points: 60
- Hint: The recursive method printArrayHelper() in the Print class here could serve as a model for solving problems 18.2 and 18.3.
- // Print.java - program prints an array using recursion
- public class Print
- {
- public static void printArray(int[] array)
- { // call helper function to do work
- printArrayHelper(array, 0);
- System.out.println(); // add ending newline
- } // end method printArray
- private static void printArrayHelper(int[] array, int k)
- { // recursively print array
- if (k == array.length)
- return;
- System.out.print(array[k] + " ");
- printArrayHelper(array, k + 1);
- } // end method printArrayHelper
- public static void main(String args[])
- {
- int array[] = { 8, 22, 88, 34, 84, 21, 94 };
- System.out.print("Array is: ");
- printArray(array); // print array recursively
- } // end main
- } // end class Print
- (10) 18.1. Show the return value of method mystery() and the output of this program.
- (25) 18.2. Write and test a recursive method strRev() that takes a character array containing a string as an argument and prints the string backward.
- Hint: User String method toCharArray(), which takes no arguments, to get a char array containing the elements of the String.
- /* Sample output:
- * Enter a string: sooner or later one of us must know
- * wonk tsum su fo eno retal ro renoos
- * Enter a string: Hannah
- * hannaH
- */
- (25) 18.3. Write and test a recursive method minRecurs() that returns the smallest element in an int array.
- /* Output:
- * 22 88 8 94 78 84 96 73 34
- * Smallest value in the array: 8
- */
Advertisement
Add Comment
Please, Sign In to add comment