====== My Solution on Section1 - Cleaning PunchCards ====== This is my Solution of "Section1 - Cleaning PunchCards" from the [[http://see.stanford.edu/see/courseinfo.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111|Computer Science Course CS106A of Prof. Mehran Sahami]] at the [[http://www.stanford.edu|STANFORD University]]. /* * 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(); } } } --- //[[mail@awerner.homeip.net|Axel Werner]] 2011-02-05 17:40// {{tag>java karel stanford university cs106 computer science learning programming}}