# CS 2110 Lecture Notes - Lecture 9: Pariah Dog, Popping

12 views7 pages

Lecture 9 – More Recursion

○ Primitive types vs Reference types

○

○ == vs equals

■ a== opaes a ad ’s alues

■ a.equals(b) compares the two objects using the equals method

■

○ Executing Recursive Methods

1. Push frame for call onto stack

2. Assign arg values to pars

3. Execute method bodies

4. Pop frame from stack and push return value on stack

○ Understanding Recursive Methods

find more resources at oneclass.com

find more resources at oneclass.com

1. Have a precise specification

2. Check that the method works in the base case(s).

3. Look at the recursive case(s). In your mind, replace each recursive call by

what it does according to the spec and verify correctness.

4. (No infinite recursion) Make sure that the args of recursive calls are in

some sense smaller than the pars of the method

○ Examples with len, dup, isPal

○

○ Efficiency with recursion

■ Calculating 2^n

■ Rathe tha 2*2*2*2…. N ties

■ (2*2)^n/2!!!!!!! If n is even

○ Recursion is a convenient and powerful way to define functions

■ Poles that see isuoutale a ofte e soled i a diide-and-

oue fashio:

● Reduce a big problem to smaller

problems of the same kind, solve the smaller problems

● Recombine the solutions to smaller

problems to form solution for big problem

find more resources at oneclass.com

find more resources at oneclass.com

## Document Summary

A. equals(b) compares the two objects using the equals method. Executing recursive methods: push frame for call onto stack, assign arg values to pars, execute method bodies, pop frame from stack and push return value on stack. Understanding recursive methods: have a precise specification, check that the method works in the base case(s), look at the recursive case(s). Recursion is a convenient and powerful way to define functions. P(cid:396)o(cid:271)le(cid:373)s that see(cid:373) i(cid:374)su(cid:396)(cid:373)ou(cid:374)ta(cid:271)le (cid:272)a(cid:374) ofte(cid:374) (cid:271)e sol(cid:448)ed i(cid:374) a (cid:862)di(cid:448)ide-and- (cid:272)o(cid:374)(cid:395)ue(cid:396)(cid:863) fashio(cid:374): Reduce a big problem to smaller problems of the same kind, solve the smaller problems. Recombine the solutions to smaller problems to form solution for big problem. Collection objects of type e. no order. Imagine just a box with a bunch of. This is also an interface, but has. Key is the first argument in put. So this creates a mapping of an obejct from one type to another. Does(cid:374)"t (cid:374)eed to (cid:271)e the sa(cid:373)e types.