Karel Defends Democracy Assignment And Solution

So if you were able to complete all the Assignment 1 problems, this one shouldn’t really take you that long. Here is the assignment:

This assignment is presented during the student Section, so there is no zip file for it to download. However, it is not that difficult to set this up yourself. To get started, click the green “C” button and create a class called “defendDemocracyKarel”. Now you can start a file with the following commands:

You also need to create a new world, as is shown in the assignment. Simply load Karel, and it’s pretty simple to create new worlds.

Here is my solution (although the solution is also available on the assignment site)


This time, I was very conscious of decomposition and making sure I solve the problem top-down. I also included comments of my thinking.

<pre>/* Karel is cleaning up the voting ballot.
 * The vote only counts if the middle rectangle is punched,
 * which means there are no beepers present.
 */

import stanford.karel.SuperKarel;

public class defendDemocracyKarel extends SuperKarel {

public void run() {
	while (frontIsClear()) {
		move();
		checkMiddleBeeper();
		cleanExtraBeepers();
		move();
	}
}
private void checkMiddleBeeper() {
	if (beepersPresent()) {
		move();
		move();
	}
}
private void cleanExtraBeepers() {
	if (noBeepersPresent()) {
		cleanBeepersSouth();
		cleanBeepersNorth();
	}
}
/* Karel moves South, checks for beepers, and picks them up
 * if there are any there.
 * Pre-condition: Karel is in the middle of a rectangle with no
 * middle beeper
 * Post-condition: Karel returns to middle facing North
 */
private void cleanBeepersSouth() {
	turnRight();
	move();
	while (beepersPresent()) {
		pickBeeper();
	}
	turnAround();
	move();
}
/* Karel moves North, checks for beepers, and picks them up
 * if there are any there.
 * Pre-condition: Karel is in the middle of a rectangle with no
 * middle beeper
 * Post-condition: Karel returns to middle facing East
 */
private void cleanBeepersNorth() {
	move();
	if (frontIsClear()) {
		move();
	}
	while (beepersPresent()) {
		pickBeeper();
	}
	turnAround();
	move();
	turnLeft();
	}
}</pre>

Ok, saying goodbye to Karel and growing into Java!

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.

  • Hello!

    I’m just started this Stanford Methodology course. One question about this assignment — why you are checking if there are no walls when in the cleanBeepersNorth method?
    I think there is no need for that as all the rectangles of the ballot are of the same design.

    private void cleanBeepersNorth() {
    move();
    if (frontIsClear()) {
    move();
    }

    • These are just my solutions, they don’t necessarily have to be “the correct” solution. Yours sounds correct as well.

      • it is not the question of correctness, you just don’t need that check at all and I cited your code not mine 😉
        sorry for the comment…

      • It would be great if you posted your code here for others to compare! I did this a while ago, so, like I said, my answers are not always the correct ones.

  • Hi! Joining the conversation, here is my code 🙂 It works well but I am not entirely convinced about the structure of the program. I like how clean yours looks

    
    import stanford.karel.*;
    
    public class defendDemocracyKarel extends SuperKarel{
    
    	public void run(){
    		goToRectangles();
    		cleanBallot();
    	}
    	
    	/*precondition for cleanBallot
    	 * Karel must be at the first rectangle
    	 */
    	
    	public void goToRectangles(){
    		move();
    	}
    	
    	public void cleanBallot(){
    		while (frontIsClear()){
    			checkAndCleanRectangle();
    			goToNextRectangle();
    		}		
    	}
    	/*We set a trigger to clean or not to clean
    	 * Precondition:Karel must be in the middle of the rectangle
    	 * Postcondition:Karel must end in the middle of the rectangle
    	 */
    	
    	public void checkAndCleanRectangle(){
    			if (noBeepersPresent()){
    				pickCenterBeepers();
    				pickTopBeepers();
    				pickBottomBeepers();
    			}
    	}
    	
    	/*Clean center Beepers:
    	 * precondition: Karel must face east and be at the middle of the rectangle
    	 * postcondition: Karel must be at the center of the rectangle */
    	public void pickCenterBeepers(){
    		while (beepersPresent()){
    			pickBeeper();
    		}
    	}
    	
    	/*Clean Top Beepers:
    	 * precondition: Karel must face east and be at the middle of the rectangle
    	 * postcondition: Karel must be at the center of the facing south */
    	
    	public void pickTopBeepers(){
    		turnLeft();
    		move();
    		while (beepersPresent()){
    			pickBeeper();
    		}
    		turnAround();
    		move();
    	}
    	
    	/*Clean Bottom Beepers:
    	 * precondition: Karel must face south and be at the middle of the rectangle
    	 * postcondition: Karel must be at the center of the rectangle facing East*/
    	
    	public void pickBottomBeepers(){
    		move();
    		turnAround();
    		while (beepersPresent()){
    			pickBeeper();
    		}
    		move();
    		turnRight();
    	}
    	
    	
    	public void goToNextRectangle(){
    		move();
    		if (frontIsClear()){
    			move();
    		}
    	}
    
    }
    
    
  • Krys

    here’s my solution: http://pastebin.com/y8nGNztv

  • Adam Boatman

    Here’s my solution

    public void run (){

    while (frontIsClear()){

    move();

    if (noBeepersPresent()){

    checkNorthForBeeper();

    checkSouthForBeepers();

    }

    }

    }

    //Karel will check south of mid-point for chad

    private void checkSouthForBeepers() {

    if (beepersPresent()){

    clearAllChad();

    turnAround();

    move();

    turnRight();

    }else{

    turnAround();

    move();

    turnRight();

    }

    }

    //If chad is located; Karel will pick up all chad

    private void clearAllChad() {

    while (beepersPresent()){

    pickBeeper();

    }

    }

    //redirecting to check North

    private void checkNorthForBeeper() {

    turnLeft();

    move();

    checkForChadNorth();

    }

    //Instructions to check and pick up chad to the North

    private void checkForChadNorth() {

    if (beepersPresent()){

    clearAllChad();

    turnAround();

    move();

    move();

    }else{

    turnAround();

    move();

    move();

    }

    }

    }

  • Mills Gang

    I’m taking the classes on line and the problem I find with some of the code including the above is that they don’t address the possibility of the last column having all it’s chad and subsequently running into the wall. To test just click “Edit World” and fill the last column with chad/beeper’s save and run again and low and behold Karel runs into the wall.

    Here my code that address the issue and will work on any world with a differing number of columns.

    /*
    * File: DefendDemocracyKarel.java
    * ————————–
    * If If there is a beeper in that position, Karel must assume that the
    * voter did not intend to cast a vote in that column and move on to the next.
    * If there is no beeper in the center square, Karel must check the other two
    * squares in the ballot and remove any and all beeper’s so that the ballot can
    * be counted correctly. Karel’s ending position must be facing East and at East
    * wall
    */

    import stanford.karel.SuperKarel;

    public class DefendDemocracyKarel extends SuperKarel {

    public void run() {
    move();

    while (frontIsClear()) {
    if (beepersPresent()) {
    chadPresent();

    } else {
    noChadPresent();
    }
    }

    if (frontIsClear()) {
    move();
    }
    }

    private void chadPresent() {
    if (frontIsClear()) {
    move();
    /*
    * Test required to keep Karel from running into wall if last column
    * has chad/beeper present
    */
    if (frontIsClear()) {
    move();
    }
    }
    }

    private void noChadPresent() {
    turnLeft();
    move();

    //while used to pickup multiple chad/beeper’s in same location
    while (

    beepersPresent()) {
    pickBeeper();
    }
    turnAround();

    //Check other side of column
    while (frontIsClear()) {
    move();
    }

    while (beepersPresent()) {
    pickBeeper();
    }
    turnAround();
    move();
    turnRight();

    if (frontIsClear()) {
    move();

    //Test required to keep Karel from running into wall
    if (frontIsClear()) {
    move();
    }
    }
    }
    }