Docsity

Solutions for Section - Section Handout - Programming Methodology- 10, Exercises of Programming Methodologies

Toggle sidebar
bg1
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();
}
}

Partial preview of the text

Download Solutions for Section - Section Handout - Programming Methodology- 10 and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout #10A

CS 106A October 3, 2007

Solutions for Section

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(); } }