Karel Defends Democracy Assignment And Solution

30 Oct

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:

import stanford.karel.SuperKarel;

public class defendDemocracyKarel extends SuperKarel {

public void run() {

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!

Advertisements

7 Responses to “Karel Defends Democracy Assignment And Solution”

  1. kortastebukleMarius January 31, 2012 at 6:57 am #

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

    • Natasha Murashev January 31, 2012 at 8:55 am #

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

      • kortastebukle January 31, 2012 at 10:35 am #

        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…

      • Natasha Murashev January 31, 2012 at 4:14 pm #

        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.

  2. Darius Lau May 4, 2012 at 5:21 pm #

    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();
    		}
    	}
    
    }
    
    
  3. Krys July 14, 2012 at 2:41 pm #

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s