|
@@ -1,10 +1,14 @@
|
1
|
1
|
import com.sun.org.apache.xpath.internal.SourceTree;
|
2
|
2
|
|
|
3
|
+import java.awt.SystemTray;
|
|
4
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
5
|
+import java.util.function.Supplier;
|
|
6
|
+
|
3
|
7
|
/**
|
4
|
8
|
* Writeloops get you thinking about how to do different things with loops.
|
5
|
9
|
*
|
6
|
10
|
* @author kyounger
|
7
|
|
- * @version 1.1
|
|
11
|
+ * @version 1.2
|
8
|
12
|
*/
|
9
|
13
|
public class WriteLoops {
|
10
|
14
|
|
|
@@ -14,11 +18,9 @@ public class WriteLoops {
|
14
|
18
|
int w = 0;
|
15
|
19
|
|
16
|
20
|
// Write a FOR loop that counts from 1 to 10.
|
17
|
|
- for (int i = 1; i <= 5; i++) {
|
18
|
21
|
// calling
|
19
|
22
|
w = w + 1;
|
20
|
23
|
// each time through the loop
|
21
|
|
- }
|
22
|
24
|
|
23
|
25
|
// this will tell the test how many times the loop executed.
|
24
|
26
|
return w;
|
|
@@ -28,11 +30,10 @@ public class WriteLoops {
|
28
|
30
|
int w = 0;
|
29
|
31
|
|
30
|
32
|
// Write a FOR loop that counts from 1 to 10.
|
31
|
|
-
|
32
|
33
|
// calling
|
33
|
34
|
w = w + 1;
|
34
|
35
|
// each time through the loop
|
35
|
|
-
|
|
36
|
+
|
36
|
37
|
return w;
|
37
|
38
|
}
|
38
|
39
|
|
|
@@ -40,11 +41,10 @@ public class WriteLoops {
|
40
|
41
|
int w = 0;
|
41
|
42
|
|
42
|
43
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
43
|
|
-
|
44
|
44
|
// calling
|
45
|
45
|
w = w + 1;
|
46
|
46
|
// each time through the loop
|
47
|
|
-
|
|
47
|
+
|
48
|
48
|
return w;
|
49
|
49
|
}
|
50
|
50
|
|
|
@@ -52,11 +52,10 @@ public class WriteLoops {
|
52
|
52
|
int w = 0;
|
53
|
53
|
|
54
|
54
|
// Write a FOR loop that counts down from 100 to 0.
|
55
|
|
-
|
56
|
55
|
// calling
|
57
|
56
|
w = w + 1;
|
58
|
57
|
// each time through the loop
|
59
|
|
-
|
|
58
|
+
|
60
|
59
|
return w;
|
61
|
60
|
}
|
62
|
61
|
|
|
@@ -64,11 +63,9 @@ public class WriteLoops {
|
64
|
63
|
int w = 0;
|
65
|
64
|
|
66
|
65
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
|
-
|
68
|
66
|
// calling
|
69
|
67
|
w = w + 1;
|
70
|
68
|
// each time through the loop
|
71
|
|
-
|
72
|
69
|
return w;
|
73
|
70
|
}
|
74
|
71
|
|
|
@@ -76,11 +73,10 @@ public class WriteLoops {
|
76
|
73
|
int w = 0;
|
77
|
74
|
|
78
|
75
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
79
|
|
-
|
80
|
76
|
// calling
|
81
|
77
|
w = w + 1;
|
82
|
78
|
// each time through the loop
|
83
|
|
-
|
|
79
|
+
|
84
|
80
|
return w;
|
85
|
81
|
}
|
86
|
82
|
|
|
@@ -89,10 +85,9 @@ public class WriteLoops {
|
89
|
85
|
|
90
|
86
|
// Write a nested FOR loop(s), where one counts from
|
91
|
87
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
92
|
|
-
|
93
|
|
- // calling
|
94
|
|
- w = w + 1;
|
95
|
|
- // each time through the inner loop
|
|
88
|
+ // calling
|
|
89
|
+ w = w + 1;
|
|
90
|
+ // each time through the inner loop
|
96
|
91
|
|
97
|
92
|
return w;
|
98
|
93
|
}
|
|
@@ -105,10 +100,10 @@ public class WriteLoops {
|
105
|
100
|
// loop index counter and if it’s greater than 51,
|
106
|
101
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
107
|
102
|
|
108
|
|
- // calling
|
109
|
|
- w = w + 1;
|
110
|
|
- // each time through the inner loop
|
111
|
|
-
|
|
103
|
+ // calling
|
|
104
|
+ w = w + 1;
|
|
105
|
+ // each time through the inner loop
|
|
106
|
+
|
112
|
107
|
return w;
|
113
|
108
|
}
|
114
|
109
|
|
|
@@ -138,35 +133,33 @@ public class WriteLoops {
|
138
|
133
|
|
139
|
134
|
// you need to use a .equals for two Strings.
|
140
|
135
|
|
141
|
|
- // calling
|
142
|
|
- w = w + 1;
|
143
|
|
- // each time through the inner loop
|
|
136
|
+ // calling
|
|
137
|
+ w = w + 1;
|
|
138
|
+ // each time through the inner loop
|
|
139
|
+
|
144
|
140
|
|
145
|
|
- System.out.println("Honey, I’m Home!");
|
146
|
|
- return w;
|
|
141
|
+ return w;
|
147
|
142
|
}
|
148
|
143
|
|
149
|
144
|
// Getting harder...
|
150
|
145
|
// First declare and set “highestScore” to 236. Then set “currentScore” to
|
151
|
|
- // “gameNextScore()”. Then write a WHILE loop that checks “currentScore”
|
|
146
|
+ // “gameNextScore()”. Then write a WHILE loop that checks "runningScore"
|
152
|
147
|
// is less than “highestScore” and if it is, adds “currentScore” to
|
153
|
148
|
// "runningScore"
|
154
|
149
|
// and then sets “currentScore” to “gameNextScore()”
|
155
|
|
- public boolean checkGameScore() {
|
|
150
|
+ public int checkGameScore() {
|
156
|
151
|
int w = 0;
|
157
|
152
|
int highestScore = 236;
|
158
|
153
|
int currentScore = gameNextScore();
|
159
|
154
|
int runningScore = 0;
|
160
|
155
|
|
161
|
156
|
// do your while loop here
|
162
|
|
-
|
163
|
|
- // do your ifs here
|
164
|
|
-
|
165
|
|
- // calling
|
166
|
|
- w = w + 1;
|
167
|
|
- // each time through the inner loop
|
168
|
|
-
|
169
|
|
- return w > 3;
|
|
157
|
+
|
|
158
|
+ // calling
|
|
159
|
+ w = w + 1;
|
|
160
|
+ // each time through the inner loop
|
|
161
|
+
|
|
162
|
+ return w; // >= 3;
|
170
|
163
|
}
|
171
|
164
|
|
172
|
165
|
// Rewrite the previous WHILE loop as a DO..WHILE loop.
|
|
@@ -179,13 +172,11 @@ public class WriteLoops {
|
179
|
172
|
|
180
|
173
|
// do your while loop here
|
181
|
174
|
|
182
|
|
- // do your ifs here
|
183
|
|
-
|
184
|
|
- // calling
|
185
|
|
- w = w + 1;
|
186
|
|
- // each time through the inner loop
|
|
175
|
+ // calling
|
|
176
|
+ w = w + 1;
|
|
177
|
+ // each time through the inner loop
|
187
|
178
|
|
188
|
|
- return w > 3;
|
|
179
|
+ return w >= 3;
|
189
|
180
|
}
|
190
|
181
|
|
191
|
182
|
// Write a WHILE loop that checks “serverIsRunning()” and if true
|
|
@@ -194,11 +185,13 @@ public class WriteLoops {
|
194
|
185
|
// and also calls “tryServerRestart()”
|
195
|
186
|
public int checkServerStatus() {
|
196
|
187
|
int w = 0;
|
197
|
|
- String adminPhoneNumber = "+1 202 456 1111"
|
|
188
|
+ String adminPhoneNumber = "+1 202 456 1111";
|
|
189
|
+
|
|
190
|
+
|
198
|
191
|
// calling
|
199
|
192
|
w = w + 1;
|
200
|
193
|
// each time through the inner loop
|
201
|
|
-
|
|
194
|
+
|
202
|
195
|
return w;
|
203
|
196
|
}
|
204
|
197
|
|
|
@@ -208,19 +201,21 @@ public class WriteLoops {
|
208
|
201
|
public int loop50by7() {
|
209
|
202
|
int w = 0;
|
210
|
203
|
|
211
|
|
- // calling
|
212
|
|
- w = w + 1;
|
213
|
|
- // each time through the inner loop
|
214
|
204
|
|
|
205
|
+ // calling
|
|
206
|
+ w = w + 1;
|
|
207
|
+ // each time through the inner loop
|
|
208
|
+
|
215
|
209
|
return w;
|
216
|
210
|
}
|
217
|
211
|
|
|
212
|
+ int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
|
|
213
|
+
|
218
|
214
|
// Foo is method that add the first 7 factors of three together and prints
|
219
|
215
|
// out the sum of them all.
|
220
|
216
|
public int foo() {
|
221
|
217
|
int w = 0;
|
222
|
218
|
// this is an array of ints. it is of length 7 (from 0 -> 6)
|
223
|
|
- int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
|
224
|
219
|
int sumOfThrees = 0;
|
225
|
220
|
|
226
|
221
|
// this is a so called Enhanced for loop
|
|
@@ -243,10 +238,11 @@ public class WriteLoops {
|
243
|
238
|
int w = 0;
|
244
|
239
|
int sumOfThrees = 0;
|
245
|
240
|
|
246
|
|
- // calling
|
247
|
|
- w = w + 1;
|
248
|
|
- // each time through the inner loop
|
249
|
|
-
|
|
241
|
+
|
|
242
|
+ // calling
|
|
243
|
+ w = w + 1;
|
|
244
|
+ // each time through the inner loop
|
|
245
|
+
|
250
|
246
|
System.out.print("The Sum is ");
|
251
|
247
|
System.out.println(sumOfThrees);
|
252
|
248
|
|
|
@@ -259,10 +255,11 @@ public class WriteLoops {
|
259
|
255
|
int w = 0;
|
260
|
256
|
int sumOfThrees = 0;
|
261
|
257
|
|
262
|
|
- // calling
|
263
|
|
- w = w + 1;
|
264
|
|
- // each time through the inner loop
|
265
|
|
-
|
|
258
|
+
|
|
259
|
+ // calling
|
|
260
|
+ w = w + 1;
|
|
261
|
+ // each time through the inner loop
|
|
262
|
+
|
266
|
263
|
System.out.print("The Sum is ");
|
267
|
264
|
System.out.println(sumOfThrees);
|
268
|
265
|
|
|
@@ -274,34 +271,35 @@ public class WriteLoops {
|
274
|
271
|
// inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
|
275
|
272
|
// “yellAtJuniorToMowLawn()”
|
276
|
273
|
// After loop, call
|
277
|
|
- // “sendJuniorBackToSchool(onTime)”
|
|
274
|
+ // “sendJuniorBackToSchool()” with an argument that decribes the day junior goes
|
|
275
|
+ // back.
|
278
|
276
|
public int manageYardAndJunior() {
|
279
|
277
|
int w = 0;
|
280
|
278
|
boolean onTime = true;
|
281
|
279
|
|
282
|
280
|
// ADD YOUR CODE here.
|
283
|
|
-
|
284
|
|
- // be sure to call
|
285
|
|
- w = w + 1;
|
286
|
|
- // each time inside the loop
|
287
|
|
-
|
|
281
|
+
|
|
282
|
+ // be sure to call
|
|
283
|
+ w = w + 1;
|
|
284
|
+ // each time inside the loop
|
|
285
|
+
|
288
|
286
|
return w;
|
289
|
287
|
}
|
290
|
288
|
|
291
|
|
- String voteTallies[] = { "Lincoln", "Washington", "Adams",
|
292
|
|
- "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams",
|
293
|
|
- "Lincoln", "Washington", "Adams", "Roosevelt"
|
294
|
|
- };
|
|
289
|
+ String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams", "Lincoln",
|
|
290
|
+ "Washington", "Adams", "Lincoln", "Washington", "Adams", "Roosevelt" };
|
|
291
|
+
|
295
|
292
|
// Given an array voteTallies[], write a FOR loop that prints out each value in
|
296
|
293
|
// the array.
|
297
|
294
|
public int tallyVote1() {
|
298
|
295
|
int w = 0;
|
299
|
296
|
int numberOfVotes = voteTallies.length;
|
300
|
297
|
|
301
|
|
- // calling
|
302
|
|
- w = w + 1;
|
303
|
|
- // each time through the inner loop
|
304
|
|
-
|
|
298
|
+
|
|
299
|
+ // calling
|
|
300
|
+ w = w + 1;
|
|
301
|
+ // each time through the inner loop
|
|
302
|
+
|
305
|
303
|
return w;
|
306
|
304
|
}
|
307
|
305
|
|
|
@@ -312,10 +310,11 @@ public class WriteLoops {
|
312
|
310
|
int w = 0;
|
313
|
311
|
int numberOfVotes = voteTallies.length;
|
314
|
312
|
|
315
|
|
- // calling
|
316
|
|
- w = w + 1;
|
317
|
|
- // each time through the inner loop
|
318
|
313
|
|
|
314
|
+ // calling
|
|
315
|
+ w = w + 1;
|
|
316
|
+ // each time through the inner loop
|
|
317
|
+
|
319
|
318
|
return w;
|
320
|
319
|
}
|
321
|
320
|
|
|
@@ -362,19 +361,44 @@ public class WriteLoops {
|
362
|
361
|
return this.scr = this.scr + ThreadLocalRandom.current().nextInt(20, 99 + 1);
|
363
|
362
|
}
|
364
|
363
|
|
365
|
|
- private boolean isSummer = () -> {
|
366
|
|
- int i = 0;
|
367
|
|
- return () -> {
|
368
|
|
- i = i + 1;
|
369
|
|
- return (i >= _3);
|
370
|
|
- };
|
371
|
|
- };
|
|
364
|
+ private void yellAtJuniorToMowLawn() {
|
|
365
|
+ /* dammit, mow the yard */}
|
|
366
|
+
|
|
367
|
+ private void sendJuniorBackToSchool(String timeForSchool) {
|
|
368
|
+ if (!timeForSchool.equalsIgnoreCase("First Day of School")) {
|
|
369
|
+ throw new IllegalArgumentException();
|
|
370
|
+ }
|
|
371
|
+ /* dammit, mow the yard */}
|
|
372
|
+
|
|
373
|
+ // private Supplier<Boolean> isSummer = () -> {
|
|
374
|
+ // int i = 0;
|
|
375
|
+ // return Supplier<Boolean> () -> {
|
|
376
|
+ // i = i + 1;
|
|
377
|
+ // return (i >= 3);
|
|
378
|
+ // };
|
|
379
|
+ // };
|
|
380
|
+ private int summer = 0;
|
|
381
|
+ private boolean isSummer() {
|
|
382
|
+ if (summer == 3) {
|
|
383
|
+ return true;
|
|
384
|
+ }
|
|
385
|
+ summer++;
|
|
386
|
+ return false;
|
|
387
|
+ }
|
|
388
|
+ private void sendEmergencyText(String mesg, String phone) {
|
|
389
|
+ }
|
|
390
|
+
|
|
391
|
+ private void tryServerRestart(String mesg, String phone) {
|
|
392
|
+ }
|
372
|
393
|
|
373
|
|
- private void sendEmergencyText(String mesg, String phone) {}
|
374
|
|
- private void tryServerRestart(String mesg, String phone) {}
|
375
|
394
|
int serverStatus = 5;
|
376
|
|
- private boolean serverIsRunning() {return (serverStatus > 20);}
|
377
|
|
- private void waitFor(int interval) {serverStatus += interval;}
|
378
|
395
|
|
|
396
|
+ private boolean serverIsRunning() {
|
|
397
|
+ return (serverStatus < 20);
|
|
398
|
+ }
|
|
399
|
+
|
|
400
|
+ private void waitFor(int interval) {
|
|
401
|
+ serverStatus += interval;
|
|
402
|
+ }
|
379
|
403
|
|
380
|
404
|
}
|