23a-practice-solutions

23a-practice-solutions - CS 106A Handout #23A July 14, 2009...

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

View Full Document Right Arrow Icon
Handout #23A CS 106A July 14, 2009 Solutions to Practice Midterm The midterm is scheduled for the following time and room: Wednesday, July 15, 11:00 A.M.–12:10P.M., Bldg 420 Room 040 Please remember that the midterm is open-book . Problem 1: Karel the Robot (10 points) There are many strategies for solving this problem. This code takes advantage of the fact that a column of three beepers appears three times in the construction of the house, making it useful to have a method like putThreeBeepers . /* * File: KatrinaRepairKarel.java * ----------------------------- * The KatrinaRepairKarel class solves the problem from the midterm * exam in which the robot builds houses at corners marked by rubble. */ import stanford.karel.*; public class KatrinaRepairKarel extends SuperKarel { /** Runs the program. */ public void run() { while (frontIsClear()) { if (beepersPresent()) { pickBeeper(); backUp(); buildHouse(); } if (frontIsClear()) { move(); } } } /** * Backs up one corner. * Precondition: There is an open square behind Karel * Postcondition: Karel is in that square facing the same direction. */ private void backUp() { turnAround(); move(); turnAround(); }
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 – /** * Builds a beeper house on stilts. * Precondition: Karel facing East at bottom of left stilt
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

23a-practice-solutions - CS 106A Handout #23A July 14, 2009...

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

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