









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
Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Conditionals 7- 2
Conditionals 7- 3 WallHuggerWorld Straight-line code
Conditionals 7- 4 Decisions
Branching code
Conditionals 7- 7 Repeating ourselves
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
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
Conditionals 7- 13 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();
Peter jumps position heading (3,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh
Conditionals 7- 14 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();
And the clock starts ticking position heading (3,3) EAST color red brushDown true WallHugger this (^) whw (^) peterParker wh whw wh
Conditionals 7- 15 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();
.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
Conditionals 7- 16 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();
.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
Conditionals 7- 19 WallHuggerWorld WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Point(3,3)); peterParker.tick4();
.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();
.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();
.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();
.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
Conditionals 7- 26 Hungry WallHugger
Conditionals 7- 27 WallHugger pigs out*
Conditionals 7- 28 Put another way*
Conditionals 7- 31 It’s okay to leave off the “else”
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 (