|
|
@@ -18,12 +18,15 @@ 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
|
|
23
|
|
- w = w + 1;
|
|
24
|
|
- // each time through the loop
|
|
|
21
|
+ // Write a FOR loop that counts from 1 to 5.
|
|
|
22
|
+ // calling
|
|
|
23
|
+ //w = w + 1;
|
|
|
24
|
+ // each time through the loop
|
|
25
|
25
|
|
|
26
|
26
|
// this will tell the test how many times the loop executed.
|
|
|
27
|
+ for(int i=0; i<5; i++){
|
|
|
28
|
+ w= w + 1;
|
|
|
29
|
+ }
|
|
27
|
30
|
return w;
|
|
28
|
31
|
}
|
|
29
|
32
|
|
|
|
@@ -32,9 +35,12 @@ public class WriteLoops {
|
|
32
|
35
|
|
|
33
|
36
|
// Write a FOR loop that counts from 1 to 10.
|
|
34
|
37
|
// calling
|
|
35
|
|
- w = w + 1;
|
|
|
38
|
+ //w = w + 1;
|
|
36
|
39
|
// each time through the loop
|
|
37
|
|
-
|
|
|
40
|
+ for(int i=0; i<10; i++){
|
|
|
41
|
+ w= w + 1;
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
38
|
44
|
return w;
|
|
39
|
45
|
}
|
|
40
|
46
|
|
|
|
@@ -45,7 +51,9 @@ public class WriteLoops {
|
|
45
|
51
|
// calling
|
|
46
|
52
|
w = w + 1;
|
|
47
|
53
|
// each time through the loop
|
|
48
|
|
-
|
|
|
54
|
+ for(int i=21; i<31; i++){
|
|
|
55
|
+ w = w + 1;
|
|
|
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
|
|
57
|
|
- w = w + 1;
|
|
|
65
|
+ //w = w + 1;
|
|
58
|
66
|
// each time through the loop
|
|
59
|
|
-
|
|
|
67
|
+ for(int i=100; i > 0; i--){
|
|
|
68
|
+ w= w+1;
|
|
|
69
|
+ }
|
|
60
|
70
|
return w;
|
|
61
|
71
|
}
|
|
62
|
72
|
|
|
|
@@ -65,8 +75,11 @@ public class WriteLoops {
|
|
65
|
75
|
|
|
66
|
76
|
// Write a FOR loop from 0 to 32 by 2s.
|
|
67
|
77
|
// calling
|
|
68
|
|
- w = w + 1;
|
|
|
78
|
+ //w = w + 1;
|
|
69
|
79
|
// each time through the loop
|
|
|
80
|
+ for(int i = 0; i>32; i+=2){
|
|
|
81
|
+ w= w+1;
|
|
|
82
|
+ }
|
|
70
|
83
|
return w;
|
|
71
|
84
|
}
|
|
72
|
85
|
|
|
|
@@ -75,9 +88,11 @@ public class WriteLoops {
|
|
75
|
88
|
|
|
76
|
89
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
|
77
|
90
|
// calling
|
|
78
|
|
- w = w + 1;
|
|
|
91
|
+ //w = w + 1;
|
|
79
|
92
|
// each time through the loop
|
|
80
|
|
-
|
|
|
93
|
+ for(int i = 1; i<5001; i+=11){
|
|
|
94
|
+ w= w+1;
|
|
|
95
|
+ }
|
|
81
|
96
|
return w;
|
|
82
|
97
|
}
|
|
83
|
98
|
|
|
|
@@ -86,9 +101,15 @@ public class WriteLoops {
|
|
86
|
101
|
|
|
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
|
|
- // calling
|
|
90
|
|
- w = w + 1;
|
|
91
|
|
- // each time through the inner loop
|
|
|
104
|
+ // calling
|
|
|
105
|
+ // w = w + 1;
|
|
|
106
|
+ // each time through the inner loop
|
|
|
107
|
+ for(int i=0; i<20; i++){
|
|
|
108
|
+ for(int z=0; z<4; z++){
|
|
|
109
|
+ w= w+1;
|
|
|
110
|
+ }
|
|
|
111
|
+ w= w+1;
|
|
|
112
|
+ }
|
|
92
|
113
|
|
|
93
|
114
|
return w;
|
|
94
|
115
|
}
|
|
|
@@ -101,12 +122,18 @@ public class WriteLoops {
|
|
101
|
122
|
// loop index counter and if it’s greater than 51,
|
|
102
|
123
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
|
103
|
124
|
|
|
104
|
|
- // calling
|
|
105
|
|
- w = w + 1;
|
|
106
|
|
- // each time through the inner loop
|
|
107
|
|
-
|
|
108
|
|
- return w;
|
|
|
125
|
+ // calling
|
|
|
126
|
+ // w = w + 1;
|
|
|
127
|
+ // each time through the inner loop
|
|
|
128
|
+ for(int i=5; i<105; i++){
|
|
|
129
|
+ if(i>51){
|
|
|
130
|
+ System.out.print("Hello Zipcode");
|
|
|
131
|
+ }else{
|
|
|
132
|
+ w= w+1;
|
|
|
133
|
+ }
|
|
109
|
134
|
}
|
|
|
135
|
+ return w;
|
|
|
136
|
+}
|
|
110
|
137
|
|
|
111
|
138
|
public void simpleLoops() {
|
|
112
|
139
|
int i = 0;
|
|
|
@@ -134,12 +161,17 @@ public class WriteLoops {
|
|
134
|
161
|
|
|
135
|
162
|
// you need to use a .equals for two Strings.
|
|
136
|
163
|
|
|
137
|
|
- // calling
|
|
138
|
|
- w = w + 1;
|
|
139
|
|
- // each time through the inner loop
|
|
|
164
|
+ // calling
|
|
|
165
|
+ //w = w + 1;
|
|
|
166
|
+ // each time through the inner loop
|
|
140
|
167
|
|
|
|
168
|
+ while (gpsCurrentLocation()!= "Home"){
|
|
|
169
|
+ driveSomeMore();
|
|
|
170
|
+ w=w+1;
|
|
|
171
|
+ }
|
|
|
172
|
+ System.out.println("Honey, I'm Home");
|
|
141
|
173
|
|
|
142
|
|
- return w;
|
|
|
174
|
+ return w;
|
|
143
|
175
|
}
|
|
144
|
176
|
|
|
145
|
177
|
// Getting harder...
|
|
|
@@ -155,11 +187,15 @@ public class WriteLoops {
|
|
155
|
187
|
int runningScore = 0;
|
|
156
|
188
|
|
|
157
|
189
|
// do your while loop here
|
|
158
|
|
-
|
|
159
|
|
- // calling
|
|
160
|
|
- w = w + 1;
|
|
161
|
|
- // each time through the inner loop
|
|
|
190
|
+
|
|
|
191
|
+ // calling
|
|
|
192
|
+ //w = w + 1;
|
|
|
193
|
+ // each time through the inner loop
|
|
|
194
|
+ while(runningScore<highestScore){
|
|
|
195
|
+ runningScore=currentScore+runningScore;
|
|
|
196
|
+ w=w+1;
|
|
162
|
197
|
|
|
|
198
|
+ }
|
|
163
|
199
|
return w; // >= 3;
|
|
164
|
200
|
}
|
|
165
|
201
|
|
|
|
@@ -173,10 +209,15 @@ public class WriteLoops {
|
|
173
|
209
|
|
|
174
|
210
|
// do your while loop here
|
|
175
|
211
|
|
|
176
|
|
- // calling
|
|
177
|
|
- w = w + 1;
|
|
178
|
|
- // each time through the inner loop
|
|
179
|
|
-
|
|
|
212
|
+ // calling
|
|
|
213
|
+ //w = w + 1;
|
|
|
214
|
+ // each time through the inner loop
|
|
|
215
|
+ do{
|
|
|
216
|
+ runningScore=runningScore+currentScore;
|
|
|
217
|
+ w=w+1;
|
|
|
218
|
+
|
|
|
219
|
+ }while(runningScore<highestScore);
|
|
|
220
|
+
|
|
180
|
221
|
return w >= 3;
|
|
181
|
222
|
}
|
|
182
|
223
|
|
|
|
@@ -187,12 +228,17 @@ public class WriteLoops {
|
|
187
|
228
|
public int checkServerStatus() {
|
|
188
|
229
|
int w = 0;
|
|
189
|
230
|
String adminPhoneNumber = "+1 202 456 1111";
|
|
190
|
|
-
|
|
191
|
231
|
|
|
192
|
232
|
// calling
|
|
193
|
|
- w = w + 1;
|
|
|
233
|
+ //w = w + 1;
|
|
194
|
234
|
// each time through the inner loop
|
|
195
|
|
-
|
|
|
235
|
+ while(serverIsRunning()){
|
|
|
236
|
+ waitFor(5);
|
|
|
237
|
+ w= w+1;
|
|
|
238
|
+ }
|
|
|
239
|
+ if(!serverIsRunning()){
|
|
|
240
|
+ sendEmergencyText("Help!",adminPhoneNumber);
|
|
|
241
|
+ }
|
|
196
|
242
|
return w;
|
|
197
|
243
|
}
|
|
198
|
244
|
|
|
|
@@ -201,12 +247,14 @@ public class WriteLoops {
|
|
201
|
247
|
// and if it is, add 7 to “i”
|
|
202
|
248
|
public int loop50by7() {
|
|
203
|
249
|
int w = 0;
|
|
204
|
|
-
|
|
205
|
|
-
|
|
206
|
|
- // calling
|
|
207
|
|
- w = w + 1;
|
|
208
|
|
- // each time through the inner loop
|
|
209
|
|
-
|
|
|
250
|
+ int i=7;
|
|
|
251
|
+ // calling
|
|
|
252
|
+ w = w + 1;
|
|
|
253
|
+ // each time through the inner loop
|
|
|
254
|
+ while(i<50){
|
|
|
255
|
+ i= 7+i;
|
|
|
256
|
+ w=w+1;
|
|
|
257
|
+ }
|
|
210
|
258
|
return w;
|
|
211
|
259
|
}
|
|
212
|
260
|
|
|
|
@@ -239,11 +287,14 @@ public class WriteLoops {
|
|
239
|
287
|
int w = 0;
|
|
240
|
288
|
int sumOfThrees = 0;
|
|
241
|
289
|
|
|
242
|
|
-
|
|
243
|
|
- // calling
|
|
244
|
|
- w = w + 1;
|
|
245
|
|
- // each time through the inner loop
|
|
246
|
|
-
|
|
|
290
|
+ // calling
|
|
|
291
|
+ w = w + 1;
|
|
|
292
|
+ // each time through the inner loop
|
|
|
293
|
+ //for(int i=sumOfThrees; i:)
|
|
|
294
|
+ for(int i=0; i<threes_array.length; i++){
|
|
|
295
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
|
|
296
|
+ w=w+1;
|
|
|
297
|
+ }
|
|
247
|
298
|
System.out.print("The Sum is ");
|
|
248
|
299
|
System.out.println(sumOfThrees);
|
|
249
|
300
|
|
|
|
@@ -255,12 +306,14 @@ public class WriteLoops {
|
|
255
|
306
|
public int rewriteFooAsWhile() {
|
|
256
|
307
|
int w = 0;
|
|
257
|
308
|
int sumOfThrees = 0;
|
|
258
|
|
-
|
|
259
|
|
-
|
|
260
|
|
- // calling
|
|
261
|
|
- w = w + 1;
|
|
262
|
|
- // each time through the inner loop
|
|
263
|
|
-
|
|
|
309
|
+ int i= 0;
|
|
|
310
|
+ // calling
|
|
|
311
|
+ w = w + 1;
|
|
|
312
|
+ // each time through the inner loop
|
|
|
313
|
+ while(i<threes_array.length){
|
|
|
314
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
|
|
315
|
+ w=w+1;
|
|
|
316
|
+ }
|
|
264
|
317
|
System.out.print("The Sum is ");
|
|
265
|
318
|
System.out.println(sumOfThrees);
|
|
266
|
319
|
|
|
|
@@ -277,13 +330,19 @@ public class WriteLoops {
|
|
277
|
330
|
public int manageYardAndJunior() {
|
|
278
|
331
|
int w = 0;
|
|
279
|
332
|
boolean onTime = true;
|
|
280
|
|
-
|
|
|
333
|
+ boolean yardNeedsMowed = true;
|
|
281
|
334
|
// ADD YOUR CODE here.
|
|
282
|
|
-
|
|
283
|
|
- // be sure to call
|
|
284
|
|
- w = w + 1;
|
|
285
|
|
- // each time inside the loop
|
|
286
|
|
-
|
|
|
335
|
+
|
|
|
336
|
+ // be sure to call
|
|
|
337
|
+ //w = w + 1;
|
|
|
338
|
+ // each time inside the loop
|
|
|
339
|
+ while(isSummer()){
|
|
|
340
|
+ if(yardNeedsMowed){
|
|
|
341
|
+ yellAtJuniorToMowLawn();
|
|
|
342
|
+ }
|
|
|
343
|
+ w=w+1;
|
|
|
344
|
+ }
|
|
|
345
|
+
|
|
287
|
346
|
return w;
|
|
288
|
347
|
}
|
|
289
|
348
|
|
|
|
@@ -295,12 +354,15 @@ public class WriteLoops {
|
|
295
|
354
|
public int tallyVote1() {
|
|
296
|
355
|
int w = 0;
|
|
297
|
356
|
int numberOfVotes = voteTallies.length;
|
|
298
|
|
-
|
|
299
|
|
-
|
|
300
|
|
- // calling
|
|
301
|
|
- w = w + 1;
|
|
302
|
|
- // each time through the inner loop
|
|
303
|
357
|
|
|
|
358
|
+ // calling
|
|
|
359
|
+ w = w + 1;
|
|
|
360
|
+ // each time through the inner loop
|
|
|
361
|
+ for(int i=0; i<numberOfVotes; i++){
|
|
|
362
|
+ System.out.println(voteTallies.toString());
|
|
|
363
|
+ w=w+1;
|
|
|
364
|
+
|
|
|
365
|
+ }
|
|
304
|
366
|
return w;
|
|
305
|
367
|
}
|
|
306
|
368
|
|
|
|
@@ -310,12 +372,14 @@ public class WriteLoops {
|
|
310
|
372
|
public int tallyVote2() {
|
|
311
|
373
|
int w = 0;
|
|
312
|
374
|
int numberOfVotes = voteTallies.length;
|
|
313
|
|
-
|
|
314
|
|
-
|
|
315
|
|
- // calling
|
|
316
|
|
- w = w + 1;
|
|
317
|
|
- // each time through the inner loop
|
|
318
|
|
-
|
|
|
375
|
+ int i = 0;
|
|
|
376
|
+ // calling
|
|
|
377
|
+ w = w + 1;
|
|
|
378
|
+ // each time through the inner loop
|
|
|
379
|
+ while(i<numberOfVotes){
|
|
|
380
|
+ System.out.println(voteTallies.toString());
|
|
|
381
|
+ w=w+1;
|
|
|
382
|
+ }
|
|
319
|
383
|
return w;
|
|
320
|
384
|
}
|
|
321
|
385
|
|
|
|
@@ -378,14 +442,15 @@ public class WriteLoops {
|
|
378
|
442
|
// return (i >= 3);
|
|
379
|
443
|
// };
|
|
380
|
444
|
// };
|
|
381
|
|
- private int summer = 0;
|
|
382
|
|
- private boolean isSummer() {
|
|
383
|
|
- if (summer == 3) {
|
|
384
|
|
- return true;
|
|
385
|
|
- }
|
|
386
|
|
- summer++;
|
|
387
|
|
- return false;
|
|
|
445
|
+ private int summer = 0;
|
|
|
446
|
+ private boolean isSummer() {
|
|
|
447
|
+ if (summer == 3) {
|
|
|
448
|
+ return true;
|
|
388
|
449
|
}
|
|
|
450
|
+ summer++;
|
|
|
451
|
+ return false;
|
|
|
452
|
+ }
|
|
|
453
|
+
|
|
389
|
454
|
private void sendEmergencyText(String mesg, String phone) {
|
|
390
|
455
|
}
|
|
391
|
456
|
|