|
@@ -7,7 +7,7 @@ import java.util.function.Supplier;
|
7
|
7
|
/**
|
8
|
8
|
* Writeloops get you thinking about how to do different things with loops.
|
9
|
9
|
*
|
10
|
|
- * @author anonymous coward
|
|
10
|
+ * @author William Brown
|
11
|
11
|
* @version -0.3
|
12
|
12
|
*
|
13
|
13
|
*/
|
|
@@ -17,22 +17,23 @@ public class WriteLoops {
|
17
|
17
|
|
18
|
18
|
public int oneToFive() {
|
19
|
19
|
int w = 0;
|
20
|
|
-
|
21
|
20
|
// Write a FOR loop that counts from 1 to 10.
|
22
|
21
|
// calling
|
23
|
|
- w = w + 1;
|
24
|
22
|
// each time through the loop
|
25
|
|
-
|
|
23
|
+ for(int i = 1; i<= 5; i++){
|
|
24
|
+ w = w + 1;
|
|
25
|
+ }
|
26
|
26
|
// this will tell the test how many times the loop executed.
|
27
|
27
|
return w;
|
28
|
28
|
}
|
29
|
29
|
|
30
|
30
|
public int oneToTen() {
|
31
|
31
|
int w = 0;
|
32
|
|
-
|
33
|
32
|
// Write a FOR loop that counts from 1 to 10.
|
34
|
33
|
// calling
|
35
|
|
- w = w + 1;
|
|
34
|
+ for(int i = 1; i <= 10; i++){
|
|
35
|
+ w = w + 1;
|
|
36
|
+ }
|
36
|
37
|
// each time through the loop
|
37
|
38
|
|
38
|
39
|
return w;
|
|
@@ -43,7 +44,9 @@ public class WriteLoops {
|
43
|
44
|
|
44
|
45
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
46
|
// calling
|
46
|
|
- w = w + 1;
|
|
47
|
+ for(int i = 21; i <= 31; i++){
|
|
48
|
+ w = w + 1;
|
|
49
|
+ }
|
47
|
50
|
// each time through the loop
|
48
|
51
|
|
49
|
52
|
return w;
|
|
@@ -54,7 +57,9 @@ public class WriteLoops {
|
54
|
57
|
|
55
|
58
|
// Write a FOR loop that counts down from 100 to 0.
|
56
|
59
|
// calling
|
57
|
|
- w = w + 1;
|
|
60
|
+ for(int i = 100; i > 0; i --){
|
|
61
|
+ w = w + 1;
|
|
62
|
+ }
|
58
|
63
|
// each time through the loop
|
59
|
64
|
|
60
|
65
|
return w;
|
|
@@ -65,7 +70,9 @@ public class WriteLoops {
|
65
|
70
|
|
66
|
71
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
72
|
// calling
|
68
|
|
- w = w + 1;
|
|
73
|
+ for(int i = 0; i <= 17; i +=2){
|
|
74
|
+ w = w + 0;
|
|
75
|
+ }
|
69
|
76
|
// each time through the loop
|
70
|
77
|
return w;
|
71
|
78
|
}
|
|
@@ -75,7 +82,9 @@ public class WriteLoops {
|
75
|
82
|
|
76
|
83
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
84
|
// calling
|
78
|
|
- w = w + 1;
|
|
85
|
+ for(int i = 1; i < 5001; i += 11){
|
|
86
|
+ w = w + 1;
|
|
87
|
+ }
|
79
|
88
|
// each time through the loop
|
80
|
89
|
|
81
|
90
|
return w;
|
|
@@ -87,7 +96,11 @@ public class WriteLoops {
|
87
|
96
|
// Write a nested FOR loop(s), where one counts from
|
88
|
97
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
98
|
// calling
|
|
99
|
+ for(int i = 0; i < 20; i++){
|
|
100
|
+ for(int j = 0; j <= 4; j++){
|
90
|
101
|
w = w + 1;
|
|
102
|
+ }
|
|
103
|
+ }
|
91
|
104
|
// each time through the inner loop
|
92
|
105
|
|
93
|
106
|
return w;
|
|
@@ -102,7 +115,13 @@ public class WriteLoops {
|
102
|
115
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
116
|
|
104
|
117
|
// calling
|
|
118
|
+ for(int i = 5; i < 105; i++){
|
|
119
|
+ if(i > 51){
|
|
120
|
+ System.out.println("Hello Zipcode");
|
|
121
|
+ } else {
|
105
|
122
|
w = w + 1;
|
|
123
|
+ }
|
|
124
|
+ }
|
106
|
125
|
// each time through the inner loop
|
107
|
126
|
|
108
|
127
|
return w;
|
|
@@ -133,12 +152,13 @@ public class WriteLoops {
|
133
|
152
|
int w = 0;
|
134
|
153
|
|
135
|
154
|
// you need to use a .equals for two Strings.
|
136
|
|
-
|
|
155
|
+ while(!gpsCurrentLocation().equals("Home")){
|
|
156
|
+ driveSomeMore();
|
|
157
|
+ w = w + 1;
|
|
158
|
+ }
|
|
159
|
+ System.out.println("Honey, I'm Home");
|
137
|
160
|
// calling
|
138
|
|
- w = w + 1;
|
139
|
161
|
// each time through the inner loop
|
140
|
|
-
|
141
|
|
-
|
142
|
162
|
return w;
|
143
|
163
|
}
|
144
|
164
|
|
|
@@ -155,11 +175,13 @@ public class WriteLoops {
|
155
|
175
|
int runningScore = 0;
|
156
|
176
|
|
157
|
177
|
// do your while loop here
|
158
|
|
-
|
|
178
|
+ while(runningScore < highestScore){
|
|
179
|
+ runningScore += currentScore;
|
|
180
|
+ gameNextScore();
|
159
|
181
|
// calling
|
160
|
182
|
w = w + 1;
|
161
|
183
|
// each time through the inner loop
|
162
|
|
-
|
|
184
|
+ }
|
163
|
185
|
return w; // >= 3;
|
164
|
186
|
}
|
165
|
187
|
|
|
@@ -172,9 +194,12 @@ public class WriteLoops {
|
172
|
194
|
int runningScore = 0;
|
173
|
195
|
|
174
|
196
|
// do your while loop here
|
175
|
|
-
|
|
197
|
+ do{
|
|
198
|
+ runningScore += currentScore;
|
|
199
|
+ gameNextScore();
|
176
|
200
|
// calling
|
177
|
201
|
w = w + 1;
|
|
202
|
+ }while(runningScore < highestScore);
|
178
|
203
|
// each time through the inner loop
|
179
|
204
|
|
180
|
205
|
return w >= 3;
|
|
@@ -187,10 +212,16 @@ public class WriteLoops {
|
187
|
212
|
public int checkServerStatus() {
|
188
|
213
|
int w = 0;
|
189
|
214
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
|
-
|
|
215
|
+ while(serverIsRunning() == true){
|
|
216
|
+ waitFor(5);
|
|
217
|
+ w = w + 1;
|
|
218
|
+ }
|
|
219
|
+ if(serverIsRunning() == false){
|
|
220
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
221
|
+ tryServerRestart("Help!", adminPhoneNumber);
|
|
222
|
+ }
|
191
|
223
|
|
192
|
224
|
// calling
|
193
|
|
- w = w + 1;
|
194
|
225
|
// each time through the inner loop
|
195
|
226
|
|
196
|
227
|
return w;
|
|
@@ -200,14 +231,13 @@ public class WriteLoops {
|
200
|
231
|
// Write a WHILE loop that checks “i” is less than 50,
|
201
|
232
|
// and if it is, add 7 to “i”
|
202
|
233
|
public int loop50by7() {
|
203
|
|
- int w = 0;
|
204
|
|
-
|
205
|
|
-
|
|
234
|
+ int i = 7;
|
|
235
|
+ while(i < 50){
|
206
|
236
|
// calling
|
207
|
|
- w = w + 1;
|
|
237
|
+ i = i + 7;
|
208
|
238
|
// each time through the inner loop
|
209
|
|
-
|
210
|
|
- return w;
|
|
239
|
+ }
|
|
240
|
+ return i;
|
211
|
241
|
}
|
212
|
242
|
|
213
|
243
|
int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
|
|
@@ -220,8 +250,8 @@ public class WriteLoops {
|
220
|
250
|
int sumOfThrees = 0;
|
221
|
251
|
|
222
|
252
|
// this is a so called Enhanced for loop
|
223
|
|
- for (int index : threes_array) {
|
224
|
|
- sumOfThrees = sumOfThrees + threes_array[index];
|
|
253
|
+ for (int i = 0; i < threes_array.length; i ++ ) {
|
|
254
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
225
|
255
|
// calling
|
226
|
256
|
w = w + 1;
|
227
|
257
|
// each time through the inner loop
|
|
@@ -237,13 +267,16 @@ public class WriteLoops {
|
237
|
267
|
// with 'i' as its index variable.
|
238
|
268
|
public int rewriteFooAsFor() {
|
239
|
269
|
int w = 0;
|
|
270
|
+ // this is an array of ints. it is of length 7 (from 0 -> 6)
|
240
|
271
|
int sumOfThrees = 0;
|
241
|
272
|
|
242
|
|
-
|
|
273
|
+ // this is a so called Enhanced for loop
|
|
274
|
+ for (int i = 0; i < threes_array.length; i ++ ) {
|
|
275
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
243
|
276
|
// calling
|
244
|
277
|
w = w + 1;
|
245
|
278
|
// each time through the inner loop
|
246
|
|
-
|
|
279
|
+ }
|
247
|
280
|
System.out.print("The Sum is ");
|
248
|
281
|
System.out.println(sumOfThrees);
|
249
|
282
|
|
|
@@ -255,12 +288,14 @@ public class WriteLoops {
|
255
|
288
|
public int rewriteFooAsWhile() {
|
256
|
289
|
int w = 0;
|
257
|
290
|
int sumOfThrees = 0;
|
258
|
|
-
|
259
|
|
-
|
|
291
|
+ int i = 0;
|
|
292
|
+ while(i < threes_array.length){
|
|
293
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
260
|
294
|
// calling
|
261
|
295
|
w = w + 1;
|
262
|
296
|
// each time through the inner loop
|
263
|
|
-
|
|
297
|
+ i++;
|
|
298
|
+ }
|
264
|
299
|
System.out.print("The Sum is ");
|
265
|
300
|
System.out.println(sumOfThrees);
|
266
|
301
|
|
|
@@ -277,13 +312,18 @@ public class WriteLoops {
|
277
|
312
|
public int manageYardAndJunior() {
|
278
|
313
|
int w = 0;
|
279
|
314
|
boolean onTime = true;
|
|
315
|
+ boolean yardNeedsMowed = true;
|
280
|
316
|
|
281
|
317
|
// ADD YOUR CODE here.
|
282
|
|
-
|
|
318
|
+ while(isSummer()){
|
283
|
319
|
// be sure to call
|
|
320
|
+ if(yardNeedsMowed == true){
|
|
321
|
+ yellAtJuniorToMowLawn();
|
|
322
|
+ }
|
284
|
323
|
w = w + 1;
|
285
|
324
|
// each time inside the loop
|
286
|
|
-
|
|
325
|
+ }
|
|
326
|
+ sendJuniorBackToSchool("so boring");
|
287
|
327
|
return w;
|
288
|
328
|
}
|
289
|
329
|
|
|
@@ -295,12 +335,12 @@ public class WriteLoops {
|
295
|
335
|
public int tallyVote1() {
|
296
|
336
|
int w = 0;
|
297
|
337
|
int numberOfVotes = voteTallies.length;
|
298
|
|
-
|
299
|
|
-
|
|
338
|
+ for(int i = 0; i < numberOfVotes; i++){
|
300
|
339
|
// calling
|
|
340
|
+ System.out.println(voteTallies[i]);
|
301
|
341
|
w = w + 1;
|
302
|
342
|
// each time through the inner loop
|
303
|
|
-
|
|
343
|
+ }
|
304
|
344
|
return w;
|
305
|
345
|
}
|
306
|
346
|
|
|
@@ -310,12 +350,14 @@ public class WriteLoops {
|
310
|
350
|
public int tallyVote2() {
|
311
|
351
|
int w = 0;
|
312
|
352
|
int numberOfVotes = voteTallies.length;
|
313
|
|
-
|
314
|
|
-
|
|
353
|
+ int idx = 0;
|
|
354
|
+ while( idx < numberOfVotes ){
|
315
|
355
|
// calling
|
|
356
|
+ System.out.println(voteTallies[idx]);
|
|
357
|
+ idx++;
|
316
|
358
|
w = w + 1;
|
317
|
359
|
// each time through the inner loop
|
318
|
|
-
|
|
360
|
+ }
|
319
|
361
|
return w;
|
320
|
362
|
}
|
321
|
363
|
|