10a-section-1-solutions

10a-section-1-soluti - private void removeChad turnRight checkPunchCorner checkPunchCorner turnLeft Removes any chad from the corner in front of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Mehran Sahami Handout #10A CS 106A September 30, 2009 Solutions for Section #1 Based on a handout by Eric Roberts /* * File: VoteCountingKarel.java * ---------------------------- * The VoteCountingKarel subclass cleans out the chad from * a ballot as described in the section handout. */ import stanford.karel.*; public class VoteCountingKarel extends SuperKarel { public void run() { while (frontIsClear()) { move(); if (noBeepersPresent()) { removeChad(); } move(); } } /** * Removes any chad from a ballot, which consists of beepers * in the squares to right and left of Karel's current position.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: */ private void removeChad() { turnRight(); checkPunchCorner(); checkPunchCorner(); turnLeft(); } /** * Removes any chad from the corner in front of Karel. The * precondition is that Karel is facing one of the corners * that represents a punch hole in a ballot; the postcondition * is that Karel is on the same square but facing in the * opposite direction since it has just come out of the hole. */ private void checkPunchCorner() { move(); while (beepersPresent()) { pickBeeper(); } turnAround(); move(); } }...
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.

Ask a homework question - tutors are online