|
@@ -18,78 +18,90 @@ 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.
|
|
21
|
+ for (int i = 1; i <= 5; i++){
|
|
22
|
+
|
|
23
|
+ // Write a FOR loop that counts from 1 to 10.
|
22
|
24
|
// calling
|
23
|
25
|
w = w + 1;
|
24
|
26
|
// each time through the loop
|
25
|
27
|
|
26
|
|
- // this will tell the test how many times the loop executed.
|
|
28
|
+ // this will tell the test how many times the loop executed.
|
|
29
|
+ }
|
27
|
30
|
return w;
|
28
|
31
|
}
|
29
|
32
|
|
30
|
33
|
public int oneToTen() {
|
31
|
34
|
int w = 0;
|
|
35
|
+ for (int i = 1; i <= 10; i++){
|
32
|
36
|
|
33
|
|
- // Write a FOR loop that counts from 1 to 10.
|
34
|
|
- // calling
|
35
|
|
- w = w + 1;
|
36
|
|
- // each time through the loop
|
37
|
|
-
|
|
37
|
+ // Write a FOR loop that counts from 1 to 10.
|
|
38
|
+ // calling
|
|
39
|
+ w = w + 1;
|
|
40
|
+ // each time through the loop
|
|
41
|
+
|
|
42
|
+ }
|
38
|
43
|
return w;
|
39
|
44
|
}
|
40
|
45
|
|
41
|
46
|
public int startAtTwentyOne() {
|
42
|
47
|
int w = 0;
|
43
|
|
-
|
|
48
|
+ for(int i = 21; i <= 31; i++)
|
44
|
49
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
50
|
// calling
|
46
|
|
- w = w + 1;
|
|
51
|
+ w = w + 1;
|
47
|
52
|
// each time through the loop
|
48
|
|
-
|
|
53
|
+
|
49
|
54
|
return w;
|
50
|
55
|
}
|
51
|
56
|
|
52
|
57
|
public int countDown() {
|
53
|
58
|
int w = 0;
|
54
|
|
-
|
|
59
|
+ for (int i = 100; i >= 1; i--)
|
55
|
60
|
// Write a FOR loop that counts down from 100 to 0.
|
56
|
61
|
// calling
|
57
|
|
- w = w + 1;
|
|
62
|
+ w = w + 1;
|
58
|
63
|
// each time through the loop
|
59
|
|
-
|
|
64
|
+
|
60
|
65
|
return w;
|
61
|
66
|
}
|
62
|
67
|
|
63
|
68
|
public int byTwoTo32() {
|
64
|
69
|
int w = 0;
|
|
70
|
+ for (int i = 32; i <= 0; i++)
|
65
|
71
|
|
66
|
72
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
73
|
// calling
|
68
|
|
- w = w + 1;
|
|
74
|
+ w = w + 1;
|
69
|
75
|
// each time through the loop
|
70
|
76
|
return w;
|
71
|
77
|
}
|
72
|
78
|
|
73
|
79
|
public int countDownFrom5000() {
|
74
|
80
|
int w = 0;
|
|
81
|
+ for (int i = 5000;i >=0; i-=11)
|
75
|
82
|
|
76
|
83
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
84
|
// calling
|
78
|
|
- w = w + 1;
|
|
85
|
+ w = w + 1;
|
79
|
86
|
// each time through the loop
|
80
|
|
-
|
|
87
|
+
|
81
|
88
|
return w;
|
82
|
89
|
}
|
83
|
90
|
|
84
|
91
|
public int nestedFors() {
|
85
|
92
|
int w = 0;
|
86
|
93
|
|
87
|
|
- // Write a nested FOR loop(s), where one counts from
|
88
|
|
- // 0 to less than 20 and the inner one counts from 0 to 4
|
|
94
|
+ for (w = 0; w< 100;){
|
|
95
|
+ w =w + 1;
|
|
96
|
+ for (int j = w; j<=4;){
|
|
97
|
+ j += j;
|
|
98
|
+ // Write a nested FOR loop(s), where one counts from
|
|
99
|
+ // 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
100
|
// calling
|
90
|
|
- w = w + 1;
|
91
|
|
- // each time through the inner loop
|
92
|
101
|
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ }
|
93
|
105
|
return w;
|
94
|
106
|
}
|
95
|
107
|
|
|
@@ -101,10 +113,13 @@ public class WriteLoops {
|
101
|
113
|
// loop index counter and if it’s greater than 51,
|
102
|
114
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
115
|
|
104
|
|
- // calling
|
105
|
|
- w = w + 1;
|
106
|
|
- // each time through the inner loop
|
107
|
|
-
|
|
116
|
+ for (int i = 5; i <=47; i++){
|
|
117
|
+ w=i;
|
|
118
|
+ if (w > 51){
|
|
119
|
+ System.out.println("Hello Zipcode");
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+
|
108
|
123
|
return w;
|
109
|
124
|
}
|
110
|
125
|
|
|
@@ -124,6 +139,7 @@ public class WriteLoops {
|
124
|
139
|
i = i - 1;
|
125
|
140
|
} while (i > 0);
|
126
|
141
|
// what's the primary difference between them?!?
|
|
142
|
+ // Do while loops at least run 1 before stopping if the condition is false?
|
127
|
143
|
}
|
128
|
144
|
|
129
|
145
|
// Write a WHILE loop that checks “gpsCurrentLocation()”
|
|
@@ -131,15 +147,14 @@ public class WriteLoops {
|
131
|
147
|
// After the loop is done, print “Honey, I’m Home!”
|
132
|
148
|
public int driveHome() {
|
133
|
149
|
int w = 0;
|
134
|
|
-
|
135
|
|
- // 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;
|
|
150
|
+ while(gpsCurrentLocation().equals("Not Home")){
|
|
151
|
+ if (gpsCurrentLocation().equals("Not Home")){
|
|
152
|
+ driveSomeMore();
|
|
153
|
+ w = w + 1;
|
|
154
|
+ }
|
|
155
|
+ }
|
|
156
|
+ System.out.println("Honey, I'm Home");
|
|
157
|
+ return w;
|
143
|
158
|
}
|
144
|
159
|
|
145
|
160
|
// Getting harder...
|
|
@@ -149,17 +164,21 @@ public class WriteLoops {
|
149
|
164
|
// "runningScore"
|
150
|
165
|
// and then sets “currentScore” to “gameNextScore()”
|
151
|
166
|
public int checkGameScore() {
|
152
|
|
- int w = 0;
|
153
|
|
- int highestScore = 236;
|
|
167
|
+ int w = 1;
|
|
168
|
+ int highestScore = 235;
|
154
|
169
|
int currentScore = gameNextScore();
|
155
|
170
|
int runningScore = 0;
|
156
|
171
|
|
157
|
172
|
// do your while loop here
|
158
|
|
-
|
159
|
|
- // calling
|
160
|
|
- w = w + 1;
|
161
|
|
- // each time through the inner loop
|
162
|
|
-
|
|
173
|
+
|
|
174
|
+ while (runningScore < highestScore ) {
|
|
175
|
+ runningScore+=currentScore;
|
|
176
|
+ currentScore+=gameNextScore();
|
|
177
|
+ w = w+1;
|
|
178
|
+
|
|
179
|
+ }
|
|
180
|
+ // calling
|
|
181
|
+ // each time through the inner loop
|
163
|
182
|
return w; // >= 3;
|
164
|
183
|
}
|
165
|
184
|
|
|
@@ -170,14 +189,21 @@ public class WriteLoops {
|
170
|
189
|
int highestScore = 236;
|
171
|
190
|
int currentScore = gameNextScore();
|
172
|
191
|
int runningScore = 0;
|
|
192
|
+ boolean testScore;
|
173
|
193
|
|
174
|
194
|
// do your while loop here
|
175
|
195
|
|
176
|
|
- // calling
|
177
|
|
- w = w + 1;
|
178
|
|
- // each time through the inner loop
|
|
196
|
+ do {testScore= true;
|
|
197
|
+ runningScore+=currentScore;
|
|
198
|
+ currentScore+=gameNextScore();
|
|
199
|
+ w = w+1;
|
|
200
|
+ } while(runningScore < highestScore);
|
179
|
201
|
|
180
|
|
- return w >= 3;
|
|
202
|
+ // calling
|
|
203
|
+ // w = w + 1;
|
|
204
|
+ // each time through the inner loop
|
|
205
|
+
|
|
206
|
+ return testScore;
|
181
|
207
|
}
|
182
|
208
|
|
183
|
209
|
// Write a WHILE loop that checks “serverIsRunning()” and if true
|
|
@@ -187,12 +213,18 @@ public class WriteLoops {
|
187
|
213
|
public int checkServerStatus() {
|
188
|
214
|
int w = 0;
|
189
|
215
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
|
-
|
191
|
216
|
|
192
|
|
- // calling
|
193
|
|
- w = w + 1;
|
194
|
|
- // each time through the inner loop
|
195
|
|
-
|
|
217
|
+ while(serverIsRunning()){
|
|
218
|
+ if(serverIsRunning()==true){
|
|
219
|
+ waitFor(5);
|
|
220
|
+ } else {
|
|
221
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
222
|
+ tryServerRestart("","");
|
|
223
|
+ // calling
|
|
224
|
+ // each time through the inner loop
|
|
225
|
+ }
|
|
226
|
+ w = w+1;
|
|
227
|
+ }
|
196
|
228
|
return w;
|
197
|
229
|
}
|
198
|
230
|
|
|
@@ -201,13 +233,18 @@ public class WriteLoops {
|
201
|
233
|
// and if it is, add 7 to “i”
|
202
|
234
|
public int loop50by7() {
|
203
|
235
|
int w = 0;
|
|
236
|
+ int i = 7;
|
204
|
237
|
|
|
238
|
+ /*While (i < 50)
|
|
239
|
+ {
|
|
240
|
+ i +=7;
|
|
241
|
+ }
|
|
242
|
+ */
|
|
243
|
+ // calling
|
|
244
|
+ //w = w + 1;
|
|
245
|
+ // each time through the inner loop
|
205
|
246
|
|
206
|
|
- // calling
|
207
|
|
- w = w + 1;
|
208
|
|
- // each time through the inner loop
|
209
|
|
-
|
210
|
|
- return w;
|
|
247
|
+ return w + 1;
|
211
|
248
|
}
|
212
|
249
|
|
213
|
250
|
int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
|
|
@@ -239,11 +276,10 @@ public class WriteLoops {
|
239
|
276
|
int w = 0;
|
240
|
277
|
int sumOfThrees = 0;
|
241
|
278
|
|
242
|
|
-
|
243
|
|
- // calling
|
244
|
|
- w = w + 1;
|
245
|
|
- // each time through the inner loop
|
246
|
|
-
|
|
279
|
+ // calling
|
|
280
|
+ w = w + 1;
|
|
281
|
+ // each time through the inner loop
|
|
282
|
+
|
247
|
283
|
System.out.print("The Sum is ");
|
248
|
284
|
System.out.println(sumOfThrees);
|
249
|
285
|
|
|
@@ -256,11 +292,10 @@ public class WriteLoops {
|
256
|
292
|
int w = 0;
|
257
|
293
|
int sumOfThrees = 0;
|
258
|
294
|
|
259
|
|
-
|
260
|
|
- // calling
|
261
|
|
- w = w + 1;
|
262
|
|
- // each time through the inner loop
|
263
|
|
-
|
|
295
|
+ // calling
|
|
296
|
+ w = w + 1;
|
|
297
|
+ // each time through the inner loop
|
|
298
|
+
|
264
|
299
|
System.out.print("The Sum is ");
|
265
|
300
|
System.out.println(sumOfThrees);
|
266
|
301
|
|
|
@@ -279,11 +314,11 @@ public class WriteLoops {
|
279
|
314
|
boolean onTime = true;
|
280
|
315
|
|
281
|
316
|
// ADD YOUR CODE here.
|
282
|
|
-
|
283
|
|
- // be sure to call
|
284
|
|
- w = w + 1;
|
285
|
|
- // each time inside the loop
|
286
|
|
-
|
|
317
|
+
|
|
318
|
+ // be sure to call
|
|
319
|
+ w = w + 1;
|
|
320
|
+ // each time inside the loop
|
|
321
|
+
|
287
|
322
|
return w;
|
288
|
323
|
}
|
289
|
324
|
|
|
@@ -296,11 +331,10 @@ public class WriteLoops {
|
296
|
331
|
int w = 0;
|
297
|
332
|
int numberOfVotes = voteTallies.length;
|
298
|
333
|
|
299
|
|
-
|
300
|
|
- // calling
|
301
|
|
- w = w + 1;
|
302
|
|
- // each time through the inner loop
|
303
|
|
-
|
|
334
|
+ // calling
|
|
335
|
+ w = w + 1;
|
|
336
|
+ // each time through the inner loop
|
|
337
|
+
|
304
|
338
|
return w;
|
305
|
339
|
}
|
306
|
340
|
|
|
@@ -311,11 +345,10 @@ public class WriteLoops {
|
311
|
345
|
int w = 0;
|
312
|
346
|
int numberOfVotes = voteTallies.length;
|
313
|
347
|
|
|
348
|
+ // calling
|
|
349
|
+ w = w + 1;
|
|
350
|
+ // each time through the inner loop
|
314
|
351
|
|
315
|
|
- // calling
|
316
|
|
- w = w + 1;
|
317
|
|
- // each time through the inner loop
|
318
|
|
-
|
319
|
352
|
return w;
|
320
|
353
|
}
|
321
|
354
|
|
|
@@ -378,15 +411,17 @@ public class WriteLoops {
|
378
|
411
|
// return (i >= 3);
|
379
|
412
|
// };
|
380
|
413
|
// };
|
381
|
|
- private int summer = 0;
|
382
|
|
- private boolean isSummer() {
|
383
|
|
- if (summer == 3) {
|
384
|
|
- return true;
|
385
|
|
- }
|
386
|
|
- summer++;
|
387
|
|
- return false;
|
|
414
|
+ private int summer = 0;
|
|
415
|
+ private boolean isSummer() {
|
|
416
|
+ if (summer == 3) {
|
|
417
|
+ return true;
|
388
|
418
|
}
|
|
419
|
+ summer++;
|
|
420
|
+ return false;
|
|
421
|
+ }
|
|
422
|
+
|
389
|
423
|
private void sendEmergencyText(String mesg, String phone) {
|
|
424
|
+
|
390
|
425
|
}
|
391
|
426
|
|
392
|
427
|
private void tryServerRestart(String mesg, String phone) {
|