{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 10-Recursion

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

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

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

View Full Document
== 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
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.

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

View Full Document
/** = 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].
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}