Download presentation
Presentation is loading. Please wait.
Published byDella Stokes Modified over 8 years ago
1
CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday: review, review, review
2
CS 100Lecture 202 Today’s Topics Recursion Another sample class hierarchy Brainstorming on P5
3
CS 100Lecture 203 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.
4
CS 100Lecture 204 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.
5
CS 100Lecture 205 Example: reverse string // Return the reverse of string s. For example, if s is // “abcd”, return the string “dcba”. public static 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); }
6
CS 100Lecture 206 Comments on reverse string: Note that the argument to the recursive call rev(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
7
CS 100Lecture 207 Frame trace Suppose rev called in main as: t = rev(“abc”); F0: first frame for rev. s “abc” Called from main, frame M0 M0: frame for main. t ____ Called from system 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 //Return the reverse of string s. public static String rev(String s) { if (s.length( ) <= 1) return s; return rev(s.substring(1)) + s.charAt(0); }
8
CS 100Lecture 208 Example 2: remove blanks // Return a copy of s, but with blanks removed public static String removeBlanks (String s) { if (s.length() == 0) return s; if (s.charAt(0) == ‘ ‘) return removeBlanks(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)); }
9
CS 100Lecture 209 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)); }
10
CS 100Lecture 2010 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); }
11
CS 100Lecture 2011 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?
12
CS 100Lecture 2012 Draw the Hierarchy
13
CS 100Lecture 2013 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?
14
CS 100Lecture 2014 Main memory subclass features What fields does this subclass have uniquely? What should the constructor look like? What methods does this subclass provide?
15
CS 100Lecture 2015 Disk subclass features What fields does this subclass have uniquely? What should the constructor look like? What methods does this subclass provide?
16
CS 100Lecture 2016 Cache subclass features What fields does this subclass have uniquely? What should the constructor look like? What methods does this subclass provide?
17
CS 100Lecture 2017 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.