-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDivision.java
More file actions
105 lines (97 loc) · 2.25 KB
/
Division.java
File metadata and controls
105 lines (97 loc) · 2.25 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import stanford.karel.SuperKarel;
public class Division extends SuperKarel {
public void run() {
move(); // We need to stand on correct starting point.
while (beepersPresent()) {
doMainOperation();
}
collectBeepers();
}
/*
* This method describes the main operation. we need to move on third square
* first. suppose, we have n beepers on second square and m on third square.
* We need to do subtraction n/m times and save that result on fourth
* square.
*/
private void doMainOperation() {
move();
subtractSecondSquare();
resetThirdSquare();
}
/*
* starting position: 3x1, facing east ending position: 5x1, facing east/
*/
private void subtractSecondSquare() {
while (beepersPresent()) {
subtractOperation(); // this line subtracts third square's beepers
// // from second square's beepers
}
increaseResult(); // this method calculates final answer on 4x1 square.
}
/*
* this method increases result each time by one.
*/
private void increaseResult() {
move();
putBeeper();
move();
}
/*
* this is the main method for subtraction operation for second and third
* squares.
*/
private void subtractOperation() {
pickBeeper();
turnAround();
move();
pickBeeper();
turnAround();
move();
move();
move();
putBeeper();
turnAround();
move();
move();
turnAround();
}
/*
* starting position: 5x1, facing east ending position: 2x1, facing east
* this function picks all beepers from 5x1 square and return to third
* square to start subtract operation again.
*/
private void resetThirdSquare() {
while (beepersPresent()) {
pickBeeper();
turnAround();
move();
move();
putBeeper();
turnAround();
move();
move();
}
returnToSecondSquare(); // return to main square to start operation
// again.
}
// starting position 5x1, facing east
// end position 2x1, facing east.
private void returnToSecondSquare() {
turnAround();
move();
move();
move();
turnAround();
}
/*
* we need this method to leave beepers only on fourth square. it picks
* beepers from third square. starting position 2x1, facing east. end
* position: 3x1, facing east.
*/
private void collectBeepers() {
move();
while (beepersPresent()) {
pickBeeper();
}
}
}