{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

29-practice-solutions

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

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

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(); }

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}