Title: Announcements
1Announcements
- P5 due Thursday
- Final exam Tuesday August 10th, 8AM, Olin 155, 2
hours - Tomorrow, Wednesday C, pointers
- Thursday, Friday review, review, review
2Todays Topics
- Recursion
- Another sample class hierarchy
- Brainstorming on P5
3Recursion
- 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. Well show that for a simple
case in this lecture.
4Correctness 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.
Well see this in the examples.
5Example 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( ) lt 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)
-
6Comments 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
7Frame trace
- Suppose rev called in main as t rev(abc)
M0 frame for main. t ____ Called
from system
//Return the reverse of string s. public static
String rev(String s) if (s.length( ) lt 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
8Example 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))
-
9Example 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))
-
10Example 4 factorial
- // Given ngt0, return !n 123n
- public static factorial(int n)
- if (nlt1) return 1
- return n factorial(n-1)
-
11Another 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 wed like
- getSpeed, getSize, getCost
- for hd how much is swap space? for RAM what is
the page size? For cache Whats the replacement
policy?
12Draw the Hierarchy
13Superclass features
- Whats 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?
14Main memory subclass features
- What fields does this subclass have uniquely?
- What should the constructor look like?
- What methods does this subclass provide?
15Disk subclass features
- What fields does this subclass have uniquely?
- What should the constructor look like?
- What methods does this subclass provide?
16Cache subclass features
- What fields does this subclass have uniquely?
- What should the constructor look like?
- What methods does this subclass provide?
17Brainstorming 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?