|
@@ -19,22 +19,22 @@ public class WriteLoops {
|
19
|
19
|
int w = 0;
|
20
|
20
|
|
21
|
21
|
// Write a FOR loop that counts from 1 to 10.
|
22
|
|
- // calling
|
|
22
|
+ // calling
|
|
23
|
+ for(int i = 0;i < 5; i++){
|
23
|
24
|
w = w + 1;
|
24
|
25
|
// each time through the loop
|
25
|
|
-
|
|
26
|
+ }
|
26
|
27
|
// this will tell the test how many times the loop executed.
|
27
|
28
|
return w;
|
28
|
29
|
}
|
29
|
30
|
|
30
|
31
|
public int oneToTen() {
|
31
|
32
|
int w = 0;
|
|
33
|
+ for(int i = 0;i < 10; i++){
|
|
34
|
+ w = w + 1;
|
|
35
|
+ // each time through the loop
|
|
36
|
+ }
|
32
|
37
|
|
33
|
|
- // Write a FOR loop that counts from 1 to 10.
|
34
|
|
- // calling
|
35
|
|
- w = w + 1;
|
36
|
|
- // each time through the loop
|
37
|
|
-
|
38
|
38
|
return w;
|
39
|
39
|
}
|
40
|
40
|
|
|
@@ -43,31 +43,35 @@ public class WriteLoops {
|
43
|
43
|
|
44
|
44
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
45
|
// calling
|
46
|
|
- w = w + 1;
|
47
|
46
|
// each time through the loop
|
48
|
|
-
|
|
47
|
+ for(int i = 21;i > 10; i--){
|
|
48
|
+ w = w + 1;
|
|
49
|
+ // each time through the loop
|
|
50
|
+ }
|
49
|
51
|
return w;
|
50
|
52
|
}
|
51
|
53
|
|
52
|
54
|
public int countDown() {
|
53
|
55
|
int w = 0;
|
54
|
56
|
|
55
|
|
- // Write a FOR loop that counts down from 100 to 0.
|
56
|
|
- // calling
|
57
|
|
- w = w + 1;
|
58
|
|
- // each time through the loop
|
59
|
|
-
|
|
57
|
+ for(int i = 99;i >= 0; i--){
|
|
58
|
+ w = w + 1;
|
|
59
|
+ // each time through the loop
|
|
60
|
+ }
|
60
|
61
|
return w;
|
61
|
62
|
}
|
62
|
63
|
|
63
|
64
|
public int byTwoTo32() {
|
64
|
|
- int w = 0;
|
|
65
|
+ int w = 34;
|
65
|
66
|
|
66
|
67
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
68
|
// calling
|
68
|
|
- w = w + 1;
|
69
|
|
- // each time through the loop
|
|
69
|
+ for(int i = 0;i <= 32; i+= 2){
|
|
70
|
+ w = w - 2;
|
|
71
|
+ // each time through the loop
|
|
72
|
+ }
|
70
|
73
|
return w;
|
|
74
|
+ // each time through the loop
|
71
|
75
|
}
|
72
|
76
|
|
73
|
77
|
public int countDownFrom5000() {
|
|
@@ -75,9 +79,10 @@ public class WriteLoops {
|
75
|
79
|
|
76
|
80
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
81
|
// calling
|
78
|
|
- w = w + 1;
|
79
|
|
- // each time through the loop
|
80
|
|
-
|
|
82
|
+ for(int i = 1; i < 5001; i+=11){
|
|
83
|
+ w = w + 1;
|
|
84
|
+ // each time through the loop
|
|
85
|
+ }
|
81
|
86
|
return w;
|
82
|
87
|
}
|
83
|
88
|
|
|
@@ -86,9 +91,14 @@ public class WriteLoops {
|
86
|
91
|
|
87
|
92
|
// Write a nested FOR loop(s), where one counts from
|
88
|
93
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
|
- // calling
|
|
94
|
+ // calling
|
|
95
|
+ for(int i = 0; i < 20; i++){
|
|
96
|
+ for(int j = 0; j < 5; j++){
|
90
|
97
|
w = w + 1;
|
91
|
|
- // each time through the inner loop
|
|
98
|
+ // each time through the loop
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+ // each time through the inner loop
|
92
|
102
|
|
93
|
103
|
return w;
|
94
|
104
|
}
|
|
@@ -101,10 +111,16 @@ public class WriteLoops {
|
101
|
111
|
// loop index counter and if it’s greater than 51,
|
102
|
112
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
113
|
|
104
|
|
- // calling
|
105
|
|
- w = w + 1;
|
106
|
|
- // each time through the inner loop
|
107
|
|
-
|
|
114
|
+ // calling
|
|
115
|
+ for(int i = 5; i <= 105; i++){
|
|
116
|
+ if(i > 51){
|
|
117
|
+ System.out.println("Hello Zipcode");
|
|
118
|
+ }else{
|
|
119
|
+ w = w + 1;}
|
|
120
|
+ // each time through the loop
|
|
121
|
+ }
|
|
122
|
+ // each time through the inner loop
|
|
123
|
+
|
108
|
124
|
return w;
|
109
|
125
|
}
|
110
|
126
|
|
|
@@ -131,15 +147,18 @@ 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
|
|
-
|
|
150
|
+ while(!gpsCurrentLocation().equals("Home")){
|
|
151
|
+ driveSomeMore();
|
|
152
|
+ w = w + 1;
|
|
153
|
+ }
|
|
154
|
+ System.out.println("Honey I'm Home!");
|
135
|
155
|
// you need to use a .equals for two Strings.
|
136
|
156
|
|
137
|
|
- // calling
|
138
|
|
- w = w + 1;
|
139
|
|
- // each time through the inner loop
|
140
|
|
-
|
|
157
|
+ // calling
|
141
|
158
|
|
142
|
|
- return w;
|
|
159
|
+ // each time through the inner loop
|
|
160
|
+
|
|
161
|
+ return w;
|
143
|
162
|
}
|
144
|
163
|
|
145
|
164
|
// Getting harder...
|
|
@@ -155,11 +174,13 @@ public class WriteLoops {
|
155
|
174
|
int runningScore = 0;
|
156
|
175
|
|
157
|
176
|
// do your while loop here
|
158
|
|
-
|
|
177
|
+ while(runningScore < highestScore){
|
159
|
178
|
// calling
|
160
|
179
|
w = w + 1;
|
|
180
|
+ runningScore += currentScore;
|
|
181
|
+
|
161
|
182
|
// each time through the inner loop
|
162
|
|
-
|
|
183
|
+ }
|
163
|
184
|
return w; // >= 3;
|
164
|
185
|
}
|
165
|
186
|
|
|
@@ -170,13 +191,15 @@ public class WriteLoops {
|
170
|
191
|
int highestScore = 236;
|
171
|
192
|
int currentScore = gameNextScore();
|
172
|
193
|
int runningScore = 0;
|
173
|
|
-
|
174
|
|
- // do your while loop here
|
175
|
|
-
|
176
|
|
- // calling
|
|
194
|
+ do{
|
177
|
195
|
w = w + 1;
|
178
|
|
- // each time through the inner loop
|
|
196
|
+ runningScore += currentScore;
|
|
197
|
+ }
|
|
198
|
+ while(runningScore < highestScore);
|
|
199
|
+ // calling
|
179
|
200
|
|
|
201
|
+ // each time through the inner loop
|
|
202
|
+ // each time through the inner loop
|
180
|
203
|
return w >= 3;
|
181
|
204
|
}
|
182
|
205
|
|
|
@@ -187,12 +210,16 @@ public class WriteLoops {
|
187
|
210
|
public int checkServerStatus() {
|
188
|
211
|
int w = 0;
|
189
|
212
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
|
-
|
|
213
|
+ while(serverIsRunning()){
|
|
214
|
+ // calling
|
|
215
|
+ waitFor(5);
|
191
|
216
|
|
192
|
|
- // calling
|
193
|
|
- w = w + 1;
|
194
|
|
- // each time through the inner loop
|
195
|
|
-
|
|
217
|
+ w = w + 1;
|
|
218
|
+ // each time through the inner loop
|
|
219
|
+ }
|
|
220
|
+ if(!serverIsRunning()){
|
|
221
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
222
|
+ }
|
196
|
223
|
return w;
|
197
|
224
|
}
|
198
|
225
|
|
|
@@ -202,11 +229,10 @@ public class WriteLoops {
|
202
|
229
|
public int loop50by7() {
|
203
|
230
|
int w = 0;
|
204
|
231
|
|
|
232
|
+ // calling
|
|
233
|
+ w = w + 1;
|
|
234
|
+ // each time through the inner loop
|
205
|
235
|
|
206
|
|
- // calling
|
207
|
|
- w = w + 1;
|
208
|
|
- // each time through the inner loop
|
209
|
|
-
|
210
|
236
|
return w;
|
211
|
237
|
}
|
212
|
238
|
|
|
@@ -239,11 +265,10 @@ public class WriteLoops {
|
239
|
265
|
int w = 0;
|
240
|
266
|
int sumOfThrees = 0;
|
241
|
267
|
|
242
|
|
-
|
243
|
|
- // calling
|
244
|
|
- w = w + 1;
|
245
|
|
- // each time through the inner loop
|
246
|
|
-
|
|
268
|
+ // calling
|
|
269
|
+ w = w + 1;
|
|
270
|
+ // each time through the inner loop
|
|
271
|
+
|
247
|
272
|
System.out.print("The Sum is ");
|
248
|
273
|
System.out.println(sumOfThrees);
|
249
|
274
|
|
|
@@ -256,11 +281,10 @@ public class WriteLoops {
|
256
|
281
|
int w = 0;
|
257
|
282
|
int sumOfThrees = 0;
|
258
|
283
|
|
259
|
|
-
|
260
|
|
- // calling
|
261
|
|
- w = w + 1;
|
262
|
|
- // each time through the inner loop
|
263
|
|
-
|
|
284
|
+ // calling
|
|
285
|
+ w = w + 1;
|
|
286
|
+ // each time through the inner loop
|
|
287
|
+
|
264
|
288
|
System.out.print("The Sum is ");
|
265
|
289
|
System.out.println(sumOfThrees);
|
266
|
290
|
|
|
@@ -279,11 +303,11 @@ public class WriteLoops {
|
279
|
303
|
boolean onTime = true;
|
280
|
304
|
|
281
|
305
|
// ADD YOUR CODE here.
|
282
|
|
-
|
283
|
|
- // be sure to call
|
284
|
|
- w = w + 1;
|
285
|
|
- // each time inside the loop
|
286
|
|
-
|
|
306
|
+
|
|
307
|
+ // be sure to call
|
|
308
|
+ w = w + 1;
|
|
309
|
+ // each time inside the loop
|
|
310
|
+
|
287
|
311
|
return w;
|
288
|
312
|
}
|
289
|
313
|
|
|
@@ -296,11 +320,10 @@ public class WriteLoops {
|
296
|
320
|
int w = 0;
|
297
|
321
|
int numberOfVotes = voteTallies.length;
|
298
|
322
|
|
299
|
|
-
|
300
|
|
- // calling
|
301
|
|
- w = w + 1;
|
302
|
|
- // each time through the inner loop
|
303
|
|
-
|
|
323
|
+ // calling
|
|
324
|
+ w = w + 1;
|
|
325
|
+ // each time through the inner loop
|
|
326
|
+
|
304
|
327
|
return w;
|
305
|
328
|
}
|
306
|
329
|
|
|
@@ -311,11 +334,10 @@ public class WriteLoops {
|
311
|
334
|
int w = 0;
|
312
|
335
|
int numberOfVotes = voteTallies.length;
|
313
|
336
|
|
|
337
|
+ // calling
|
|
338
|
+ w = w + 1;
|
|
339
|
+ // each time through the inner loop
|
314
|
340
|
|
315
|
|
- // calling
|
316
|
|
- w = w + 1;
|
317
|
|
- // each time through the inner loop
|
318
|
|
-
|
319
|
341
|
return w;
|
320
|
342
|
}
|
321
|
343
|
|
|
@@ -378,14 +400,15 @@ public class WriteLoops {
|
378
|
400
|
// return (i >= 3);
|
379
|
401
|
// };
|
380
|
402
|
// };
|
381
|
|
- private int summer = 0;
|
382
|
|
- private boolean isSummer() {
|
383
|
|
- if (summer == 3) {
|
384
|
|
- return true;
|
385
|
|
- }
|
386
|
|
- summer++;
|
387
|
|
- return false;
|
|
403
|
+ private int summer = 0;
|
|
404
|
+ private boolean isSummer() {
|
|
405
|
+ if (summer == 3) {
|
|
406
|
+ return true;
|
388
|
407
|
}
|
|
408
|
+ summer++;
|
|
409
|
+ return false;
|
|
410
|
+ }
|
|
411
|
+
|
389
|
412
|
private void sendEmergencyText(String mesg, String phone) {
|
390
|
413
|
}
|
391
|
414
|
|