29-practice-solutions - Mehran Sahami Handout #29 CS 106A...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Mehran Sahami Handout #29 CS 106A October 21, 2009 Solutions to Practice Midterm Portions of this handout by Eric Roberts and Patrick Young Problem 1: Karel the Robot (20 points) /* File: InnerBorderKarel.java */ import stanford.karel.*; public class InnerBorderKarel extends SuperKarel { public void run() { moveUpRow(); for(int i = 0; i < 4; i++) { handleBorder(); nextPosition(); } } // Assumes Karel starts one avenue before the first beeper to // be placed in this line of the border. Places beepers until // Karel reaches a wall, but does not place a beeper on the last // corner (where Karel is facing the wall). private void handleBorder() { move(); while (frontIsClear()) { // We check for any existing beepers, so we don't put // two beepers on any of the "corners" of the border if (noBeepersPresent()) { putBeeper(); } move(); } } // Moves Karel up one row while keeping the same orientation private void moveUpRow() { turnLeft(); move(); turnRight(); } // Assumes Karel is facing a wall at the end of line of placed // beepers and repositions Karel to be facing in direction of next // line in the border of beepers that needs to be placed private void nextPosition() { turnRight(); move(); turnRight(); move(); turnRight(); } }...
View Full Document

This note was uploaded on 12/01/2009 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 4

29-practice-solutions - Mehran Sahami Handout #29 CS 106A...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online