Java Recursion: Implementing a Bagel-Forward Movement, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-3vk
koofers-user-3vk 🇺🇸

10 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS111 Computer Programming
Department of Computer Science
Wellesley College
There and back again
Buggle recursion
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download Java Recursion: Implementing a Bagel-Forward Movement and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

There and back again

Buggle recursion

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(3); by

dropBagel();

forward(); and solving …

bagelForward(2); by

dropBagel();

forward(); and solving …

bagelForward(1); by

dropBagel();

forward(); and solving …

bagelForward(0); by

Doing nothing at all

bagelForward(int n)

public void bagelForward(int n) {

if (n == 0 ) { // base case

// do nothing

} else { // recursive case

dropBagel();

forward();

// Now what?

bagelForward(int n)

public void bagelForward(int n) {

if (n == 0 ) { // base case

// do nothing

} else { // recursive case

dropBagel();

forward();

bagelForward(n- 1 ); // recursive invocation

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

EAST

color red

brushDown false

betty bagels forward

BagelBuggle bb

BagelWorld bw

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

BagelBuggle bb

position

heading

EAST

color red

brushDown false

Drop the bagel & step away from the wall

BagelWorld bw

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

EAST

color red

brushDown false

Another cell, another bagel

BagelBuggle bb

BagelWorld bw

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

EAST

color red

brushDown false

… and another invocation

BagelBuggle bb

BagelWorld bw

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

EAST

color red

brushDown false

The base case

BagelBuggle bb

BagelWorld bw

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

EAST

color red

brushDown false

bagelForward(0) completes

BagelBuggle bb

BagelWorld bw

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

BagelBuggle bb

position

heading

EAST

color red

brushDown false

bagelForward(2) completes

BagelWorld bw

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

EAST

color red

brushDown false

betty.bagelForward(3) completes

BagelBuggle bb

BagelWorld bw

BagelBuggle betty = new BagelBuggle();

betty.brushUp();

betty.bagelForward( 3 );

this betty

Object

Land

Execution

Land

run

bw bb