Lecture 10-Recursion - Read:pp.403408 butSKIPsect.15.1.2...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Recursion: If you get the point, stop; otherwise,  see Recursion. Infinite recursion: See Infinite recursion. Read: pp. 403-408 but SKIP sect. 15.1.2 ProgramLive CD, page 15-3, has  interesting recursive methods. Download presented algorithms from  the website Recursive definition : A definition that is defined in terms of itself. Recursive method : a method that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops), which we cover later. Recursion is an important programming tool. Purely functional languages have no loops —only recursion. CS1110 30 September. Recursion Prelim I 7:30–9:00PM Thursday, 7 Oct
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
== versus equals Read pp 154-155 2 b a1 c a2 a1 equals(Object) Object a2 equals(Object) Object In this situation: b == c is false public boolean equals(Object ob) { return this == ob; } d null d == c ? d.equals(c) ? A: d.equals(c) is false B: d.equals(c) is true C: d.equals(c) gives an error
Background image of page 2
3 b a1 c a2 a1 equals(Object) Object a2 equals(Object) Object x y equals(Object) Point 6 8 /** = “p is a Point and it describes the same point as this Point. */ public boolean equals(Object p) x y equals(Object) Point 6 8 b == c is false b.equals(c) is true Use b == c to test whether b and c contain the name of the same object.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
/** = the number of ‘e’s in s */ public String noe(String s) { if (s.length() == 0) { return 0; } // { s has at least one char } } 4 Called the base case Called the recursive case Express the answer with the same terminology as the specification, but on a smaller scale: number of ‘e’s in s = (if s[0] = ‘e’ then 1 else 0) + number of ‘e’s in s[1. .] return (s[0] = ‘e’ ? 1 : 0) + noe(s.substring(1)); Notation: s[i] shorthand for s.charAt[i].
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2010 for the course CS 9339 at Cornell University (Engineering School).

Page1 / 14

Lecture 10-Recursion - Read:pp.403408 butSKIPsect.15.1.2...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online