Guest User

Untitled

a guest
Oct 26th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. /*
  2. * File: CollectNewspaperKarel.java
  3. * Author: Saba Pochkhua
  4. * --------------------------------
  5. * At present, the CollectNewspaperKarel subclass does nothing.
  6. * Your job in the assignment is to add the necessary code to
  7. * instruct Karel to walk to the door of its house, pick up the
  8. * newspaper (represented by a beeper, of course), and then return
  9. * to its initial position in the upper left corner of the house.
  10. */
  11.  
  12. import stanford.karel.*;
  13.  
  14. public class CollectNewspaperKarel extends SuperKarel {
  15.  
  16. /*
  17. * Using "public void run()" we are commending Karel 3 to do things :
  18. * 1. to go to the newspaper
  19. * 2. then to take the newspaper
  20. * 3. and finally return to the start point
  21. */
  22. public void run () {
  23. goToNewspaper ();
  24. takeNewspaper ();
  25. goBack ();
  26. }
  27.  
  28. /*
  29. * Precondition: Karel is standing on the 3X4 coordinates
  30. * Postcondition: Karel is standing on the 6X3 coordinates
  31. */
  32. private void goToNewspaper() {
  33. move();
  34. move();
  35. turnRight ();
  36. move();
  37. turnLeft();
  38. move();
  39. }
  40.  
  41. /*
  42. * Precondition: no newspaper in bag
  43. * postcondition: newspaper in bag
  44. */
  45. private void takeNewspaper() {
  46. pickBeeper();
  47. }
  48.  
  49. /*
  50. * Precondition: Karel is standing on the 6X3 coordinates
  51. * Postcondition: Karel is standing on the 3X4 coordinates
  52. */
  53. private void goBack() {
  54. turnAround();
  55. move ();
  56. turnRight();
  57. move();
  58. turnLeft();
  59. move();
  60. move();
  61. turnAround();
  62. }
  63.  
  64. }
Add Comment
Please, Sign In to add comment