User Tools

Site Tools


it-artikel:java:my-solution-on-section1-cleaning-punchcards

My Solution on Section1 - Cleaning PunchCards

This is my Solution of “Section1 - Cleaning PunchCards” from the Computer Science Course CS106A of Prof. Mehran Sahami at the STANFORD University.

Section1.java
/*
 * File: Section1.java
 * --------------------------------
 * Karel checks a punch card for half punched holes and cleans
 * those up. holes where the middle thingy is still existend
 * are considered as valid. 
 */
import stanford.karel.*;
 
public class Section1 extends SuperKarel {
	/**
	 * Specifies the program entry point.
	 */
	public void run() {
		while( frontIsClear() ) {
			move();
			checkAndCleanHole();
			move();
		}
	}
 
	private void checkAndCleanHole() {
		/* This Method checks the current hole Karel is standing in and
		 * cleans it up completly IF required.
		 * 
		 *  pre-condition:	karel sitting in the middle of a hole (on Y=3),
		 *  				facing EAST
		 *  
		 *  post-condition:	karel sitting in the middle of a hole (on Y=3),
		 *  				facing EAST
		 */
		if( noBeepersPresent() ) {
			cleanLeft();
			cleanRight();
		}
	}
 
	private void cleanLeft() {
		/* This Method cleans the LEFT side of a Hole
		 * 
		 *  pre-condition:	karel sitting in the middle of a hole (on Y=3),
		 *  				facing EAST
		 *  
		 *  post-condition:	karel sitting in the middle of a hole (on Y=3),
		 *  				facing EAST
		 */
		turnLeft();
		move();
		pickAllBeepers();
		turnAround();
		move();
		turnLeft();
	}
 
 
	private void cleanRight() {
		/* This Method cleans the RIGHT side of a Hole
		 * 
		 *  pre-condition:	karel sitting in the middle of a hole (on Y=3),
		 *  				facing EAST
		 *  
		 *  post-condition:	karel sitting in the middle of a hole (on Y=3),
		 *  				facing EAST
		 */
		turnRight();
		move();
		pickAllBeepers();
		turnAround();
		move();
		turnRight();
	}
 
	private void pickAllBeepers() {
		/* This Method picks up ALL Beepers at Karels actual position
		 */
		while( beepersPresent() ){
			pickBeeper();
		}
	}
}

Axel Werner 2011-02-05 17:40

it-artikel/java/my-solution-on-section1-cleaning-punchcards.txt · Last modified: 2022-08-31 12:30 by 127.0.0.1