Axel Werner's OPEN SOURCE Knowledge Base
Because Information Wants To Be Free!
Sie befinden sich hier: Willkommen auf meiner persönlichen Homepage! » Meine IT Artikel » My Solution on Section1 - Cleaning PunchCards
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
| — |
it-artikel:my-solution-on-section1-cleaning-punchcards [2009-10-10 16:23] (aktuell) mail@awerner.homeip.net angelegt |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== 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|STANDFORD University]]. | ||
| + | |||
| + | <code> | ||
| + | /* | ||
| + | * 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(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </code> | ||