29-practice-midterm-solutions

# 29-practice-midterm- - Mehran Sahami Handout#29 CS 106A Solutions to Practice Midterm Portions of this handout by Eric Roberts and Patrick Young

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

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.

Unformatted text preview: Mehran Sahami Handout #29 CS 106A October 24, 2007 Solutions to Practice Midterm Portions of this handout by Eric Roberts and Patrick Young Problem 1: Karel the Robot (15 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 02/15/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

### Page1 / 4

29-practice-midterm- - Mehran Sahami Handout#29 CS 106A Solutions to Practice Midterm Portions of this handout by Eric Roberts and Patrick Young

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

View Full Document
Ask a homework question - tutors are online