10a-section-1-solutions

10a-section-1-solutions - Mehran Sahami CS 106A Handout...

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

View Full Document Right Arrow Icon
Mehran Sahami Handout #10A CS 106A October 3, 2011 Solutions for Section #1 Based on a handout by Eric Roberts /* * File: UnitedNationsKarel.java * ---------------------------- * The UnitedNationsKarel subclass builds houses at corners * marked by rubble. */ import stanford.karel.*; public class UnitedNationsKarel extends SuperKarel { public void run() { while (frontIsClear()) { if(beepersPresent()) { pickBeeper(); backup(); buildHouse(); } if(frontIsClear()) { move(); } } } /** * Builds a beeper house on stilts. * Precondition: Karel facing East at bottom of left stilt * Postcondition: Karel facing East at bottom of right stilt */ private void buildHouse() { turnLeft(); putThreeBeepers(); move(); turnRight(); move(); turnRight(); putThreeBeepers(); turnAround(); move(); turnRight(); move(); turnRight(); putThreeBeepers(); turnLeft(); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
– 2 – /* * Creates a line of three beepers. * Precondition: Karel is in the first square in the line * Postcondition: Karel is in the last square in the line
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

10a-section-1-solutions - Mehran Sahami CS 106A Handout...

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