Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: CollectNewspaperKarel.java
- * Author: Saba Pochkhua
- * --------------------------------
- * At present, the CollectNewspaperKarel subclass does nothing.
- * Your job in the assignment is to add the necessary code to
- * instruct Karel to walk to the door of its house, pick up the
- * newspaper (represented by a beeper, of course), and then return
- * to its initial position in the upper left corner of the house.
- */
- import stanford.karel.*;
- public class CollectNewspaperKarel extends SuperKarel {
- /*
- * Using "public void run()" we are commending Karel 3 to do things :
- * 1. to go to the newspaper
- * 2. then to take the newspaper
- * 3. and finally return to the start point
- */
- public void run () {
- goToNewspaper ();
- takeNewspaper ();
- goBack ();
- }
- /*
- * Precondition: Karel is standing on the 3X4 coordinates
- * Postcondition: Karel is standing on the 6X3 coordinates
- */
- private void goToNewspaper() {
- move();
- move();
- turnRight ();
- move();
- turnLeft();
- move();
- }
- /*
- * Precondition: no newspaper in bag
- * postcondition: newspaper in bag
- */
- private void takeNewspaper() {
- pickBeeper();
- }
- /*
- * Precondition: Karel is standing on the 6X3 coordinates
- * Postcondition: Karel is standing on the 3X4 coordinates
- */
- private void goBack() {
- turnAround();
- move ();
- turnRight();
- move();
- turnLeft();
- move();
- move();
- turnAround();
- }
- }
Add Comment
Please, Sign In to add comment