|
@@ -20,7 +20,10 @@ public class WriteLoops {
|
20
|
20
|
|
21
|
21
|
// Write a FOR loop that counts from 1 to 10.
|
22
|
22
|
// calling
|
|
23
|
+ for (int i=1;i<=5;i++)
|
|
24
|
+ {
|
23
|
25
|
w = w + 1;
|
|
26
|
+ }
|
24
|
27
|
// each time through the loop
|
25
|
28
|
|
26
|
29
|
// this will tell the test how many times the loop executed.
|
|
@@ -32,7 +35,10 @@ public class WriteLoops {
|
32
|
35
|
|
33
|
36
|
// Write a FOR loop that counts from 1 to 10.
|
34
|
37
|
// calling
|
|
38
|
+ for (int i=1;i<=10;i++)
|
|
39
|
+ {
|
35
|
40
|
w = w + 1;
|
|
41
|
+ }
|
36
|
42
|
// each time through the loop
|
37
|
43
|
|
38
|
44
|
return w;
|
|
@@ -43,9 +49,11 @@ public class WriteLoops {
|
43
|
49
|
|
44
|
50
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
51
|
// calling
|
|
52
|
+ for (int i=21;i<=31;i++)
|
|
53
|
+ {
|
46
|
54
|
w = w + 1;
|
47
|
55
|
// each time through the loop
|
48
|
|
-
|
|
56
|
+ }
|
49
|
57
|
return w;
|
50
|
58
|
}
|
51
|
59
|
|
|
@@ -54,9 +62,11 @@ public class WriteLoops {
|
54
|
62
|
|
55
|
63
|
// Write a FOR loop that counts down from 100 to 0.
|
56
|
64
|
// calling
|
|
65
|
+ for(int i=100;i>0;i--)
|
|
66
|
+ {
|
57
|
67
|
w = w + 1;
|
58
|
68
|
// each time through the loop
|
59
|
|
-
|
|
69
|
+ }
|
60
|
70
|
return w;
|
61
|
71
|
}
|
62
|
72
|
|
|
@@ -65,17 +75,22 @@ public class WriteLoops {
|
65
|
75
|
|
66
|
76
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
77
|
// calling
|
|
78
|
+ for(int i=0;i>32;i+=2)
|
|
79
|
+ {
|
68
|
80
|
w = w + 1;
|
|
81
|
+ }
|
69
|
82
|
// each time through the loop
|
70
|
83
|
return w;
|
71
|
84
|
}
|
72
|
85
|
|
73
|
86
|
public int countDownFrom5000() {
|
74
|
87
|
int w = 0;
|
75
|
|
-
|
76
|
88
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
89
|
// calling
|
|
90
|
+ for(int i=5000;i>0;i-=11)
|
|
91
|
+ {
|
78
|
92
|
w = w + 1;
|
|
93
|
+ }
|
79
|
94
|
// each time through the loop
|
80
|
95
|
|
81
|
96
|
return w;
|
|
@@ -87,7 +102,13 @@ public class WriteLoops {
|
87
|
102
|
// Write a nested FOR loop(s), where one counts from
|
88
|
103
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
104
|
// calling
|
|
105
|
+ for(int i=0;i<20;i++)
|
|
106
|
+ {
|
|
107
|
+ for(int j=0;j<=4;j++)
|
|
108
|
+ {
|
90
|
109
|
w = w + 1;
|
|
110
|
+ }
|
|
111
|
+ }
|
91
|
112
|
// each time through the inner loop
|
92
|
113
|
|
93
|
114
|
return w;
|
|
@@ -95,16 +116,19 @@ public class WriteLoops {
|
95
|
116
|
|
96
|
117
|
public int helloZipCode() {
|
97
|
118
|
int w = 0;
|
98
|
|
-
|
99
|
119
|
// Write a FOR loop that counts from 5 to 105. Put an IF
|
100
|
120
|
// statement inside the loop that checks the
|
101
|
121
|
// loop index counter and if it’s greater than 51,
|
102
|
122
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
|
-
|
104
|
|
- // calling
|
105
|
|
- w = w + 1;
|
106
|
|
- // each time through the inner loop
|
107
|
|
-
|
|
123
|
+ // calling
|
|
124
|
+ for (int i = 5; i <= 105; i++)
|
|
125
|
+ {
|
|
126
|
+ if (i>51)
|
|
127
|
+ System.out.println("Hello Zipcode");
|
|
128
|
+ else
|
|
129
|
+ w = w + 1;
|
|
130
|
+ }
|
|
131
|
+ // each time through the inner loop
|
108
|
132
|
return w;
|
109
|
133
|
}
|
110
|
134
|
|
|
@@ -131,15 +155,15 @@ public class WriteLoops {
|
131
|
155
|
// After the loop is done, print “Honey, I’m Home!”
|
132
|
156
|
public int driveHome() {
|
133
|
157
|
int w = 0;
|
134
|
|
-
|
|
158
|
+ do {
|
|
159
|
+ driveSomeMore();
|
135
|
160
|
// you need to use a .equals for two Strings.
|
136
|
|
-
|
137
|
|
- // calling
|
138
|
|
- w = w + 1;
|
139
|
|
- // each time through the inner loop
|
140
|
|
-
|
141
|
|
-
|
142
|
|
- return w;
|
|
161
|
+ // calling
|
|
162
|
+ w = w + 1;
|
|
163
|
+ // each time through the inner loop
|
|
164
|
+ } while (gpsCurrentLocation() != "Home");
|
|
165
|
+ System.out.println("Honey, I'm Home!");
|
|
166
|
+ return w;
|
143
|
167
|
}
|
144
|
168
|
|
145
|
169
|
// Getting harder...
|
|
@@ -149,7 +173,7 @@ public class WriteLoops {
|
149
|
173
|
// "runningScore"
|
150
|
174
|
// and then sets “currentScore” to “gameNextScore()”
|
151
|
175
|
public int checkGameScore() {
|
152
|
|
- int w = 0;
|
|
176
|
+ int w = 1;
|
153
|
177
|
int highestScore = 236;
|
154
|
178
|
int currentScore = gameNextScore();
|
155
|
179
|
int runningScore = 0;
|
|
@@ -157,7 +181,12 @@ public class WriteLoops {
|
157
|
181
|
// do your while loop here
|
158
|
182
|
|
159
|
183
|
// calling
|
160
|
|
- w = w + 1;
|
|
184
|
+ while (runningScore <= highestScore)
|
|
185
|
+ {
|
|
186
|
+ runningScore = runningScore + currentScore;
|
|
187
|
+ w = w + 1;
|
|
188
|
+ currentScore = gameNextScore();
|
|
189
|
+ }
|
161
|
190
|
// each time through the inner loop
|
162
|
191
|
|
163
|
192
|
return w; // >= 3;
|
|
@@ -172,7 +201,13 @@ public class WriteLoops {
|
172
|
201
|
int runningScore = 0;
|
173
|
202
|
|
174
|
203
|
// do your while loop here
|
175
|
|
-
|
|
204
|
+ do
|
|
205
|
+ {
|
|
206
|
+ runningScore = runningScore + currentScore;
|
|
207
|
+ w = w + 1;
|
|
208
|
+ currentScore = gameNextScore();
|
|
209
|
+ }
|
|
210
|
+ while(runningScore <= highestScore);
|
176
|
211
|
// calling
|
177
|
212
|
w = w + 1;
|
178
|
213
|
// each time through the inner loop
|
|
@@ -184,15 +219,23 @@ public class WriteLoops {
|
184
|
219
|
// calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
|
185
|
220
|
// is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
|
186
|
221
|
// and also calls “tryServerRestart()”
|
187
|
|
- public int checkServerStatus() {
|
|
222
|
+ public int checkServerStatus()
|
|
223
|
+ {
|
188
|
224
|
int w = 0;
|
189
|
225
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
|
-
|
191
|
|
-
|
192
|
|
- // calling
|
193
|
|
- w = w + 1;
|
194
|
|
- // each time through the inner loop
|
195
|
|
-
|
|
226
|
+ boolean res = serverIsRunning();
|
|
227
|
+ while (res == true)
|
|
228
|
+ {
|
|
229
|
+ waitFor(5);
|
|
230
|
+ }
|
|
231
|
+ if (res != true)
|
|
232
|
+ {
|
|
233
|
+ // calling
|
|
234
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
235
|
+ tryServerRestart("Restart Server", adminPhoneNumber);
|
|
236
|
+ w = w + 1;
|
|
237
|
+ // each time through the inner loop
|
|
238
|
+ }
|
196
|
239
|
return w;
|
197
|
240
|
}
|
198
|
241
|
|
|
@@ -201,10 +244,14 @@ public class WriteLoops {
|
201
|
244
|
// and if it is, add 7 to “i”
|
202
|
245
|
public int loop50by7() {
|
203
|
246
|
int w = 0;
|
204
|
|
-
|
205
|
|
-
|
206
|
|
- // calling
|
|
247
|
+ int i = 7;
|
|
248
|
+ while(i<50)
|
|
249
|
+ {
|
|
250
|
+ i = i+7;
|
|
251
|
+ }
|
|
252
|
+ // calling
|
207
|
253
|
w = w + 1;
|
|
254
|
+
|
208
|
255
|
// each time through the inner loop
|
209
|
256
|
|
210
|
257
|
return w;
|
|
@@ -220,7 +267,8 @@ public class WriteLoops {
|
220
|
267
|
int sumOfThrees = 0;
|
221
|
268
|
|
222
|
269
|
// this is a so called Enhanced for loop
|
223
|
|
- for (int index : threes_array) {
|
|
270
|
+ for (int index : threes_array)
|
|
271
|
+ {
|
224
|
272
|
sumOfThrees = sumOfThrees + threes_array[index];
|
225
|
273
|
// calling
|
226
|
274
|
w = w + 1;
|
|
@@ -235,18 +283,20 @@ public class WriteLoops {
|
235
|
283
|
// Ponder this: can all FOR loops be rewritten as WHILE loops?
|
236
|
284
|
// rewrite the loop inside of "foo()" as a standard for loop
|
237
|
285
|
// with 'i' as its index variable.
|
238
|
|
- public int rewriteFooAsFor() {
|
|
286
|
+ public int rewriteFooAsFor()
|
|
287
|
+ {
|
239
|
288
|
int w = 0;
|
240
|
289
|
int sumOfThrees = 0;
|
241
|
|
-
|
242
|
|
-
|
243
|
|
- // calling
|
|
290
|
+ int str = threes_array.length;
|
|
291
|
+ for (int i = 0; i<= str; i++)
|
|
292
|
+ {
|
|
293
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
244
|
294
|
w = w + 1;
|
245
|
|
- // each time through the inner loop
|
246
|
|
-
|
|
295
|
+ }
|
|
296
|
+ // calling
|
|
297
|
+ // each time through the inner loop
|
247
|
298
|
System.out.print("The Sum is ");
|
248
|
299
|
System.out.println(sumOfThrees);
|
249
|
|
-
|
250
|
300
|
return w;
|
251
|
301
|
}
|
252
|
302
|
|
|
@@ -255,10 +305,17 @@ public class WriteLoops {
|
255
|
305
|
public int rewriteFooAsWhile() {
|
256
|
306
|
int w = 0;
|
257
|
307
|
int sumOfThrees = 0;
|
258
|
|
-
|
|
308
|
+ int i = 0;
|
|
309
|
+ int arraylength = threes_array.length;
|
|
310
|
+
|
|
311
|
+ while (i <= arraylength)
|
|
312
|
+ {
|
|
313
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
|
314
|
+ w = w + 1;
|
|
315
|
+ }
|
259
|
316
|
|
260
|
317
|
// calling
|
261
|
|
- w = w + 1;
|
|
318
|
+
|
262
|
319
|
// each time through the inner loop
|
263
|
320
|
|
264
|
321
|
System.out.print("The Sum is ");
|
|
@@ -274,16 +331,25 @@ public class WriteLoops {
|
274
|
331
|
// After loop, call
|
275
|
332
|
// “sendJuniorBackToSchool()” with an argument that decribes the day junior goes
|
276
|
333
|
// back.
|
277
|
|
- public int manageYardAndJunior() {
|
|
334
|
+ public int manageYardAndJunior()
|
|
335
|
+ {
|
278
|
336
|
int w = 0;
|
279
|
337
|
boolean onTime = true;
|
280
|
|
-
|
|
338
|
+ boolean yardNeedsMowed = true;
|
|
339
|
+
|
281
|
340
|
// ADD YOUR CODE here.
|
|
341
|
+ while (isSummer()) {
|
|
342
|
+
|
|
343
|
+ if (yardNeedsMowed == true) {
|
|
344
|
+ yellAtJuniorToMowLawn();
|
|
345
|
+ }
|
|
346
|
+ w = w + 1;
|
|
347
|
+ sendJuniorBackToSchool("");
|
282
|
348
|
|
283
|
349
|
// be sure to call
|
284
|
|
- w = w + 1;
|
|
350
|
+
|
285
|
351
|
// each time inside the loop
|
286
|
|
-
|
|
352
|
+ }
|
287
|
353
|
return w;
|
288
|
354
|
}
|
289
|
355
|
|
|
@@ -295,7 +361,11 @@ public class WriteLoops {
|
295
|
361
|
public int tallyVote1() {
|
296
|
362
|
int w = 0;
|
297
|
363
|
int numberOfVotes = voteTallies.length;
|
298
|
|
-
|
|
364
|
+ for(int i =0;i<numberOfVotes;i++)
|
|
365
|
+ {
|
|
366
|
+ System.out.println(voteTallies[i]+",");
|
|
367
|
+ }
|
|
368
|
+
|
299
|
369
|
|
300
|
370
|
// calling
|
301
|
371
|
w = w + 1;
|
|
@@ -310,7 +380,13 @@ public class WriteLoops {
|
310
|
380
|
public int tallyVote2() {
|
311
|
381
|
int w = 0;
|
312
|
382
|
int numberOfVotes = voteTallies.length;
|
313
|
|
-
|
|
383
|
+ int i =0;
|
|
384
|
+ while(i<numberOfVotes)
|
|
385
|
+
|
|
386
|
+ {
|
|
387
|
+ System.out.println(voteTallies[i]+",");
|
|
388
|
+ i++;
|
|
389
|
+ }
|
314
|
390
|
|
315
|
391
|
// calling
|
316
|
392
|
w = w + 1;
|