1 / 23

Recursion & Collections API

Recursion & Collections API. Recursion Revisited Programming Assignments using the Collections API. Recursion as Repetition. public static void hello (int N) { for (int k = 0; k < N; k++) System.out.println (“Hello World!”); }. Recursion as Repetition. public static void hello (int N)

bonner
Télécharger la présentation

Recursion & Collections API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion & Collections API • Recursion Revisited • Programming Assignments using the Collections API

  2. Recursion as Repetition public static void hello (int N) { for (int k = 0; k < N; k++) System.out.println (“Hello World!”); }

  3. Recursion as Repetition public static void hello (int N) { if ( N == 0) return; else { System.out.println (“Hello World!”); hello (N – 1); } }

  4. Recursion as Repetition • Write a recursive method pow() that returns xn, where x and n are both integers greater than or equal to zero.

  5. Recursion as Repetition public static long pow (int x, int n) { if ( x == 0) return 0; if ( n == 0) return 1; long result = x * pow ( x, n – 1); return result; }

  6. Recursive String Methods • Write a recursive method that will print the characters in a string recursively, one character at a time.

  7. Recursive String Methods public static void printString (String s) { if ( s.length() == 0) return; else { System.out.println ( s.charAt(0) ); printString ( s.substring (1) ); } }

  8. Recursive String Methods • Write a recursive method that will print a String in reverse order one character at a time.

  9. Recursive String Methods public static void printReverse ( String s ) { if ( s.length() > 0 ) { printReverse ( s.substring ( 1 ) ); System.out.println ( s.charAt ( 0 ) ); } } • Many recursive solutions involve breaking a sequential structure, such as a string or an array, into its head and tail. An operation is performed on the head, and the algorithm recurses on the tail.

  10. Recursive String Methods • Write a recursive method that will count the number of occurrences of the character ch in the String s.

  11. Recursive String Methods public static int countChar (String s, char ch) { if ( s.length() == 0 ) return 0; else if ( s.charAt ( 0 ) == ch) return 1 + countChar ( s.substring (1), ch); else return 0 + countChar ( s.substring (1), ch); }

  12. Recursive String Methods • Write a recursive method to rotate a String by N characters to the right. For example, rotateR (“hello”, 3) should return “llohe”.

  13. Recursive String Methods public static String rotateR (String s, int n) { if ( n == 0 ) return s; else { StringBuffer buf = new StringBuffer (); buf.append (s.charAt (s.length() - 1)); buf.append (s.substring (0, s.length() - 1)); return rotateR (buf.toString(), n - 1); } }

  14. Recursive String Methods • Write a recursive method to convert a String representing a binary number to its decimal equivalent. For example, binTodecimal (“101011”) should return the int 43.

  15. Recursive String Methods public static int binTodecimal (String s) { if ( s.length() == 1) return Integer.parseInt (s); else return Integer.parseInt (s.substring (s.length() - 1)) + 2 * binTodecimal (s.substring (0, s.length() - 1); }

  16. Recursive String Methods • A palindrome is a string that is the same as its reverse – “radar” and “able was I ere I saw elba”. Write a recursive boolean method that determines whether its String parameter is a palindrome.

  17. Recursive String Methods public static boolean palindrome ( String s) { if ( s.length() == 0 || s.length() == 1 ) return true; else { if ( s.charAt(0) != s.charAt (s.length() - 1)) return false; else return palindrome ( s.substring (1, s.length() - 1) ); } }

  18. Recursive Array Methods • Write a recursive method that will do a sequential search on an array.

  19. Recursive Array Methods public static int rSearch (int[] arr, int head, int key ) { if ( head == arr.length ) return –1; else if ( arr[head] == key ) return head; else return rSearch ( arr, head + 1, key ); }

  20. Recursive Array Methods • Write a recursive method that will do a selection sort on an array.

  21. Recursive Array Methods public static void selectionSort ( int[] arr, int last ) { if ( last > 0 ) { int maxLoc = findMaxIdx ( arr, last); swap ( arr, last, maxLoc ); selectionSort ( arr, last – 1 ); } } public static int findMaxIdx ( arr, last) { int maxIdx = 0; for (int i = 0; i <= last; i++) if ( arr [i] > arr[maxIdx) maxIdx = i; }

  22. Program 1 • Create a class with a method that takes a string and returns the number of characters that only occur once in the string. It is expected that the method will be called repeatedly with the same strings. Since the counting operation can be time consuming, the method should cache the results so that when the method is given a string previously encountered, it will simply retrieve the stored result. Use collections wherever appropriate.

  23. Program 2 • Write a program that reads strings from input and outputs them sorted by length, shortest string first. If a subset of input strings has the same length, your program should output them in alphabetical order.

More Related