10a section 1 solutions


Mehran Sahami Handout #10A
CS 106A October 3, 2007
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.
*/
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();
}
}


Wyszukiwarka

Podobne podstrony:
38a section 7 solutions
30a section 5 solutions
34a section 6 solutions
20a section 3 solutions
15a section 2 solutions
41a section 8 solutions
Output Section Type
Silosy section 7
Output Section Description
language tests 10a
Output Section LMA
solution?BDB27F
2010 experimental solutions

więcej podobnych podstron