SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Fall 2004
Prof. Doug Baldwin
Hand out Lab 3
No Friday office hours this week
Using robot with Sun Java tools?
Averaging measurements for lab 2?
Sections 6.1 - 6.3
(Lots of this summary got lost when my computer crashed after running its battery down)
Recursion is solving a problem in terms of smaller versions of itself
Value-producing recursion must always return a value
Value-producing recursions always have at least 2 return statements
Three kinds of recursion
Towers of Hanoi
Goal: Move the tower from the left pole to the right (or vice versa)
Rules:
Imagine a "Super Array" class that represents arrays with some additional features
Arrays
class SuperArray {
private int[] A; // Underlying array
private int n; // Size of array
public SuperArray( int size ) {
A = new int[ size ];
n = size;
}
public getElement( int i ) {
return A[i];
}
...
Design a recursive method for SuperArray that initializes elements 0 through i to 0
init( int i )
if i < 0 // test
do nothing // base case
else
A[i] = 0; // Recursive case
init( i - 1 )
Some more sophisticated recursions
Read Sections 6.4 and 6.5