|
|
@@ -74,7 +74,7 @@ public class WriteLoops {
|
|
74
|
74
|
// Write a FOR loop from 0 to 32 by 2s.
|
|
75
|
75
|
// calling
|
|
76
|
76
|
for (int i=0; i<32; i+=2){
|
|
77
|
|
- w = w + 0;
|
|
|
77
|
+ w = w + 1;
|
|
78
|
78
|
}
|
|
79
|
79
|
// each time through the loop
|
|
80
|
80
|
return w;
|
|
|
@@ -215,7 +215,7 @@ public class WriteLoops {
|
|
215
|
215
|
// each time through the inner loop
|
|
216
|
216
|
if (!serverIsRunning()){
|
|
217
|
217
|
sendEmergencyText("Help!", adminPhoneNumber);
|
|
218
|
|
- tryServerRestart("Help!", adminPhoneNumber);
|
|
|
218
|
+ tryServerRestart("Restart my server please.", adminPhoneNumber);
|
|
219
|
219
|
}
|
|
220
|
220
|
return w;
|
|
221
|
221
|
}
|
|
|
@@ -225,11 +225,12 @@ public class WriteLoops {
|
|
225
|
225
|
// and if it is, add 7 to “i”
|
|
226
|
226
|
public int loop50by7() {
|
|
227
|
227
|
int w = 0;
|
|
228
|
|
-
|
|
229
|
|
- // calling
|
|
230
|
|
- w = w + 1;
|
|
231
|
|
- // each time through the inner loop
|
|
232
|
|
-
|
|
|
228
|
+ int i = 7;
|
|
|
229
|
+ while (i<59) {
|
|
|
230
|
+ // calling
|
|
|
231
|
+ i += 7;
|
|
|
232
|
+ // each time through the inner loop
|
|
|
233
|
+ }
|
|
233
|
234
|
return w;
|
|
234
|
235
|
}
|
|
235
|
236
|
|
|
|
@@ -244,7 +245,7 @@ public class WriteLoops {
|
|
244
|
245
|
|
|
245
|
246
|
// this is a so called Enhanced for loop
|
|
246
|
247
|
for (int index : threes_array) {
|
|
247
|
|
- sumOfThrees = sumOfThrees + threes_array[index];
|
|
|
248
|
+ sumOfThrees = sumOfThrees + index;
|
|
248
|
249
|
// calling
|
|
249
|
250
|
w = w + 1;
|
|
250
|
251
|
// each time through the inner loop
|
|
|
@@ -259,17 +260,19 @@ public class WriteLoops {
|
|
259
|
260
|
// rewrite the loop inside of "foo()" as a standard for loop
|
|
260
|
261
|
// with 'i' as its index variable.
|
|
261
|
262
|
public int rewriteFooAsFor() {
|
|
262
|
|
- int w = 0;
|
|
|
263
|
+
|
|
263
|
264
|
int sumOfThrees = 0;
|
|
264
|
265
|
|
|
265
|
|
- // calling
|
|
266
|
|
- w = w + 1;
|
|
267
|
|
- // each time through the inner loop
|
|
|
266
|
+ for (int w=0; w<threes_array.length; w++ ){
|
|
|
267
|
+ // calling
|
|
|
268
|
+ sumOfThrees = sumOfThrees + 1;
|
|
268
|
269
|
|
|
|
270
|
+ // each time through the inner loop
|
|
|
271
|
+ }
|
|
269
|
272
|
System.out.print("The Sum is ");
|
|
270
|
273
|
System.out.println(sumOfThrees);
|
|
271
|
274
|
|
|
272
|
|
- return w;
|
|
|
275
|
+ return sumOfThrees;
|
|
273
|
276
|
}
|
|
274
|
277
|
|
|
275
|
278
|
// Ponder this: can all WHILE loops be rewritten as FOR loops?
|
|
|
@@ -278,10 +281,12 @@ public class WriteLoops {
|
|
278
|
281
|
int w = 0;
|
|
279
|
282
|
int sumOfThrees = 0;
|
|
280
|
283
|
|
|
281
|
|
- // calling
|
|
282
|
|
- w = w + 1;
|
|
283
|
|
- // each time through the inner loop
|
|
284
|
|
-
|
|
|
284
|
+ while (sumOfThrees < threes_array.length){
|
|
|
285
|
+ // calling
|
|
|
286
|
+ sumOfThrees = sumOfThrees + w;
|
|
|
287
|
+ w = w + 1;
|
|
|
288
|
+ // each time through the inner loop
|
|
|
289
|
+ }
|
|
285
|
290
|
System.out.print("The Sum is ");
|
|
286
|
291
|
System.out.println(sumOfThrees);
|
|
287
|
292
|
|
|
|
@@ -298,13 +303,16 @@ public class WriteLoops {
|
|
298
|
303
|
public int manageYardAndJunior() {
|
|
299
|
304
|
int w = 0;
|
|
300
|
305
|
boolean onTime = true;
|
|
301
|
|
-
|
|
302
|
|
- // ADD YOUR CODE here.
|
|
303
|
|
-
|
|
304
|
|
- // be sure to call
|
|
305
|
|
- w = w + 1;
|
|
306
|
|
- // each time inside the loop
|
|
307
|
|
-
|
|
|
306
|
+ boolean yardNeedsMowed = true;
|
|
|
307
|
+ while(isSummer()){
|
|
|
308
|
+ // ADD YOUR CODE here.
|
|
|
309
|
+ if(yardNeedsMowed){
|
|
|
310
|
+ yellAtJuniorToMowLawn();}
|
|
|
311
|
+ // be sure to call
|
|
|
312
|
+ w = w + 1;
|
|
|
313
|
+ // each time inside the loop
|
|
|
314
|
+ }
|
|
|
315
|
+ sendJuniorBackToSchool("Go to school");
|
|
308
|
316
|
return w;
|
|
309
|
317
|
}
|
|
310
|
318
|
|
|
|
@@ -317,11 +325,12 @@ public class WriteLoops {
|
|
317
|
325
|
public int tallyVote1() {
|
|
318
|
326
|
int w = 0;
|
|
319
|
327
|
int numberOfVotes = voteTallies.length;
|
|
320
|
|
-
|
|
|
328
|
+for (int idx=0; idx< voteTallies.length; idx++){
|
|
321
|
329
|
// calling
|
|
|
330
|
+ System.out.println(voteTallies[idx]);
|
|
322
|
331
|
w = w + 1;
|
|
323
|
332
|
// each time through the inner loop
|
|
324
|
|
-
|
|
|
333
|
+ }
|
|
325
|
334
|
return w;
|
|
326
|
335
|
}
|
|
327
|
336
|
|
|
|
@@ -331,11 +340,12 @@ public class WriteLoops {
|
|
331
|
340
|
public int tallyVote2() {
|
|
332
|
341
|
int w = 0;
|
|
333
|
342
|
int numberOfVotes = voteTallies.length;
|
|
334
|
|
-
|
|
|
343
|
+while (w<numberOfVotes){
|
|
335
|
344
|
// calling
|
|
|
345
|
+ System.out.println(voteTallies[w]);
|
|
336
|
346
|
w = w + 1;
|
|
337
|
347
|
// each time through the inner loop
|
|
338
|
|
-
|
|
|
348
|
+ }
|
|
339
|
349
|
return w;
|
|
340
|
350
|
}
|
|
341
|
351
|
|