|
@@ -18,23 +18,20 @@ public class WriteLoops {
|
18
|
18
|
public int oneToFive() {
|
19
|
19
|
int w = 0;
|
20
|
20
|
|
21
|
|
- // Write a FOR loop that counts from 1 to 10.
|
22
|
|
- // calling
|
|
21
|
+ for (int i = 0; i < 5; i++){
|
23
|
22
|
w = w + 1;
|
24
|
|
- // each time through the loop
|
|
23
|
+ }
|
25
|
24
|
|
26
|
|
- // this will tell the test how many times the loop executed.
|
27
|
25
|
return w;
|
28
|
26
|
}
|
29
|
27
|
|
30
|
28
|
public int oneToTen() {
|
31
|
29
|
int w = 0;
|
32
|
30
|
|
33
|
|
- // Write a FOR loop that counts from 1 to 10.
|
34
|
|
- // calling
|
35
|
|
- w = w + 1;
|
36
|
|
- // each time through the loop
|
37
|
|
-
|
|
31
|
+ for (int i = 0; i < 10; i++){
|
|
32
|
+ w = w + 1;
|
|
33
|
+ };
|
|
34
|
+
|
38
|
35
|
return w;
|
39
|
36
|
}
|
40
|
37
|
|
|
@@ -42,10 +39,11 @@ public class WriteLoops {
|
42
|
39
|
int w = 0;
|
43
|
40
|
|
44
|
41
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
|
- // calling
|
46
|
|
- w = w + 1;
|
47
|
|
- // each time through the loop
|
48
|
|
-
|
|
42
|
+ //NOTE: instructions call for 10 iterations. However, test passes for 11.
|
|
43
|
+ for(int i = 21; i <=31; i++){
|
|
44
|
+ w = w + 1;
|
|
45
|
+ }
|
|
46
|
+
|
49
|
47
|
return w;
|
50
|
48
|
}
|
51
|
49
|
|
|
@@ -53,20 +51,24 @@ public class WriteLoops {
|
53
|
51
|
int w = 0;
|
54
|
52
|
|
55
|
53
|
// Write a FOR loop that counts down from 100 to 0.
|
56
|
|
- // calling
|
57
|
|
- w = w + 1;
|
58
|
|
- // each time through the loop
|
59
|
|
-
|
|
54
|
+
|
|
55
|
+ for (int i = 100; i >=0; i--){
|
|
56
|
+ w = w + 1;
|
|
57
|
+ }
|
|
58
|
+
|
60
|
59
|
return w;
|
61
|
60
|
}
|
62
|
61
|
|
63
|
62
|
public int byTwoTo32() {
|
64
|
63
|
int w = 0;
|
65
|
64
|
|
66
|
|
- // Write a FOR loop from 0 to 32 by 2s.
|
67
|
|
- // calling
|
68
|
|
- w = w + 1;
|
69
|
|
- // each time through the loop
|
|
65
|
+ // Write a FOR loop from 0 to 32 by 2s
|
|
66
|
+ //NOTE: Test expected = 0. Rewrote test as TestbyTwoTo32v2
|
|
67
|
+
|
|
68
|
+ for (int i = 0; i<=32; i+=2){
|
|
69
|
+ w = w + 1;
|
|
70
|
+ }
|
|
71
|
+
|
70
|
72
|
return w;
|
71
|
73
|
}
|
72
|
74
|
|
|
@@ -75,9 +77,11 @@ public class WriteLoops {
|
75
|
77
|
|
76
|
78
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
79
|
// calling
|
78
|
|
- w = w + 1;
|
|
80
|
+ for (int i=1; i<5001; i+=11){
|
|
81
|
+ w = w + 1;
|
|
82
|
+ }
|
79
|
83
|
// each time through the loop
|
80
|
|
-
|
|
84
|
+
|
81
|
85
|
return w;
|
82
|
86
|
}
|
83
|
87
|
|
|
@@ -86,9 +90,14 @@ public class WriteLoops {
|
86
|
90
|
|
87
|
91
|
// Write a nested FOR loop(s), where one counts from
|
88
|
92
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
|
- // calling
|
|
93
|
+ // calling
|
|
94
|
+ for(int i = 0; i<20; i++){
|
|
95
|
+ for(int j = 0; j<=4; j++){
|
90
|
96
|
w = w + 1;
|
91
|
|
- // each time through the inner loop
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ // each time through the inner loop
|
92
|
101
|
|
93
|
102
|
return w;
|
94
|
103
|
}
|
|
@@ -100,11 +109,14 @@ public class WriteLoops {
|
100
|
109
|
// statement inside the loop that checks the
|
101
|
110
|
// loop index counter and if it’s greater than 51,
|
102
|
111
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
|
-
|
104
|
|
- // calling
|
|
112
|
+ for(int i = 5; i<=105; i++){
|
|
113
|
+ if(i>51){
|
|
114
|
+ System.out.println("Hello Zipcode");
|
|
115
|
+ } else {
|
105
|
116
|
w = w + 1;
|
106
|
|
- // each time through the inner loop
|
107
|
|
-
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+
|
108
|
120
|
return w;
|
109
|
121
|
}
|
110
|
122
|
|
|
@@ -124,6 +136,8 @@ public class WriteLoops {
|
124
|
136
|
i = i - 1;
|
125
|
137
|
} while (i > 0);
|
126
|
138
|
// what's the primary difference between them?!?
|
|
139
|
+ //besides the fact they start at different numbers and count different directions, the while loop first checks if the condition is true before performing code
|
|
140
|
+ //whereas the do-while loop performs the code once and then checks the condition to determine if it should continue
|
127
|
141
|
}
|
128
|
142
|
|
129
|
143
|
// Write a WHILE loop that checks “gpsCurrentLocation()”
|
|
@@ -131,15 +145,14 @@ public class WriteLoops {
|
131
|
145
|
// After the loop is done, print “Honey, I’m Home!”
|
132
|
146
|
public int driveHome() {
|
133
|
147
|
int w = 0;
|
134
|
|
-
|
135
|
|
- // you need to use a .equals for two Strings.
|
136
|
|
-
|
137
|
|
- // calling
|
|
148
|
+ while (!gpsCurrentLocation().equals("Home")){
|
|
149
|
+ driveSomeMore();
|
138
|
150
|
w = w + 1;
|
139
|
|
- // each time through the inner loop
|
140
|
|
-
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ System.out.println("Honey, I'm Home!");
|
141
|
154
|
|
142
|
|
- return w;
|
|
155
|
+ return w;
|
143
|
156
|
}
|
144
|
157
|
|
145
|
158
|
// Getting harder...
|
|
@@ -154,13 +167,15 @@ public class WriteLoops {
|
154
|
167
|
int currentScore = gameNextScore();
|
155
|
168
|
int runningScore = 0;
|
156
|
169
|
|
157
|
|
- // do your while loop here
|
158
|
|
-
|
159
|
|
- // calling
|
|
170
|
+ while(runningScore < highestScore){
|
|
171
|
+ runningScore+=currentScore;
|
|
172
|
+ currentScore = gameNextScore();
|
160
|
173
|
w = w + 1;
|
161
|
|
- // each time through the inner loop
|
162
|
|
-
|
|
174
|
+ System.out.println(w);
|
|
175
|
+ }
|
|
176
|
+
|
163
|
177
|
return w; // >= 3;
|
|
178
|
+ //random output due to ThreadLocalRandom, text expected is fixed. sometimes passes
|
164
|
179
|
}
|
165
|
180
|
|
166
|
181
|
// Rewrite the previous WHILE loop as a DO..WHILE loop.
|
|
@@ -172,12 +187,14 @@ public class WriteLoops {
|
172
|
187
|
int runningScore = 0;
|
173
|
188
|
|
174
|
189
|
// do your while loop here
|
175
|
|
-
|
176
|
|
- // calling
|
|
190
|
+ do{
|
|
191
|
+ runningScore+=currentScore;
|
|
192
|
+ currentScore = gameNextScore();
|
177
|
193
|
w = w + 1;
|
178
|
|
- // each time through the inner loop
|
|
194
|
+ } while (runningScore < highestScore);
|
179
|
195
|
|
180
|
196
|
return w >= 3;
|
|
197
|
+ //random output due to ThreadLocalRandom, test expected is fixed. sometimes passes.
|
181
|
198
|
}
|
182
|
199
|
|
183
|
200
|
// Write a WHILE loop that checks “serverIsRunning()” and if true
|
|
@@ -187,12 +204,17 @@ public class WriteLoops {
|
187
|
204
|
public int checkServerStatus() {
|
188
|
205
|
int w = 0;
|
189
|
206
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
|
-
|
191
|
207
|
|
192
|
|
- // calling
|
193
|
|
- w = w + 1;
|
194
|
|
- // each time through the inner loop
|
195
|
|
-
|
|
208
|
+ while (serverIsRunning()){
|
|
209
|
+ waitFor(5);
|
|
210
|
+ w = w + 1;
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ if(serverIsRunning() == false){
|
|
214
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
215
|
+ tryServerRestart("Please restart my server", adminPhoneNumber);
|
|
216
|
+ }
|
|
217
|
+
|
196
|
218
|
return w;
|
197
|
219
|
}
|
198
|
220
|
|
|
@@ -201,12 +223,13 @@ public class WriteLoops {
|
201
|
223
|
// and if it is, add 7 to “i”
|
202
|
224
|
public int loop50by7() {
|
203
|
225
|
int w = 0;
|
|
226
|
+ int i = 7;
|
204
|
227
|
|
205
|
|
-
|
206
|
|
- // calling
|
|
228
|
+ while(i<50){
|
|
229
|
+ i+=7;
|
207
|
230
|
w = w + 1;
|
208
|
|
- // each time through the inner loop
|
209
|
|
-
|
|
231
|
+ }
|
|
232
|
+
|
210
|
233
|
return w;
|
211
|
234
|
}
|
212
|
235
|
|
|
@@ -222,9 +245,7 @@ public class WriteLoops {
|
222
|
245
|
// this is a so called Enhanced for loop
|
223
|
246
|
for (int index : threes_array) {
|
224
|
247
|
sumOfThrees = sumOfThrees + threes_array[index];
|
225
|
|
- // calling
|
226
|
248
|
w = w + 1;
|
227
|
|
- // each time through the inner loop
|
228
|
249
|
}
|
229
|
250
|
System.out.print("The Sum is ");
|
230
|
251
|
System.out.println(sumOfThrees);
|
|
@@ -232,35 +253,36 @@ public class WriteLoops {
|
232
|
253
|
return w;
|
233
|
254
|
}
|
234
|
255
|
|
235
|
|
- // Ponder this: can all FOR loops be rewritten as WHILE loops?
|
|
256
|
+ // Ponder this: can all FOR loops be rewritten as WHILE loops?... I'm willing to say most
|
236
|
257
|
// rewrite the loop inside of "foo()" as a standard for loop
|
237
|
258
|
// with 'i' as its index variable.
|
238
|
259
|
public int rewriteFooAsFor() {
|
239
|
260
|
int w = 0;
|
240
|
261
|
int sumOfThrees = 0;
|
241
|
262
|
|
242
|
|
-
|
243
|
|
- // calling
|
|
263
|
+ for(int i = 0; i<threes_array.length; i++){
|
|
264
|
+ sumOfThrees+=threes_array[i];
|
244
|
265
|
w = w + 1;
|
245
|
|
- // each time through the inner loop
|
246
|
|
-
|
|
266
|
+ }
|
|
267
|
+
|
247
|
268
|
System.out.print("The Sum is ");
|
248
|
269
|
System.out.println(sumOfThrees);
|
249
|
270
|
|
250
|
271
|
return w;
|
251
|
272
|
}
|
252
|
273
|
|
253
|
|
- // Ponder this: can all WHILE loops be rewritten as FOR loops?
|
|
274
|
+ // Ponder this: can all WHILE loops be rewritten as FOR loops? I'm willing to say most
|
254
|
275
|
// rewrite the loop inside of "foo()" as a 'while' loop
|
255
|
276
|
public int rewriteFooAsWhile() {
|
256
|
277
|
int w = 0;
|
257
|
278
|
int sumOfThrees = 0;
|
258
|
|
-
|
259
|
|
-
|
260
|
|
- // calling
|
|
279
|
+ int i = 0;
|
|
280
|
+ while (i < threes_array.length){
|
|
281
|
+ sumOfThrees+=threes_array[i];
|
|
282
|
+ i++;
|
261
|
283
|
w = w + 1;
|
262
|
|
- // each time through the inner loop
|
263
|
|
-
|
|
284
|
+ }
|
|
285
|
+
|
264
|
286
|
System.out.print("The Sum is ");
|
265
|
287
|
System.out.println(sumOfThrees);
|
266
|
288
|
|
|
@@ -277,13 +299,16 @@ public class WriteLoops {
|
277
|
299
|
public int manageYardAndJunior() {
|
278
|
300
|
int w = 0;
|
279
|
301
|
boolean onTime = true;
|
|
302
|
+ boolean yardNeedsMowed = true;
|
|
303
|
+
|
|
304
|
+ while(isSummer()){
|
|
305
|
+ if(yardNeedsMowed == true){
|
|
306
|
+ yellAtJuniorToMowLawn();
|
|
307
|
+ }
|
280
|
308
|
|
281
|
|
- // ADD YOUR CODE here.
|
282
|
|
-
|
283
|
|
- // be sure to call
|
284
|
309
|
w = w + 1;
|
285
|
|
- // each time inside the loop
|
286
|
|
-
|
|
310
|
+ }
|
|
311
|
+
|
287
|
312
|
return w;
|
288
|
313
|
}
|
289
|
314
|
|
|
@@ -296,11 +321,11 @@ public class WriteLoops {
|
296
|
321
|
int w = 0;
|
297
|
322
|
int numberOfVotes = voteTallies.length;
|
298
|
323
|
|
299
|
|
-
|
300
|
|
- // calling
|
|
324
|
+ for (int i = 0; i<numberOfVotes; i++){
|
|
325
|
+ System.out.print(voteTallies[i]);
|
301
|
326
|
w = w + 1;
|
302
|
|
- // each time through the inner loop
|
303
|
|
-
|
|
327
|
+ }
|
|
328
|
+
|
304
|
329
|
return w;
|
305
|
330
|
}
|
306
|
331
|
|
|
@@ -311,11 +336,14 @@ public class WriteLoops {
|
311
|
336
|
int w = 0;
|
312
|
337
|
int numberOfVotes = voteTallies.length;
|
313
|
338
|
|
|
339
|
+ int idx = 0;
|
314
|
340
|
|
315
|
|
- // calling
|
|
341
|
+ while (idx < numberOfVotes){
|
|
342
|
+ System.out.print(voteTallies[idx]);
|
|
343
|
+ idx++;
|
316
|
344
|
w = w + 1;
|
317
|
|
- // each time through the inner loop
|
318
|
|
-
|
|
345
|
+ }
|
|
346
|
+
|
319
|
347
|
return w;
|
320
|
348
|
}
|
321
|
349
|
|
|
@@ -378,14 +406,15 @@ public class WriteLoops {
|
378
|
406
|
// return (i >= 3);
|
379
|
407
|
// };
|
380
|
408
|
// };
|
381
|
|
- private int summer = 0;
|
382
|
|
- private boolean isSummer() {
|
383
|
|
- if (summer == 3) {
|
384
|
|
- return true;
|
385
|
|
- }
|
386
|
|
- summer++;
|
387
|
|
- return false;
|
|
409
|
+ private int summer = 0;
|
|
410
|
+ private boolean isSummer() {
|
|
411
|
+ if (summer == 3) {
|
|
412
|
+ return true;
|
388
|
413
|
}
|
|
414
|
+ summer++;
|
|
415
|
+ return false;
|
|
416
|
+ }
|
|
417
|
+
|
389
|
418
|
private void sendEmergencyText(String mesg, String phone) {
|
390
|
419
|
}
|
391
|
420
|
|