-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectNewspaperKarel.java
executable file
·72 lines (64 loc) · 1.63 KB
/
CollectNewspaperKarel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* File: CollectNewspaperKarel.java
* --------------------------------
* The CollectNewspaperKarel subclass instructs 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 {
/**
* The entry point to Karel's execution.
*/
public void run() {
moveToTheNewspaper();
pickNewspaper();
returnToTheStartingPoint();
}
/**
* Makes Karel move 3 steps forward.
*/
private void moveThree() {
for (int i=0; i < 3; ++i) {
move();
}
}
/**
* Makes Karel move one step right. Karel faces the same
* direction before and after the execution.
*/
private void moveRight() {
turnRight();
move();
turnLeft();
}
/**
* Moves Karel from his original position to the corner with
* the beeper representing his newspaper. Karel faces East
* both before and after the call.
*/
private void moveToTheNewspaper() {
moveRight();
moveThree();
}
/**
* Assuming that Karel is standing at the corner with the
* beeper representing his newspaper, makes him pick it up.
*/
private void pickNewspaper() {
pickBeeper();
}
/**
* Assuming that Karel i standing just outside his door
* facing East, this method makes him return back inside
* of his house to his original starting location and
* orientation.
*/
private void returnToTheStartingPoint() {
turnAround();
moveThree();
moveRight();
turnAround();
}
}