


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An example of recursion in java programming through the implementation of a bagelbuggle object that moves forward, dropping bagels at each step until it reaches a specified number of steps. The code includes the bagelforward() method and its recursive invocation, as well as the creation and execution of the bagelworld and bagelbuggle classes.
Typology: Study notes
1 / 42
This page cannot be seen from the preview
Don't miss anything!



































Recursion
o Recursion solves a problem
by solving a smaller
instance of the same
problem.
o Think divide, conquer, and
glue when all the
subproblems have the
same “shape” as the
original problem.
We could solve …
bagelForward(int n)
bagelForward(int n)
Putting a wrapper around it
public class BagelWorld extends BuggleWorld {
public void run() {
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
}
}
class BagelBuggle extends Buggle {
public void bagelForward(int n) {
if (n == 0 ) { // base case
// do nothing
} else { // recursive case
dropBagel();
forward();
bagelForward(n- 1 ); // recursive invocation
}
}
}
position
heading
color red
brushDown false
betty bagels forward
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
position
heading
color red
brushDown false
Drop the bagel & step away from the wall
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
position
heading
color red
brushDown false
Another cell, another bagel
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
bagelForward(2)
this n bb 2
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
position
heading
color red
brushDown false
… and another invocation
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
bagelForward(2)
this n bb 2
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bagelForward(1)
this n bb 1
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
position
heading
color red
brushDown false
The base case
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
bagelForward(2)
this n bb 2
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bagelForward(1)
this n bb 1
bagelForward(0)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
this n bb 0
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
position
heading
color red
brushDown false
bagelForward(0) completes
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
bagelForward(2)
this n bb 2
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bagelForward(1)
this n bb 1
position
heading
color red
brushDown false
bagelForward(2) completes
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bagelForward(3)
if (n== 0 ) {
} else {
dropBagel();
forward();
bagelForward(n- 1 );
bw bb
this n bb 3
position
heading
color red
brushDown false
betty.bagelForward(3) completes
BagelBuggle betty = new BagelBuggle();
betty.brushUp();
betty.bagelForward( 3 );
this betty
Object
Land
Execution
Land
run
bw bb