Docsity

Java Code Snippet: Building Stairs with StairBuggle Objects in StairWorld, Study notes of Computer Science

Partial preview of the text

Download Java Code Snippet: Building Stairs with StairBuggle Objects in StairWorld and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Methods with Parameters

and Return Values

Friday, Sep. 14, 2007

Methods 4-

One small step for Buggles...

one giant leap for Bugglekind

Methods 4-

One stair of a fixed size

public class StairWorld extends BuggleWorld { ...

public void run() { StairBuggle stepper = new StairBuggle(); stepper.buildStair(); } }

class StairBuggle extends Buggle { public void buildStair() { this.left(); this.forward(4); this.right(); this.forward(4); } }

Methods 4-

One stair, and we choose the size

public class StairWorld extends BuggleWorld { ...

public void run() { StairBuggle stepper = new StairBuggle(); stepper.buildStair(5); } }

class StairBuggle extends Buggle { public void buildStair(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1); } }

Methods 4-

Begin with the first statement in run()

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); stepper.buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

Methods 4-

Create a StairBuggle and go to the second statement

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); stepper.buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

stepper

Methods 4-

Request that the first step be built

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

stepper

Methods 4-

Create an execution frame for buildStair(5)

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(5)

stairSize 5

SB

stepper

Methods 4-

Evaluate the receiver, this, in the second statement

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

NORTH

red

SB

.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(5)

stairSize 5

SB

SB

stepper

Methods 4-

Evaluate the argument expression stairSize - 1

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

NORTH

red

SB

.left(); .forward(stairSize-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(5)

stairSize 5

SB

SB

SB

stepper

Methods 4-

Evaluate the argument subexpression stairSize

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

NORTH

red

SB

.left(); .forward(stairSize-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(5)

stairSize 5

SB

SB

SB

stepper

Methods 4-

Evaluate the argument expression 5 - 1

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

NORTH

red

SB

.left(); .forward(5-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(5)

stairSize 5

SB

SB

SB

stepper

Methods 4-

Turn right and start the fourth statement

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

.left(); .forward(4); .right(); this.forward(stairSize-1);

SB

this

.buildStair(5)

stairSize 5

SB

SB

SB

SB

stepper

Methods 4-

Done with the first step!

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

.left(); .forward(4); .right(); .forward(4);

SB

this

.buildStair(5)

stairSize 5

SB

SB

SB

SB

SB

stepper

Methods 4-

Request that the second step be built

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

stepper

Methods 4-

Create an execution frame for buildStair(4)

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

SB

this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(4)

stairSize 4 stepper

Methods 4-

Turn right

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

SB

.left(); .forward(3); .right(); this.forward(stairSize-1);

SB

this

.buildStair(4)

stairSize 4

SB SB SB

stepper

Methods 4-

Draw the horizontal to finish the second stair

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

SB

.left(); .forward(3); .right(); .forward(3);

SB

this

.buildStair(4)

stairSize 4

SB SB SB SB

stepper

Methods 4-

Request that the third step be built

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

SB

stepper

Methods 4-

Create an execution frame for buildStair(3)

Object Land

Execution Land

StairWorld

StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); .buildStair(3);

this

.run()

SW

SW

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

SB

SB

SB

this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);

SB

this

.buildStair(3)

stairSize 3 stepper

Methods 4-

A void method

/ tells the StairBuggle to draw a stair /

public void buildStep(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1); }

reserved word void says method does not return a value

Methods 4-

A fruitful method

/ tells the StairBuggle to draw a stair and to return the StairBuggle’s position after completing the stair. /

public buildStep(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);

}

Methods 4-

A fruitful method

/ tells the StairBuggle to draw a stair and to return the StairBuggle’s position after completing the stair. /

public Location buildStep(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1); return this.getPosition(); }

Methods 4-

Jack and Jill go up the hill

Methods 4-

Suppose run() is invoked for an instance of StairWorld

Object Land

Execution Land

StairWorld

StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); jill.setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));

this

.run()

SW

SW

Methods 4-

Jack is born

Object Land

Execution Land

StairWorld

StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); jill.setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));

this

.run()

SW

SW

jack

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

Methods 4-

Jill is Born (rooming with Jack)

Object Land

Execution Land

StairWorld

StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); jill.setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));

this

.run()

SW

SW

jack

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

jill

Methods 4-

Jill feels blue

Object Land

Execution Land

StairWorld

StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); .setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));

this

.run()

SW

SW

jack

position

heading

color

brushDown true

StairBuggle

EAST

red

SB

position

heading

color

brushDown true

StairBuggle

EAST

blue

SB

jill

SB