witchway915

Untitled

Dec 5th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. CS 209 – Java Programming – Assignment #22
  2.  
  3. 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
  4.  
  5. Hint: The recursive method printArrayHelper() in the Print class here could serve as a model for solving problems 18.2 and 18.3.
  6.  
  7. // Print.java - program prints an array using recursion
  8.  
  9. public class Print
  10. {
  11. public static void printArray(int[] array)
  12. { // call helper function to do work
  13. printArrayHelper(array, 0);
  14. System.out.println(); // add ending newline
  15. } // end method printArray
  16.  
  17. private static void printArrayHelper(int[] array, int k)
  18. { // recursively print array
  19. if (k == array.length)
  20. return;
  21. System.out.print(array[k] + " ");
  22. printArrayHelper(array, k + 1);
  23. } // end method printArrayHelper
  24.  
  25. public static void main(String args[])
  26. {
  27. int array[] = { 8, 22, 88, 34, 84, 21, 94 };
  28. System.out.print("Array is: ");
  29. printArray(array); // print array recursively
  30. } // end main
  31. } // end class Print
  32.  
  33.  
  34. (10) 18.1. Show the return value of method mystery() and the output of this program.
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. (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.
  48.  
  49. Hint: User String method toCharArray(), which takes no arguments, to get a char array containing the elements of the String.
  50.  
  51. /* Sample output:
  52. * Enter a string: sooner or later one of us must know
  53. * wonk tsum su fo eno retal ro renoos
  54. * Enter a string: Hannah
  55. * hannaH
  56. */
  57.  
  58.  
  59. (25) 18.3. Write and test a recursive method minRecurs() that returns the smallest element in an int array.
  60.  
  61. /* Output:
  62. * 22 88 8 94 78 84 96 73 34
  63. * Smallest value in the array: 8
  64. */
Advertisement
Add Comment
Please, Sign In to add comment