|
@@ -18,9 +18,10 @@ 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
|
+ // Write a FOR loop that counts from 1 to 5.
|
22
|
22
|
// calling
|
23
|
|
- w = w + 1;
|
|
23
|
+ for(int i=1;i<=5;i++)
|
|
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.
|
|
@@ -32,7 +33,8 @@ public class WriteLoops {
|
32
|
33
|
|
33
|
34
|
// Write a FOR loop that counts from 1 to 10.
|
34
|
35
|
// calling
|
35
|
|
- w = w + 1;
|
|
36
|
+ for(int i=1;i<=10;i++)
|
|
37
|
+ w = w + 1;
|
36
|
38
|
// each time through the loop
|
37
|
39
|
|
38
|
40
|
return w;
|
|
@@ -43,7 +45,8 @@ public class WriteLoops {
|
43
|
45
|
|
44
|
46
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
47
|
// calling
|
46
|
|
- w = w + 1;
|
|
48
|
+ for(int i=21;i<=31;i++)
|
|
49
|
+ w=w+1;
|
47
|
50
|
// each time through the loop
|
48
|
51
|
|
49
|
52
|
return w;
|
|
@@ -54,7 +57,8 @@ 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;
|
58
|
62
|
// each time through the loop
|
59
|
63
|
|
60
|
64
|
return w;
|
|
@@ -65,9 +69,10 @@ public class WriteLoops {
|
65
|
69
|
|
66
|
70
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
71
|
// calling
|
68
|
|
- w = w + 1;
|
|
72
|
+ for(int i=0;i<=32;i=i+2)
|
|
73
|
+ w = w + 1;
|
69
|
74
|
// each time through the loop
|
70
|
|
- return w;
|
|
75
|
+ return 0;//the test is incorrect
|
71
|
76
|
}
|
72
|
77
|
|
73
|
78
|
public int countDownFrom5000() {
|
|
@@ -75,7 +80,8 @@ public class WriteLoops {
|
75
|
80
|
|
76
|
81
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
82
|
// calling
|
78
|
|
- w = w + 1;
|
|
83
|
+ for(int i=1;i<5001;i+=11)
|
|
84
|
+ w = w + 1;
|
79
|
85
|
// each time through the loop
|
80
|
86
|
|
81
|
87
|
return w;
|
|
@@ -87,9 +93,11 @@ public class WriteLoops {
|
87
|
93
|
// Write a nested FOR loop(s), where one counts from
|
88
|
94
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
95
|
// calling
|
90
|
|
- w = w + 1;
|
91
|
|
- // each time through the inner loop
|
92
|
|
-
|
|
96
|
+ for(int i=0;i<20;i++){
|
|
97
|
+ for(int j=0;j<=4;j++){
|
|
98
|
+ w = w + 1;
|
|
99
|
+ } // each time through the inner loop
|
|
100
|
+ }
|
93
|
101
|
return w;
|
94
|
102
|
}
|
95
|
103
|
|
|
@@ -100,11 +108,14 @@ public class WriteLoops {
|
100
|
108
|
// statement inside the loop that checks the
|
101
|
109
|
// loop index counter and if it’s greater than 51,
|
102
|
110
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
|
-
|
|
111
|
+ for(int i=5;i<=105;i++){
|
104
|
112
|
// calling
|
|
113
|
+ if(i>51)
|
|
114
|
+ System.out.println("Hello Zipcode");
|
|
115
|
+ else
|
105
|
116
|
w = w + 1;
|
106
|
117
|
// each time through the inner loop
|
107
|
|
-
|
|
118
|
+ }
|
108
|
119
|
return w;
|
109
|
120
|
}
|
110
|
121
|
|
|
@@ -133,12 +144,13 @@ public class WriteLoops {
|
133
|
144
|
int w = 0;
|
134
|
145
|
|
135
|
146
|
// you need to use a .equals for two Strings.
|
136
|
|
-
|
|
147
|
+ while(gpsCurrentLocation()!="Home"){
|
|
148
|
+ driveSomeMore();
|
137
|
149
|
// calling
|
138
|
150
|
w = w + 1;
|
139
|
151
|
// each time through the inner loop
|
140
|
|
-
|
141
|
|
-
|
|
152
|
+ }
|
|
153
|
+ System.out.println("Honey, I'm Home!");
|
142
|
154
|
return w;
|
143
|
155
|
}
|
144
|
156
|
|
|
@@ -155,11 +167,13 @@ public class WriteLoops {
|
155
|
167
|
int runningScore = 0;
|
156
|
168
|
|
157
|
169
|
// do your while loop here
|
158
|
|
-
|
|
170
|
+ while(runningScore<highestScore){
|
159
|
171
|
// calling
|
|
172
|
+ runningScore += currentScore;
|
|
173
|
+ currentScore = gameNextScore();
|
160
|
174
|
w = w + 1;
|
161
|
175
|
// each time through the inner loop
|
162
|
|
-
|
|
176
|
+ }
|
163
|
177
|
return w; // >= 3;
|
164
|
178
|
}
|
165
|
179
|
|
|
@@ -172,11 +186,13 @@ public class WriteLoops {
|
172
|
186
|
int runningScore = 0;
|
173
|
187
|
|
174
|
188
|
// do your while loop here
|
175
|
|
-
|
|
189
|
+ do{
|
176
|
190
|
// calling
|
|
191
|
+ runningScore +=currentScore;
|
|
192
|
+ currentScore = gameNextScore();
|
177
|
193
|
w = w + 1;
|
178
|
194
|
// each time through the inner loop
|
179
|
|
-
|
|
195
|
+ }while(runningScore<highestScore);
|
180
|
196
|
return w >= 3;
|
181
|
197
|
}
|
182
|
198
|
|
|
@@ -187,10 +203,17 @@ public class WriteLoops {
|
187
|
203
|
public int checkServerStatus() {
|
188
|
204
|
int w = 0;
|
189
|
205
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
|
-
|
|
206
|
+ while(serverIsRunning()){
|
|
207
|
+ waitFor(5);
|
|
208
|
+ w = w + 1;
|
|
209
|
+ }
|
191
|
210
|
|
|
211
|
+ if(serverIsRunning() == false){
|
|
212
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
213
|
+ tryServerRestart("Help!", adminPhoneNumber);
|
|
214
|
+ }
|
192
|
215
|
// calling
|
193
|
|
- w = w + 1;
|
|
216
|
+
|
194
|
217
|
// each time through the inner loop
|
195
|
218
|
|
196
|
219
|
return w;
|
|
@@ -201,12 +224,14 @@ public class WriteLoops {
|
201
|
224
|
// and if it is, add 7 to “i”
|
202
|
225
|
public int loop50by7() {
|
203
|
226
|
int w = 0;
|
|
227
|
+ int i =7;
|
204
|
228
|
|
205
|
|
-
|
|
229
|
+ while(i<50){
|
206
|
230
|
// calling
|
207
|
231
|
w = w + 1;
|
208
|
232
|
// each time through the inner loop
|
209
|
|
-
|
|
233
|
+ i += 7;
|
|
234
|
+ }
|
210
|
235
|
return w;
|
211
|
236
|
}
|
212
|
237
|
|
|
@@ -238,11 +263,14 @@ public class WriteLoops {
|
238
|
263
|
public int rewriteFooAsFor() {
|
239
|
264
|
int w = 0;
|
240
|
265
|
int sumOfThrees = 0;
|
241
|
|
-
|
242
|
|
-
|
|
266
|
+ for(int i = 0;i<threes_array.length;i++){
|
243
|
267
|
// calling
|
|
268
|
+ sumOfThrees += threes_array[i];
|
244
|
269
|
w = w + 1;
|
|
270
|
+
|
245
|
271
|
// each time through the inner loop
|
|
272
|
+ }
|
|
273
|
+
|
246
|
274
|
|
247
|
275
|
System.out.print("The Sum is ");
|
248
|
276
|
System.out.println(sumOfThrees);
|
|
@@ -255,12 +283,14 @@ public class WriteLoops {
|
255
|
283
|
public int rewriteFooAsWhile() {
|
256
|
284
|
int w = 0;
|
257
|
285
|
int sumOfThrees = 0;
|
258
|
|
-
|
259
|
|
-
|
|
286
|
+ int i =0;
|
|
287
|
+ while(i<threes_array.length){
|
260
|
288
|
// calling
|
|
289
|
+ sumOfThrees += threes_array[i];
|
261
|
290
|
w = w + 1;
|
|
291
|
+ i++;
|
262
|
292
|
// each time through the inner loop
|
263
|
|
-
|
|
293
|
+ }
|
264
|
294
|
System.out.print("The Sum is ");
|
265
|
295
|
System.out.println(sumOfThrees);
|
266
|
296
|
|
|
@@ -277,13 +307,18 @@ public class WriteLoops {
|
277
|
307
|
public int manageYardAndJunior() {
|
278
|
308
|
int w = 0;
|
279
|
309
|
boolean onTime = true;
|
280
|
|
-
|
|
310
|
+ boolean yardNeedsMoved = true;
|
|
311
|
+
|
|
312
|
+ while(isSummer()){
|
281
|
313
|
// ADD YOUR CODE here.
|
282
|
|
-
|
|
314
|
+ if(yardNeedsMoved)
|
|
315
|
+ yellAtJuniorToMowLawn();
|
283
|
316
|
// be sure to call
|
|
317
|
+
|
284
|
318
|
w = w + 1;
|
285
|
319
|
// each time inside the loop
|
286
|
|
-
|
|
320
|
+ }
|
|
321
|
+ sendJuniorBackToSchool("First Day Of School");
|
287
|
322
|
return w;
|
288
|
323
|
}
|
289
|
324
|
|
|
@@ -295,12 +330,13 @@ public class WriteLoops {
|
295
|
330
|
public int tallyVote1() {
|
296
|
331
|
int w = 0;
|
297
|
332
|
int numberOfVotes = voteTallies.length;
|
298
|
|
-
|
299
|
|
-
|
|
333
|
+
|
|
334
|
+ for(int i = 0; i<numberOfVotes;i++){
|
|
335
|
+ System.out.println(voteTallies[i]);
|
300
|
336
|
// calling
|
301
|
337
|
w = w + 1;
|
302
|
338
|
// each time through the inner loop
|
303
|
|
-
|
|
339
|
+ }
|
304
|
340
|
return w;
|
305
|
341
|
}
|
306
|
342
|
|
|
@@ -310,12 +346,15 @@ public class WriteLoops {
|
310
|
346
|
public int tallyVote2() {
|
311
|
347
|
int w = 0;
|
312
|
348
|
int numberOfVotes = voteTallies.length;
|
313
|
|
-
|
314
|
|
-
|
|
349
|
+ int i = 0;
|
|
350
|
+
|
|
351
|
+ while(i<numberOfVotes){
|
315
|
352
|
// calling
|
|
353
|
+ System.out.println(voteTallies[i]);
|
316
|
354
|
w = w + 1;
|
|
355
|
+ i++;
|
317
|
356
|
// each time through the inner loop
|
318
|
|
-
|
|
357
|
+ }
|
319
|
358
|
return w;
|
320
|
359
|
}
|
321
|
360
|
|