1 / 17

Announcements

Announcements. P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday: review, review, review. Today’s Topics. Recursion Another sample class hierarchy Brainstorming on P5. Recursion.

gates
Download Presentation

Announcements

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. Announcements • P5 due Thursday • Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours • Tomorrow, Wednesday: C, pointers • Thursday, Friday: review, review, review Lecture 20

  2. Today’s Topics • Recursion • Another sample class hierarchy • Brainstorming on P5 Lecture 20

  3. Recursion • A method is recursive if it calls itself. • Many algorithms are most easily written using recursive methods. (e.g., quicksort) • To see that recursive method calls (calls of a method from within the body of a method) work, you have only to execute a method call of a recursive method yourself, using the rules that you already know. We’ll show that for a simple case in this lecture. Lecture 20

  4. Correctness of recursive methods • However, to understand that a particular recursive method is correct, you should not think about executing it. Rather, do two things: • Understand that each recursive method call is correct in terms of the specification of the method. • Make sure that the recursion “terminates” (much like making sure that a loop terminates). That is, see to it that in some sense the arguments of the recursive call are “smaller” than the parameters of the method and that when the parameters are “as small as possible”, the method terminates without calling itself recursively. We’ll see this in the examples. Lecture 20

  5. Example: reverse string // Return the reverse of string s. For example, if s is // “abcd”, return the string “dcba”. publicstatic String rev(String s) { // If the string is empty or contains one character, return it if (s.length( ) <= 1) return s; // The string has the form C c, where C is a character; // c is a string. C is s.charAt(0). c is s.substring(1). // Return the reverse of c catenated with C return rev(s.substring(1)) + s.charAt(0); } Lecture 20

  6. Comments on reverse string: • Note that the argument to the recursive callrev(s.substring(1))has one less character than parameter s. • Therefore, the “depth” of recursion --the maximum number of recursive calls with frames that will exist at any time-- is the number of characters in s. • Note that in the simple case (length 1), there is no recursive call • All of this suggests that the recursion will terminate Lecture 20

  7. Frame trace M0: frame for main. t ____ Called from system • Suppose rev called in main as: t = rev(“abc”); //Return the reverse of string s. publicstatic String rev(String s) { if (s.length( ) <= 1) return s; return rev(s.substring(1)) + s.charAt(0); } F0: first frame for rev. s “abc” Called from main, frame M0 F2: second frame for rev. s “bc” Called from rev inside rev, frame F1 F3: third frame for rev. s “c” Called from rev inside rev, frame F2 Lecture 20

  8. Example 2: remove blanks // Return a copy of s, but with blanks removed public static StringremoveBlanks (String s) { if (s.length() == 0) return s; if (s.charAt(0) == ‘ ‘) returnremoveBlanks(s.substring(1)); // first character of s is not a blank. // Return first character followed by rest of s // but with blanks removed return s.charAt(0) + removeBlanks(s.substring(1)); } Lecture 20

  9. Example 3: duplicate chars // Return a copy of s but with each character repeated public static String dup(String s) { if s.length() == 0) return s; return s.charAt(0) + s.charAt(0) + dup(s.substring(1)); } Lecture 20

  10. Example 4: factorial // Given n>=0, return !n = 1*2*3*…*n public static factorial(int n) { if (n<=1) return 1; return n * factorial(n-1); } Lecture 20

  11. Another class hierarchy • Problem context: data storage facilities in a computer system • Suppose there are three: • cache (very fast, very small) • main memory (RAM), pretty fast, moderately sized • disk (hard drive), slow, very big • Methods we’d like: • getSpeed, getSize, getCost • for hd: how much is swap space? for RAM: what is the page size? For cache: What’s the replacement policy? Lecture 20

  12. Draw the Hierarchy Lecture 20

  13. Superclass features • What’s the constructor look like? • What methods should be included in the superclass? • What fields should be in the superclass? • Do we want to define any abstract methods here? Lecture 20

  14. Main memory subclass features • What fields does this subclass have uniquely? • What should the constructor look like? • What methods does this subclass provide? Lecture 20

  15. Disk subclass features • What fields does this subclass have uniquely? • What should the constructor look like? • What methods does this subclass provide? Lecture 20

  16. Cache subclass features • What fields does this subclass have uniquely? • What should the constructor look like? • What methods does this subclass provide? Lecture 20

  17. Brainstorming on P5_2 • Some facts about U.S. politicians that should be represented in your system? • Possible routines to write about this data? • Possible interface options? • What else? Lecture 20

More Related