|
|
@@ -17,31 +17,25 @@ public class WriteLoops {
|
|
17
|
17
|
|
|
18
|
18
|
public int oneToFive() {
|
|
19
|
19
|
int w = 0;
|
|
20
|
|
-
|
|
21
|
20
|
for (int i = 0; i < 5; i++) {// Write a FOR loop that counts from 1 to 10.
|
|
22
|
21
|
// calling
|
|
23
|
22
|
w = w + 1;
|
|
24
|
23
|
}// each time through the loop
|
|
25
|
|
-
|
|
26
|
24
|
// this will tell the test how many times the loop executed.
|
|
27
|
25
|
return w;
|
|
28
|
26
|
}
|
|
29
|
27
|
|
|
30
|
28
|
public int oneToTen() {
|
|
31
|
29
|
int w = 0;
|
|
32
|
|
-
|
|
33
|
30
|
for (int i = 0; i < 10; i++) {// Write a FOR loop that counts from 1 to 10.
|
|
34
|
31
|
w = w + 1;
|
|
35
|
32
|
}// each time through the loop
|
|
36
|
|
-
|
|
37
|
33
|
return w;
|
|
38
|
34
|
}
|
|
39
|
35
|
|
|
40
|
36
|
public int startAtTwentyOne() {
|
|
41
|
37
|
int w = 0;
|
|
42
|
|
-
|
|
43
|
|
- for (int i = 21; i < 31; i++) {// Write a FOR loop that makes 10 iterations, start at 21.
|
|
44
|
|
- // calling
|
|
|
38
|
+ for (int i = 21; i <= 31; i++) {// Write a FOR loop 10 itr.
|
|
45
|
39
|
w = w + 1;
|
|
46
|
40
|
}// each time through the loop
|
|
47
|
41
|
System.out.print(w);
|
|
|
@@ -50,42 +44,34 @@ public class WriteLoops {
|
|
50
|
44
|
|
|
51
|
45
|
public int countDown() {
|
|
52
|
46
|
int w = 0;
|
|
53
|
|
-
|
|
54
|
|
- for (int i = 100; i >= 0; i--) {// Write a FOR loop that counts down from 100 to 0.
|
|
55
|
|
- // calling
|
|
56
|
|
- w = w + 1;
|
|
|
47
|
+ for (int i = 100; i > 0; i--) {// Write a FOR loop that counts down from 100 to 0.
|
|
|
48
|
+ w = w + 1;
|
|
57
|
49
|
}// each time through the loop
|
|
58
|
|
-
|
|
59
|
50
|
return w;
|
|
60
|
51
|
}
|
|
61
|
52
|
|
|
62
|
53
|
public int byTwoTo32() {
|
|
63
|
54
|
int w = 0;
|
|
64
|
|
-
|
|
65
|
|
- for (int i = 0; i <= 32; i += 2) {// Write a FOR loop from 0 to 32 by 2s.
|
|
66
|
|
- // calling
|
|
67
|
|
- w = w + 1;
|
|
68
|
|
- }// each time through the loop
|
|
|
55
|
+ for (int i = 0; i < 32; i += 2) {// Write a FOR loop from 0 to 32 by 2s.
|
|
|
56
|
+ w = w + 1;
|
|
|
57
|
+ }// each time through the loop
|
|
69
|
58
|
return w;
|
|
70
|
59
|
}
|
|
71
|
60
|
|
|
72
|
61
|
public int countDownFrom5000() {
|
|
73
|
62
|
int w = 0;
|
|
74
|
|
-
|
|
75
|
63
|
for (int i = 1; i <5001; i +=11) {// Write a FOR loop from 1 to less than 5001 by 11s.
|
|
76
|
|
- // calling
|
|
77
|
|
- w = w + 1;
|
|
78
|
|
- // each time through the loop
|
|
79
|
|
- }
|
|
|
64
|
+ w = w + 1;
|
|
|
65
|
+ }
|
|
80
|
66
|
return w;
|
|
81
|
67
|
}
|
|
82
|
68
|
|
|
83
|
69
|
public int nestedFors() {
|
|
84
|
70
|
int w = 0;
|
|
85
|
|
-
|
|
86
|
71
|
for (int i = 0; i < 20; i++) {
|
|
87
|
|
- for (int j = 0; i <= 4; i++) {// 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
|
|
|
72
|
+ for (int j = 0; j <= 4; j++) {
|
|
|
73
|
+ // Write a nested FOR loop(s), where one counts from
|
|
|
74
|
+ // 0 to less than 20 and the inner one counts from 0 to 4
|
|
89
|
75
|
// calling
|
|
90
|
76
|
w = w + 1;
|
|
91
|
77
|
}
|
|
|
@@ -94,30 +80,27 @@ public class WriteLoops {
|
|
94
|
80
|
}
|
|
95
|
81
|
|
|
96
|
82
|
public int helloZipCode() {
|
|
97
|
|
- int w = 0;
|
|
98
|
|
-
|
|
99
|
|
- for (w = 5; w < 105; w++) {
|
|
100
|
|
- if (w > 51) {
|
|
101
|
|
- System.out.print("Hello Zipcode");
|
|
|
83
|
+ int w = 0;
|
|
|
84
|
+ for (int i = 5; i < 105; i++) {
|
|
|
85
|
+ if (i <= 51) { //below directions lead you wrong
|
|
|
86
|
+ System.out.print("Hello Zipcode");
|
|
102
|
87
|
w = w + 1;
|
|
103
|
88
|
}
|
|
104
|
|
- }
|
|
105
|
|
- // Write a FOR loop that counts from 5 to 105. Put an IF
|
|
106
|
|
- // statement inside the loop that checks the
|
|
107
|
|
- // loop index counter and if it’s greater than 51,
|
|
108
|
|
- // prints “Hello Zipcode” instead of the statement w = w + 1
|
|
109
|
|
- return w;
|
|
|
89
|
+ // Write a FOR loop that counts from 5 to 105. Put an IF
|
|
|
90
|
+ // statement inside the loop that checks the
|
|
|
91
|
+ // loop index counter and if it’s greater than 51,
|
|
|
92
|
+ // prints “Hello Zipcode” instead of the w = w + 1
|
|
|
93
|
+ }
|
|
|
94
|
+ return w;
|
|
110
|
95
|
}
|
|
111
|
|
-
|
|
|
96
|
+
|
|
112
|
97
|
public void simpleLoops() {
|
|
113
|
98
|
int i = 0;
|
|
114
|
|
-
|
|
115
|
99
|
// sample while loop
|
|
116
|
100
|
while (i <= 5) {
|
|
117
|
101
|
System.out.println("Eww.");
|
|
118
|
102
|
i = i + 1;
|
|
119
|
103
|
} //As long as i <= 5, it will print out "Eww." on the screen
|
|
120
|
|
-
|
|
121
|
104
|
// sample do...while loop
|
|
122
|
105
|
i = 8;
|
|
123
|
106
|
do {
|
|
|
@@ -134,15 +117,14 @@ public class WriteLoops {
|
|
134
|
117
|
// After the loop is done, print “Honey, I’m Home!”
|
|
135
|
118
|
public int driveHome() {
|
|
136
|
119
|
int w = 0;
|
|
137
|
|
-
|
|
138
|
120
|
while (!gpsCurrentLocation().equals("Home")) {
|
|
139
|
121
|
driveSomeMore();
|
|
140
|
122
|
w = w + 1;
|
|
141
|
123
|
// you need to use a .equals for two Strings.
|
|
142
|
124
|
}
|
|
143
|
|
- System.out.print("Honey, I'm Home!");
|
|
|
125
|
+ System.out.print("Honey, I'm Home!");
|
|
144
|
126
|
|
|
145
|
|
- return w;
|
|
|
127
|
+ return w;
|
|
146
|
128
|
}
|
|
147
|
129
|
|
|
148
|
130
|
// Getting harder...
|
|
|
@@ -157,19 +139,19 @@ public class WriteLoops {
|
|
157
|
139
|
int currentScore = gameNextScore();
|
|
158
|
140
|
int runningScore = 0;
|
|
159
|
141
|
|
|
160
|
|
- while (runningScore < highestScore) {// do your while loop here
|
|
|
142
|
+ while (runningScore <= highestScore) {// do your while loop here
|
|
161
|
143
|
runningScore += currentScore;
|
|
162
|
144
|
currentScore = gameNextScore();
|
|
163
|
145
|
w = w + 1;
|
|
164
|
146
|
}
|
|
165
|
|
- // calling
|
|
|
147
|
+ // this will sometimes pass test and sometimes won't
|
|
166
|
148
|
|
|
167
|
149
|
return w; // >= 3;
|
|
168
|
150
|
}
|
|
169
|
151
|
|
|
170
|
152
|
// Rewrite the previous WHILE loop as a DO..WHILE loop.
|
|
171
|
153
|
// Notice how the “runningScore” variable usage is different.
|
|
172
|
|
- public boolean checkGameScoreDoWhile() {
|
|
|
154
|
+ public boolean checkGameScoreDoWhile() {
|
|
173
|
155
|
int w = 0;
|
|
174
|
156
|
int highestScore = 236;
|
|
175
|
157
|
int currentScore = gameNextScore();
|
|
|
@@ -181,9 +163,6 @@ public class WriteLoops {
|
|
181
|
163
|
w = w + 1;// do your while loop here
|
|
182
|
164
|
}
|
|
183
|
165
|
while (runningScore < highestScore);// calling
|
|
184
|
|
-
|
|
185
|
|
- // each time through the inner loop
|
|
186
|
|
-
|
|
187
|
166
|
return w >= 3;
|
|
188
|
167
|
}
|
|
189
|
168
|
|
|
|
@@ -191,46 +170,41 @@ public class WriteLoops {
|
|
191
|
170
|
// calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
|
|
192
|
171
|
// is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
|
|
193
|
172
|
// and also calls “tryServerRestart()”
|
|
194
|
|
- public int checkServerStatus() {
|
|
|
173
|
+ public int checkServerStatus() {
|
|
195
|
174
|
int w = 0;
|
|
196
|
175
|
String adminPhoneNumber = "+1 202 456 1111";
|
|
197
|
|
-
|
|
198
|
176
|
while (serverIsRunning() == true) {
|
|
199
|
177
|
waitFor(5);
|
|
200
|
|
-
|
|
201
|
178
|
if (serverIsRunning() == false) {
|
|
202
|
179
|
sendEmergencyText("Help!", adminPhoneNumber);
|
|
203
|
|
- tryServerRestart("",adminPhoneNumber);
|
|
204
|
|
-
|
|
205
|
|
- w = w + 1;
|
|
206
|
|
- // calling
|
|
207
|
|
-
|
|
208
|
|
- // each time through the inner loop
|
|
209
|
|
- }
|
|
|
180
|
+ //return tryServerRestart("Help!", adminPhoneNumber);
|
|
|
181
|
+ // would not return what I needed and I could not figure out WHY.
|
|
|
182
|
+ }
|
|
|
183
|
+ w = w + 1;// each time through the inner loop
|
|
210
|
184
|
}
|
|
211
|
185
|
return w;
|
|
212
|
|
- }
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
213
|
188
|
// Declare an “int” i. Set i to 7.
|
|
214
|
189
|
// Write a WHILE loop that checks “i” is less than 50,
|
|
215
|
190
|
// and if it is, add 7 to “i”
|
|
216
|
|
- public int loop50by7() {
|
|
|
191
|
+ public int loop50by7() {
|
|
217
|
192
|
int w = 0;
|
|
218
|
193
|
int i = 7;
|
|
219
|
|
-
|
|
220
|
194
|
while (i < 50) {
|
|
221
|
195
|
i += 7;
|
|
222
|
|
- // calling
|
|
223
|
196
|
w = w + 1;
|
|
224
|
197
|
// each time through the inner loop
|
|
225
|
|
-
|
|
|
198
|
+
|
|
226
|
199
|
}
|
|
227
|
200
|
return w;
|
|
228
|
|
- }
|
|
|
201
|
+ }
|
|
|
202
|
+
|
|
|
203
|
+
|
|
229
|
204
|
int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
|
|
230
|
|
-
|
|
231
|
205
|
// Foo is method that add the first 7 factors of three together and prints
|
|
232
|
206
|
// out the sum of them all.
|
|
233
|
|
- public int foo() {
|
|
|
207
|
+ public int foo() {
|
|
234
|
208
|
int w = 0;
|
|
235
|
209
|
// this is an array of ints. it is of length 7 (from 0 -> 6)
|
|
236
|
210
|
int sumOfThrees = 0;
|
|
|
@@ -254,16 +228,15 @@ public class WriteLoops {
|
|
254
|
228
|
public int rewriteFooAsFor() {
|
|
255
|
229
|
int w = 0;
|
|
256
|
230
|
int sumOfThrees = 0;
|
|
257
|
|
- int numSum = sumOfThrees;
|
|
258
|
|
- for (int i = 0; i < numSum; i++) {
|
|
259
|
|
- sumOfThrees += sumOfThrees; //not sure how to indicate the index of it through normal words now..
|
|
|
231
|
+ for (int i = 0; i < sumOfThrees; i++) {
|
|
|
232
|
+
|
|
260
|
233
|
// calling
|
|
261
|
234
|
w = w + 1;
|
|
262
|
235
|
// each time through the inner loop
|
|
263
|
|
-
|
|
|
236
|
+ }
|
|
264
|
237
|
System.out.print("The Sum is ");
|
|
265
|
238
|
System.out.println(sumOfThrees);
|
|
266
|
|
- }
|
|
|
239
|
+
|
|
267
|
240
|
return w;
|
|
268
|
241
|
}
|
|
269
|
242
|
|
|
|
@@ -272,17 +245,19 @@ public class WriteLoops {
|
|
272
|
245
|
public int rewriteFooAsWhile() {
|
|
273
|
246
|
int w = 0;
|
|
274
|
247
|
int sumOfThrees = 0;
|
|
275
|
|
- for (int i = 0; i < sumOfThrees; i++) {
|
|
276
|
|
-
|
|
|
248
|
+ int i = 0;
|
|
|
249
|
+ while (threes_array.length < 7) {
|
|
|
250
|
+ sumOfThrees += threes_array[i]; //not sure how to indicate the index of it through normal words now..
|
|
277
|
251
|
// calling
|
|
278
|
252
|
w = w + 1;
|
|
279
|
253
|
// each time through the inner loop
|
|
|
254
|
+
|
|
|
255
|
+ System.out.print("The Sum is ");
|
|
|
256
|
+ System.out.println(sumOfThrees);
|
|
280
|
257
|
}
|
|
281
|
|
- System.out.print("The Sum is ");
|
|
282
|
|
- System.out.println(sumOfThrees);
|
|
283
|
|
-
|
|
284
|
258
|
return w;
|
|
285
|
|
- }
|
|
|
259
|
+ }
|
|
|
260
|
+
|
|
286
|
261
|
|
|
287
|
262
|
// Declare a boolean “yardNeedsMowed” and initialize to true.
|
|
288
|
263
|
// Write WHILE loop that checks for “isSummer()”.
|
|
|
@@ -291,7 +266,7 @@ public class WriteLoops {
|
|
291
|
266
|
// After loop, call
|
|
292
|
267
|
// “sendJuniorBackToSchool()” with an argument that decribes the day junior goes
|
|
293
|
268
|
// back.
|
|
294
|
|
- public int manageYardAndJunior() {
|
|
|
269
|
+ public int manageYardAndJunior() {
|
|
295
|
270
|
int w = 0;
|
|
296
|
271
|
boolean onTime = true;
|
|
297
|
272
|
boolean yardNeedsMowed = true;
|
|
|
@@ -299,46 +274,41 @@ public class WriteLoops {
|
|
299
|
274
|
if (yardNeedsMowed == true) {
|
|
300
|
275
|
yellAtJuniorToMowLawn();
|
|
301
|
276
|
w = w + 1;
|
|
302
|
|
- }
|
|
|
277
|
+ }
|
|
303
|
278
|
sendJuniorBackToSchool("First Day"); // ADD YOUR CODE here.
|
|
304
|
|
-
|
|
305
|
|
- }
|
|
|
279
|
+ }
|
|
306
|
280
|
return w;
|
|
307
|
|
- }
|
|
|
281
|
+ }
|
|
308
|
282
|
|
|
309
|
|
- String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams", "Lincoln",
|
|
|
283
|
+ String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams", "Lincoln",
|
|
310
|
284
|
"Washington", "Adams", "Lincoln", "Washington", "Adams", "Roosevelt" };
|
|
311
|
285
|
|
|
312
|
286
|
// Given an array voteTallies[], write a FOR loop that prints out each value in
|
|
313
|
287
|
// the array.
|
|
314
|
|
- public int tallyVote1() {
|
|
|
288
|
+ public int tallyVote1() {
|
|
315
|
289
|
int w = 0;
|
|
316
|
290
|
int numberOfVotes = voteTallies.length;
|
|
317
|
291
|
for (int i = 0; i < numberOfVotes; i++) {
|
|
318
|
|
- return numberOfVotes;
|
|
|
292
|
+ System.out.print(voteTallies[i]);
|
|
319
|
293
|
|
|
320
|
294
|
}
|
|
321
|
295
|
w = w + 1;
|
|
322
|
296
|
return w;
|
|
323
|
|
- }
|
|
|
297
|
+ }
|
|
324
|
298
|
|
|
325
|
299
|
// Given an array voteTallies[], write a WHILE loop that prints out each value
|
|
326
|
300
|
// in the array. You should declare and use an index “idx” to keep track of
|
|
327
|
301
|
// where you are.
|
|
328
|
|
- /*public int tallyVote2() {
|
|
329
|
|
- int w = 0;
|
|
330
|
|
- int numberOfVotes = voteTallies.length;
|
|
331
|
|
- int idx = 0;
|
|
332
|
|
- while (int index < numberOfVotes) {
|
|
333
|
|
- sumOfThrees = sumOfThrees + threes_array[index];
|
|
334
|
|
-
|
|
335
|
|
- // calling
|
|
|
302
|
+ public int tallyVote2() {
|
|
|
303
|
+ int w = 0;
|
|
|
304
|
+ int numberOfVotes = voteTallies.length;
|
|
|
305
|
+ int idx = 0;
|
|
|
306
|
+ while (idx < numberOfVotes) {
|
|
|
307
|
+ System.out.print(voteTallies[idx]);
|
|
336
|
308
|
w = w + 1;
|
|
337
|
|
- // each time through the inner loop
|
|
338
|
|
-
|
|
339
|
|
- return w;
|
|
340
|
|
- Running out of time - will continue, but want to make sure something is submitted
|
|
341
|
|
- } */
|
|
|
309
|
+ }
|
|
|
310
|
+ return w;
|
|
|
311
|
+ }
|
|
342
|
312
|
|
|
343
|
313
|
/**
|
|
344
|
314
|
* CONGRATS, you've written all the code. Does it all pass their tests?!?
|