Java Methods ObjectOriented Programming and Data Structures 3
Java Methods Object-Oriented Programming and Data Structures 3 rd AP edition Maria Litvin ● Gary Litvin Chapter(13) = Chapter(12) + 1 Algorithms and Recursion Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
Objectives: • Learn about recursion • Learn to interpret and write recursive methods 2
Properties of Algorithms • Compactness: an algorithm can use iterations or recursion to repeat the same steps multiple times • Generality: the same algorithm applies to any “size” of task or any input values • Abstractness: an algorithm does not depend on a particular computer language or platform (although it may depend on the general computing model) 3
Recursion • A recursive solution describes a procedure for a particular task in terms of applying the same procedure to a similar but smaller task. • Must have a base case when the task is so simple that no recursion is needed. • Recursive calls must eventually converge to a base case. 4
Recursion: an Example Procedure: Climb steps Base case: if no steps to climb stop Recursive case: more steps to climb 1. Step up one step 2. Climb steps 5
Recursive Methods • A recursive method calls itself • Must have a base case (can be implicit) • Example: public class My. Math { public static int add. Squares (int n) { if (n == 0) // if n is equal to 0 return 0; else return add. Squares (n - 1) + n * n; } } Base case Calls itself (with a smaller value of the parameter) 6
Recursion: How Does it Work • Implemented on a computer as a form of iterations, but hidden from the programmer • Assisted by the system stack 0 add. Squares (0) Base case 0 add. Squares (1) 1 1 add. Squares (2) 2 add. Squares (3) 3 4 add. Squares (4) 5 14 30 7
Recursion (cont’d) Recursion is especially useful for dealing with nested structures or branching processes 8
Case Study: File Manager How does it compute the total number of files and the total size? 9
File Manager (cont’d) total. Bytes (folder) { count 0 (This is pseudocode, not Java!) for each item X in folder { Base case if X is a file count + the number of bytes in X else (if X is a folder) count + total. Bytes(X) } return count } 10
File Manager (cont’d) • OOP is convenient for such structures 11
File Manager (cont’d) public interface File. Item { int file. Count( ); int total. Size( ); } public class File implements File. Item {. . . public int file. Count( ) { return 1; }. . . } public class Folder implements File. Item { private List<File. Item> items; . . . public int file. Count ( ) { int count = 1; for (File. Item item : items) count += item. file. Count ( ); return count; }. . . } 12
Review: • What is called a base case in recursion? • Suppose we define “word” as a sequence of letters. Turn this into a recursive definition. • What does the call some. Fun("draw") return? public String some. Fun (String s) { if (s. length( ) >= 2) s = some. Fun(s. substring(1)) + s. char. At(0); return s; } 13
- Slides: 13