|
@@ -19,8 +19,8 @@ public class WriteLoops {
|
19
|
19
|
int w = 0;
|
20
|
20
|
|
21
|
21
|
// Write a FOR loop that counts from 1 to 10.
|
22
|
|
- // calling
|
23
|
|
- w = w + 1;
|
|
22
|
+ for (w=0; w<5;w++){
|
|
23
|
+ }
|
24
|
24
|
// each time through the loop
|
25
|
25
|
|
26
|
26
|
// this will tell the test how many times the loop executed.
|
|
@@ -31,10 +31,8 @@ public class WriteLoops {
|
31
|
31
|
int w = 0;
|
32
|
32
|
|
33
|
33
|
// Write a FOR loop that counts from 1 to 10.
|
34
|
|
- // calling
|
35
|
|
- w = w + 1;
|
36
|
|
- // each time through the loop
|
37
|
|
-
|
|
34
|
+ for (w=0; w<10; w++) {
|
|
35
|
+ }
|
38
|
36
|
return w;
|
39
|
37
|
}
|
40
|
38
|
|
|
@@ -42,21 +40,16 @@ public class WriteLoops {
|
42
|
40
|
int w = 0;
|
43
|
41
|
|
44
|
42
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
|
- // calling
|
46
|
|
- w = w + 1;
|
47
|
|
- // each time through the loop
|
48
|
|
-
|
|
43
|
+ for (w=21;w>11;w--){
|
|
44
|
+ }
|
49
|
45
|
return w;
|
50
|
46
|
}
|
51
|
47
|
|
52
|
48
|
public int countDown() {
|
53
|
49
|
int w = 0;
|
54
|
|
-
|
55
|
50
|
// Write a FOR loop that counts down from 100 to 0.
|
56
|
|
- // calling
|
57
|
|
- w = w + 1;
|
58
|
|
- // each time through the loop
|
59
|
|
-
|
|
51
|
+ for (w=100; w>0; w--){
|
|
52
|
+ }
|
60
|
53
|
return w;
|
61
|
54
|
}
|
62
|
55
|
|
|
@@ -64,20 +57,16 @@ public class WriteLoops {
|
64
|
57
|
int w = 0;
|
65
|
58
|
|
66
|
59
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
|
- // calling
|
68
|
|
- w = w + 1;
|
69
|
|
- // each time through the loop
|
|
60
|
+ for(w=0; w<=32; w=w+2){
|
|
61
|
+ }
|
70
|
62
|
return w;
|
71
|
63
|
}
|
72
|
64
|
|
73
|
65
|
public int countDownFrom5000() {
|
74
|
66
|
int w = 0;
|
75
|
|
-
|
76
|
67
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
|
- // calling
|
78
|
|
- w = w + 1;
|
79
|
|
- // each time through the loop
|
80
|
|
-
|
|
68
|
+ for (w = 1; w<5001; w= w+11){
|
|
69
|
+ }
|
81
|
70
|
return w;
|
82
|
71
|
}
|
83
|
72
|
|
|
@@ -86,10 +75,13 @@ public class WriteLoops {
|
86
|
75
|
|
87
|
76
|
// Write a nested FOR loop(s), where one counts from
|
88
|
77
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
|
78
|
+ for (w=0;w<20;w++){
|
|
79
|
+ for (int j=0;j<=4;j++) {
|
|
80
|
+ return j;
|
89
|
81
|
// calling
|
90
|
|
- w = w + 1;
|
91
|
82
|
// each time through the inner loop
|
92
|
|
-
|
|
83
|
+ }
|
|
84
|
+ }
|
93
|
85
|
return w;
|
94
|
86
|
}
|
95
|
87
|
|
|
@@ -100,11 +92,13 @@ public class WriteLoops {
|
100
|
92
|
// statement inside the loop that checks the
|
101
|
93
|
// loop index counter and if it’s greater than 51,
|
102
|
94
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
|
-
|
104
|
|
- // calling
|
105
|
|
- w = w + 1;
|
|
95
|
+ for(w=5;w<=105;w++){
|
|
96
|
+ if (w > 51) {
|
|
97
|
+ System.out.println("Hello Zipcode");
|
|
98
|
+ }
|
|
99
|
+ // calling
|
106
|
100
|
// each time through the inner loop
|
107
|
|
-
|
|
101
|
+ }
|
108
|
102
|
return w;
|
109
|
103
|
}
|
110
|
104
|
|
|
@@ -129,9 +123,13 @@ public class WriteLoops {
|
129
|
123
|
// Write a WHILE loop that checks “gpsCurrentLocation()”
|
130
|
124
|
// and if that is not equal to “Home” then and it calls “driveSomeMore()”.
|
131
|
125
|
// After the loop is done, print “Honey, I’m Home!”
|
|
126
|
+
|
132
|
127
|
public int driveHome() {
|
133
|
128
|
int w = 0;
|
134
|
|
-
|
|
129
|
+ while (!gpsCurrentLocation().equals("Home")) {
|
|
130
|
+ driveSomeMore();
|
|
131
|
+ }
|
|
132
|
+ System.out.println("Honey, I'm Home!");
|
135
|
133
|
// you need to use a .equals for two Strings.
|
136
|
134
|
|
137
|
135
|
// calling
|
|
@@ -142,6 +140,7 @@ public class WriteLoops {
|
142
|
140
|
return w;
|
143
|
141
|
}
|
144
|
142
|
|
|
143
|
+
|
145
|
144
|
// Getting harder...
|
146
|
145
|
// First declare and set “highestScore” to 236. Then set “currentScore” to
|
147
|
146
|
// “gameNextScore()”. Then write a WHILE loop that checks "runningScore"
|
|
@@ -155,11 +154,13 @@ public class WriteLoops {
|
155
|
154
|
int runningScore = 0;
|
156
|
155
|
|
157
|
156
|
// do your while loop here
|
158
|
|
-
|
|
157
|
+ while (runningScore < highestScore) {
|
|
158
|
+ runningScore = runningScore + currentScore;
|
|
159
|
+ currentScore = gameNextScore();
|
159
|
160
|
// calling
|
160
|
161
|
w = w + 1;
|
161
|
162
|
// each time through the inner loop
|
162
|
|
-
|
|
163
|
+ }
|
163
|
164
|
return w; // >= 3;
|
164
|
165
|
}
|
165
|
166
|
|
|
@@ -172,11 +173,11 @@ public class WriteLoops {
|
172
|
173
|
int runningScore = 0;
|
173
|
174
|
|
174
|
175
|
// do your while loop here
|
175
|
|
-
|
176
|
|
- // calling
|
177
|
|
- w = w + 1;
|
178
|
|
- // each time through the inner loop
|
179
|
|
-
|
|
176
|
+ do {
|
|
177
|
+ runningScore = runningScore + currentScore;
|
|
178
|
+ currentScore = gameNextScore();
|
|
179
|
+ }
|
|
180
|
+ while (runningScore <highestScore);
|
180
|
181
|
return w >= 3;
|
181
|
182
|
}
|
182
|
183
|
|
|
@@ -184,11 +185,20 @@ public class WriteLoops {
|
184
|
185
|
// calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
|
185
|
186
|
// is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
|
186
|
187
|
// and also calls “tryServerRestart()”
|
|
188
|
+
|
187
|
189
|
public int checkServerStatus() {
|
188
|
|
- int w = 0;
|
|
190
|
+ int w = 0;
|
189
|
191
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
192
|
|
191
|
|
-
|
|
193
|
+ while (serverIsRunning() == true) {
|
|
194
|
+ waitFor(5);
|
|
195
|
+ }
|
|
196
|
+ if (serverIsRunning() == false) {
|
|
197
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
198
|
+ }
|
|
199
|
+ else {
|
|
200
|
+ tryServerRestart("Help!", adminPhoneNumber);
|
|
201
|
+ }
|
192
|
202
|
// calling
|
193
|
203
|
w = w + 1;
|
194
|
204
|
// each time through the inner loop
|
|
@@ -199,14 +209,19 @@ public class WriteLoops {
|
199
|
209
|
// Declare an “int” i. Set i to 7.
|
200
|
210
|
// Write a WHILE loop that checks “i” is less than 50,
|
201
|
211
|
// and if it is, add 7 to “i”
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
202
|
215
|
public int loop50by7() {
|
203
|
216
|
int w = 0;
|
204
|
|
-
|
205
|
|
-
|
|
217
|
+ int i = 7;
|
|
218
|
+
|
|
219
|
+ while (i<50) {
|
|
220
|
+ i = i+7;
|
206
|
221
|
// calling
|
207
|
222
|
w = w + 1;
|
208
|
223
|
// each time through the inner loop
|
209
|
|
-
|
|
224
|
+ }
|
210
|
225
|
return w;
|
211
|
226
|
}
|
212
|
227
|
|
|
@@ -239,11 +254,12 @@ public class WriteLoops {
|
239
|
254
|
int w = 0;
|
240
|
255
|
int sumOfThrees = 0;
|
241
|
256
|
|
242
|
|
-
|
|
257
|
+ for (w=0; w<threes_array.length; w++){
|
|
258
|
+ sumOfThrees = sumOfThrees + threes_array[w];
|
243
|
259
|
// calling
|
244
|
260
|
w = w + 1;
|
245
|
261
|
// each time through the inner loop
|
246
|
|
-
|
|
262
|
+ }
|
247
|
263
|
System.out.print("The Sum is ");
|
248
|
264
|
System.out.println(sumOfThrees);
|
249
|
265
|
|
|
@@ -255,15 +271,16 @@ public class WriteLoops {
|
255
|
271
|
public int rewriteFooAsWhile() {
|
256
|
272
|
int w = 0;
|
257
|
273
|
int sumOfThrees = 0;
|
258
|
|
-
|
259
|
|
-
|
|
274
|
+
|
|
275
|
+ while(w<threes_array.length) {
|
|
276
|
+ sumOfThrees = sumOfThrees + threes_array[w];
|
260
|
277
|
// calling
|
261
|
278
|
w = w + 1;
|
262
|
279
|
// each time through the inner loop
|
263
|
280
|
|
264
|
281
|
System.out.print("The Sum is ");
|
265
|
282
|
System.out.println(sumOfThrees);
|
266
|
|
-
|
|
283
|
+ }
|
267
|
284
|
return w;
|
268
|
285
|
}
|
269
|
286
|
|
|
@@ -279,11 +296,16 @@ public class WriteLoops {
|
279
|
296
|
boolean onTime = true;
|
280
|
297
|
|
281
|
298
|
// ADD YOUR CODE here.
|
282
|
|
-
|
|
299
|
+ boolean yardNeedsMowed = true;
|
|
300
|
+ while (isSummer()) {
|
|
301
|
+ if (yardNeedsMowed) {
|
|
302
|
+ yellAtJuniorToMowLawn();
|
283
|
303
|
// be sure to call
|
284
|
304
|
w = w + 1;
|
285
|
305
|
// each time inside the loop
|
286
|
|
-
|
|
306
|
+ }
|
|
307
|
+ }
|
|
308
|
+ sendJuniorBackToSchool("went great");
|
287
|
309
|
return w;
|
288
|
310
|
}
|
289
|
311
|
|
|
@@ -296,26 +318,27 @@ public class WriteLoops {
|
296
|
318
|
int w = 0;
|
297
|
319
|
int numberOfVotes = voteTallies.length;
|
298
|
320
|
|
299
|
|
-
|
|
321
|
+ for (w=0;w<numberOfVotes;w++) {
|
|
322
|
+ System.out.println(w);
|
300
|
323
|
// calling
|
301
|
324
|
w = w + 1;
|
302
|
325
|
// each time through the inner loop
|
303
|
|
-
|
|
326
|
+ }
|
304
|
327
|
return w;
|
305
|
328
|
}
|
306
|
329
|
|
307
|
330
|
// Given an array voteTallies[], write a WHILE loop that prints out each value
|
308
|
331
|
// in the array. You should declare and use an index “idx” to keep track of
|
309
|
332
|
// where you are.
|
|
333
|
+
|
|
334
|
+
|
310
|
335
|
public int tallyVote2() {
|
311
|
336
|
int w = 0;
|
312
|
337
|
int numberOfVotes = voteTallies.length;
|
313
|
|
-
|
314
|
|
-
|
315
|
|
- // calling
|
316
|
|
- w = w + 1;
|
317
|
|
- // each time through the inner loop
|
318
|
|
-
|
|
338
|
+ int idx = 0;
|
|
339
|
+ while (idx<numberOfVotes){
|
|
340
|
+ System.out.println(voteTallies[idx++]);
|
|
341
|
+ }
|
319
|
342
|
return w;
|
320
|
343
|
}
|
321
|
344
|
|