Spiderman: Conditional Statements – Computer Programming | CS 111, Study notes of Computer Science

Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-748
koofers-user-748 🇺🇸

5

(2)

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Spiderman
Conditional statements
Conditionals 7-2
Spider sense
Actuators change the world:
forward(); left();
brushDown();
dropBagel();
Sensors detect the world:
isFacingWall();
getColor(); isOverBagel();
Control takes input from
sensors to do something
through actuators
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Spiderman: Conditional Statements – Computer Programming | CS 111 and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Spiderman

Conditional statements

Conditionals 7- 2

Spider sense

Actuators change the world:

forward(); left();

brushDown();

dropBagel();

Sensors detect the world:

isFacingWall();

getColor(); isOverBagel();

Control takes input from

sensors to do something

through actuators

Conditionals 7- 3 WallHuggerWorld Straight-line code

forward();

forward();

left();

forward();

Conditionals 7- 4 Decisions

left();

isFacingWall()

forward();

true false

Branching code

Conditionals 7- 7 Repeating ourselves

class WallHugger extends Buggle

public void followWall()

if (isFacingWall()) {

left();

} else {

forward();

public class WallHuggerWorld extends BuggleWorld

public void run ()

WallHugger peterParker = new WallHugger();

peterParker.setPosition(new Point(3,3));

peterParker.followWall();

peterParker.followWall();

Conditionals 7- 8 Repeating ourselves more succinctly class WallHugger extends Buggle { public void followWall() { … // same as before } public void followWall2() { followWall(); followWall(); } public void followWall4() { followWall2(); followWall2(); } public void followWall8() { followWall4(); followWall4(); } } public class WallHuggerWorld extends BuggleWorld { public void run () { WallHugger peterParker = new WallHugger(); peterParker.setPosition( new Point(3,3)); peterParker.followWall8(); } }

Conditionals 7- 9 Capturing the pattern // Allows instances of subclasses to // perform some action on every clock // tick (a predetermined number of times). public class TickBuggle extends Buggle { public void tick() { // Default is to do nothing } public void tick2() { tick(); tick(); } public void tick4() { tick2(); tick2(); } ... public void tick1024() { tick512(); tick512(); } } // class TickBuggle Conditionals 7- 10

TickBuggle

class WallHugger extends TickBuggle { public void followWall() { … } public void tick() { // insert code here that we // want executed repeatedly followWall(); } } public class WallHuggerWorld extends BuggleWorld { public void run() { WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick128(); } } Using the pattern

Buggle

WallHugger

Conditionals 7- 13 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

.run()

Peter jumps position heading (3,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh

Execution

Land

Conditionals 7- 14 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

.run()

And the clock starts ticking position heading (3,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh

Execution

Land

Conditionals 7- 15 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

.run() tick4() is invoked position heading (3,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh tick2(); tick2(); .tick4() this (^) wh

Execution

Land

wh

Conditionals 7- 16 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

.run() tick4() invokes tick2() position heading (3,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh tick2(); tick2(); wh .tick4() this (^) wh tick(); tick(); wh .tick2() this (^) wh

Execution

Land

Conditionals 7- 19 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

Execution

Land

.run() Finally,... position heading (4,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh tick2(); tick2(); .tick4() this (^) wh tick(); tick(); .tick2() this (^) wh followWall(); .tick() this (^) wh .followWall() this (^) wh if (isFacingWall()) { left(); } else { forward(); } wh wh wh wh Conditionals 7- 20 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

Execution

Land

.run() followWall() goes away position heading (4,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh tick2(); tick2(); .tick4() this (^) wh tick(); tick(); .tick2() this (^) wh followWall(); .tick() this (^) wh wh wh wh

Conditionals 7- 21 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

Execution

Land

.run() And so does tick position heading (4,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh tick2(); tick2(); .tick4() this (^) wh tick(); tick(); .tick2() this (^) wh wh wh Conditionals 7- 22 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();

Object

Land

Execution

Land

.run() But another tick takes its place position heading (4,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh tick2(); tick2(); .tick4() this (^) wh tick(); tick(); .tick2() this (^) wh followWall(); .tick() this (^) wh wh wh wh

Conditionals 7- 25 Bagels, bagels everywhere

BuggleWorld

RandomBagel

World

WallHugger

World

public class WallHuggerWorld extends RandomBagelWorld

public void run ()

WallHugger peterParker = new WallHugger();

peterParker.setPosition(new Point(3,3));

peterParker.tick128();

Conditionals 7- 26 Hungry WallHugger

left();

isFacingWall()

forward();

true false

isOverBagel()

true false

pickUpBagel();

Conditionals 7- 27 WallHugger pigs out*

class WallHugger extends TickBuggle

public void followWall()

if (isOverBagel()) {

pickupBagel();

} else {

if (isFacingWall()) {

left();

} else {

forward();

*Using nested conditionals.

Conditionals 7- 28 Put another way*

class WallHugger extends TickBuggle

public void followWall()

if (isOverBagel()) {

pickupBagel();

} else if (isFacingWall()) {

left();

} else {

forward();

*Multi-way conditionals.

Conditionals 7- 31 It’s okay to leave off the “else”

left();

isFacingWall()

forward();

true false

isOverBagel()

true false

pickUpBagel();

public void followWall() { if (isOverbagel()) { pickupBagel(); } if (isFacingWall()) { left(); } else { forward(); } } Conditionals 7- 32 Real general form of the if statement The if statement has the form: if ( ) [ else ]

Three cases

if ( )

else

if ( )

if ( )

else if ( )