|
@@ -0,0 +1,380 @@
|
|
1
|
+import com.sun.org.apache.xpath.internal.SourceTree;
|
|
2
|
+
|
|
3
|
+/**
|
|
4
|
+ * Writeloops get you thinking about how to do different things with loops.
|
|
5
|
+ *
|
|
6
|
+ * @author kyounger
|
|
7
|
+ * @version 1.1
|
|
8
|
+ */
|
|
9
|
+public class WriteLoops {
|
|
10
|
+
|
|
11
|
+ private static final int _3 = 3;
|
|
12
|
+
|
|
13
|
+ public int oneToFive() {
|
|
14
|
+ int w = 0;
|
|
15
|
+
|
|
16
|
+ // Write a FOR loop that counts from 1 to 10.
|
|
17
|
+ for (int i = 1; i <= 5; i++) {
|
|
18
|
+ // calling
|
|
19
|
+ w = w + 1;
|
|
20
|
+ // each time through the loop
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ // this will tell the test how many times the loop executed.
|
|
24
|
+ return w;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public int oneToTen() {
|
|
28
|
+ int w = 0;
|
|
29
|
+
|
|
30
|
+ // Write a FOR loop that counts from 1 to 10.
|
|
31
|
+
|
|
32
|
+ // calling
|
|
33
|
+ w = w + 1;
|
|
34
|
+ // each time through the loop
|
|
35
|
+
|
|
36
|
+ return w;
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ public int startAtTwentyOne() {
|
|
40
|
+ int w = 0;
|
|
41
|
+
|
|
42
|
+ // Write a FOR loop that makes 10 iterations, start at 21.
|
|
43
|
+
|
|
44
|
+ // calling
|
|
45
|
+ w = w + 1;
|
|
46
|
+ // each time through the loop
|
|
47
|
+
|
|
48
|
+ return w;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public int countDown() {
|
|
52
|
+ int w = 0;
|
|
53
|
+
|
|
54
|
+ // Write a FOR loop that counts down from 100 to 0.
|
|
55
|
+
|
|
56
|
+ // calling
|
|
57
|
+ w = w + 1;
|
|
58
|
+ // each time through the loop
|
|
59
|
+
|
|
60
|
+ return w;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ public int byTwoTo32() {
|
|
64
|
+ int w = 0;
|
|
65
|
+
|
|
66
|
+ // Write a FOR loop from 0 to 32 by 2s.
|
|
67
|
+
|
|
68
|
+ // calling
|
|
69
|
+ w = w + 1;
|
|
70
|
+ // each time through the loop
|
|
71
|
+
|
|
72
|
+ return w;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public int countDownFrom5000() {
|
|
76
|
+ int w = 0;
|
|
77
|
+
|
|
78
|
+ // Write a FOR loop from 1 to less than 5001 by 11s.
|
|
79
|
+
|
|
80
|
+ // calling
|
|
81
|
+ w = w + 1;
|
|
82
|
+ // each time through the loop
|
|
83
|
+
|
|
84
|
+ return w;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public int nestedFors() {
|
|
88
|
+ int w = 0;
|
|
89
|
+
|
|
90
|
+ // Write a nested FOR loop(s), where one counts from
|
|
91
|
+ // 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
|
|
96
|
+
|
|
97
|
+ return w;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ public int helloZipCode() {
|
|
101
|
+ int w = 0;
|
|
102
|
+
|
|
103
|
+ // Write a FOR loop that counts from 5 to 105. Put an IF
|
|
104
|
+ // statement inside the loop that checks the
|
|
105
|
+ // loop index counter and if it’s greater than 51,
|
|
106
|
+ // prints “Hello Zipcode” instead of the statement w = w + 1;
|
|
107
|
+
|
|
108
|
+ // calling
|
|
109
|
+ w = w + 1;
|
|
110
|
+ // each time through the inner loop
|
|
111
|
+
|
|
112
|
+ return w;
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ public void simpleLoops() {
|
|
116
|
+ int i = 0;
|
|
117
|
+
|
|
118
|
+ // sample while loop
|
|
119
|
+ while (i <= 5) {
|
|
120
|
+ System.out.println("Eww.");
|
|
121
|
+ i = i + 1;
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ // sample do...while loop
|
|
125
|
+ i = 8;
|
|
126
|
+ do {
|
|
127
|
+ System.out.println("Eww.");
|
|
128
|
+ i = i - 1;
|
|
129
|
+ } while (i > 0);
|
|
130
|
+ // what's the primary difference between them?!?
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ // Write a WHILE loop that checks “gpsCurrentLocation()”
|
|
134
|
+ // and if that is not equal to “Home” then and it calls “driveSomeMore()”.
|
|
135
|
+ // After the loop is done, print “Honey, I’m Home!”
|
|
136
|
+ public int driveHome() {
|
|
137
|
+ int w = 0;
|
|
138
|
+
|
|
139
|
+ // you need to use a .equals for two Strings.
|
|
140
|
+
|
|
141
|
+ // calling
|
|
142
|
+ w = w + 1;
|
|
143
|
+ // each time through the inner loop
|
|
144
|
+
|
|
145
|
+ System.out.println("Honey, I’m Home!");
|
|
146
|
+ return w;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ // Getting harder...
|
|
150
|
+ // First declare and set “highestScore” to 236. Then set “currentScore” to
|
|
151
|
+ // “gameNextScore()”. Then write a WHILE loop that checks “currentScore”
|
|
152
|
+ // is less than “highestScore” and if it is, adds “currentScore” to
|
|
153
|
+ // "runningScore"
|
|
154
|
+ // and then sets “currentScore” to “gameNextScore()”
|
|
155
|
+ public boolean checkGameScore() {
|
|
156
|
+ int w = 0;
|
|
157
|
+ int highestScore = 236;
|
|
158
|
+ int currentScore = gameNextScore();
|
|
159
|
+ int runningScore = 0;
|
|
160
|
+
|
|
161
|
+ // 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;
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ // Rewrite the previous WHILE loop as a DO..WHILE loop.
|
|
173
|
+ // Notice how the “runningScore” variable usage is different.
|
|
174
|
+ public boolean checkGameScoreDoWhile() {
|
|
175
|
+ int w = 0;
|
|
176
|
+ int highestScore = 236;
|
|
177
|
+ int currentScore = gameNextScore();
|
|
178
|
+ int runningScore = 0;
|
|
179
|
+
|
|
180
|
+ // do your while loop here
|
|
181
|
+
|
|
182
|
+ // do your ifs here
|
|
183
|
+
|
|
184
|
+ // calling
|
|
185
|
+ w = w + 1;
|
|
186
|
+ // each time through the inner loop
|
|
187
|
+
|
|
188
|
+ return w > 3;
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ // Write a WHILE loop that checks “serverIsRunning()” and if true
|
|
192
|
+ // calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
|
|
193
|
+ // is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
|
|
194
|
+ // and also calls “tryServerRestart()”
|
|
195
|
+ public int checkServerStatus() {
|
|
196
|
+ int w = 0;
|
|
197
|
+ String adminPhoneNumber = "+1 202 456 1111"
|
|
198
|
+ // calling
|
|
199
|
+ w = w + 1;
|
|
200
|
+ // each time through the inner loop
|
|
201
|
+
|
|
202
|
+ return w;
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ // Declare an “int” i. Set i to 7.
|
|
206
|
+ // Write a WHILE loop that checks “i” is less than 50,
|
|
207
|
+ // and if it is, add 7 to “i”
|
|
208
|
+ public int loop50by7() {
|
|
209
|
+ int w = 0;
|
|
210
|
+
|
|
211
|
+ // calling
|
|
212
|
+ w = w + 1;
|
|
213
|
+ // each time through the inner loop
|
|
214
|
+
|
|
215
|
+ return w;
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+ // Foo is method that add the first 7 factors of three together and prints
|
|
219
|
+ // out the sum of them all.
|
|
220
|
+ public int foo() {
|
|
221
|
+ int w = 0;
|
|
222
|
+ // 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
|
+ int sumOfThrees = 0;
|
|
225
|
+
|
|
226
|
+ // this is a so called Enhanced for loop
|
|
227
|
+ for (int index : threes_array) {
|
|
228
|
+ sumOfThrees = sumOfThrees + threes_array[index];
|
|
229
|
+ // calling
|
|
230
|
+ w = w + 1;
|
|
231
|
+ // each time through the inner loop
|
|
232
|
+ }
|
|
233
|
+ System.out.print("The Sum is ");
|
|
234
|
+ System.out.println(sumOfThrees);
|
|
235
|
+
|
|
236
|
+ return w;
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ // Ponder this: can all FOR loops be rewritten as WHILE loops?
|
|
240
|
+ // rewrite the loop inside of "foo()" as a standard for loop
|
|
241
|
+ // with 'i' as its index variable.
|
|
242
|
+ public int rewriteFooAsFor() {
|
|
243
|
+ int w = 0;
|
|
244
|
+ int sumOfThrees = 0;
|
|
245
|
+
|
|
246
|
+ // calling
|
|
247
|
+ w = w + 1;
|
|
248
|
+ // each time through the inner loop
|
|
249
|
+
|
|
250
|
+ System.out.print("The Sum is ");
|
|
251
|
+ System.out.println(sumOfThrees);
|
|
252
|
+
|
|
253
|
+ return w;
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ // Ponder this: can all WHILE loops be rewritten as FOR loops?
|
|
257
|
+ // rewrite the loop inside of "foo()" as a 'while' loop
|
|
258
|
+ public int rewriteFooAsWhile() {
|
|
259
|
+ int w = 0;
|
|
260
|
+ int sumOfThrees = 0;
|
|
261
|
+
|
|
262
|
+ // calling
|
|
263
|
+ w = w + 1;
|
|
264
|
+ // each time through the inner loop
|
|
265
|
+
|
|
266
|
+ System.out.print("The Sum is ");
|
|
267
|
+ System.out.println(sumOfThrees);
|
|
268
|
+
|
|
269
|
+ return w;
|
|
270
|
+ }
|
|
271
|
+
|
|
272
|
+ // Declare a boolean “yardNeedsMowed” and initialize to true.
|
|
273
|
+ // Write WHILE loop that checks for “isSummer()”.
|
|
274
|
+ // inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
|
|
275
|
+ // “yellAtJuniorToMowLawn()”
|
|
276
|
+ // After loop, call
|
|
277
|
+ // “sendJuniorBackToSchool(onTime)”
|
|
278
|
+ public int manageYardAndJunior() {
|
|
279
|
+ int w = 0;
|
|
280
|
+ boolean onTime = true;
|
|
281
|
+
|
|
282
|
+ // ADD YOUR CODE here.
|
|
283
|
+
|
|
284
|
+ // be sure to call
|
|
285
|
+ w = w + 1;
|
|
286
|
+ // each time inside the loop
|
|
287
|
+
|
|
288
|
+ return w;
|
|
289
|
+ }
|
|
290
|
+
|
|
291
|
+ String voteTallies[] = { "Lincoln", "Washington", "Adams",
|
|
292
|
+ "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams",
|
|
293
|
+ "Lincoln", "Washington", "Adams", "Roosevelt"
|
|
294
|
+ };
|
|
295
|
+ // Given an array voteTallies[], write a FOR loop that prints out each value in
|
|
296
|
+ // the array.
|
|
297
|
+ public int tallyVote1() {
|
|
298
|
+ int w = 0;
|
|
299
|
+ int numberOfVotes = voteTallies.length;
|
|
300
|
+
|
|
301
|
+ // calling
|
|
302
|
+ w = w + 1;
|
|
303
|
+ // each time through the inner loop
|
|
304
|
+
|
|
305
|
+ return w;
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ // Given an array voteTallies[], write a WHILE loop that prints out each value
|
|
309
|
+ // in the array. You should declare and use an index “idx” to keep track of
|
|
310
|
+ // where you are.
|
|
311
|
+ public int tallyVote2() {
|
|
312
|
+ int w = 0;
|
|
313
|
+ int numberOfVotes = voteTallies.length;
|
|
314
|
+
|
|
315
|
+ // calling
|
|
316
|
+ w = w + 1;
|
|
317
|
+ // each time through the inner loop
|
|
318
|
+
|
|
319
|
+ return w;
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+ /**
|
|
323
|
+ * CONGRATS, you've written all the code. Does it all pass their tests?!?
|
|
324
|
+ *
|
|
325
|
+ *
|
|
326
|
+ * If not, why not? :-)
|
|
327
|
+ *
|
|
328
|
+ *
|
|
329
|
+ */
|
|
330
|
+
|
|
331
|
+ /**
|
|
332
|
+ * IGNORE the CODER behind the CURTAIN. These are the support routines to make
|
|
333
|
+ * all the examples interesting.
|
|
334
|
+ */
|
|
335
|
+ // instance variables - replace the example below with your own
|
|
336
|
+ private int x;
|
|
337
|
+
|
|
338
|
+ /**
|
|
339
|
+ * Constructor for objects of class WriteLoops
|
|
340
|
+ */
|
|
341
|
+ public WriteLoops() {
|
|
342
|
+ // initialise instance variables
|
|
343
|
+ x = 0;
|
|
344
|
+ }
|
|
345
|
+
|
|
346
|
+ private int gps = 0;
|
|
347
|
+
|
|
348
|
+ private String gpsCurrentLocation() {
|
|
349
|
+ if (this.gps > 5) {
|
|
350
|
+ return "Home";
|
|
351
|
+ }
|
|
352
|
+ return "Not Home";
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+ private void driveSomeMore() {
|
|
356
|
+ this.gps += 1;
|
|
357
|
+ }
|
|
358
|
+
|
|
359
|
+ private int scr = 31;
|
|
360
|
+
|
|
361
|
+ private int gameNextScore() {
|
|
362
|
+ return this.scr = this.scr + ThreadLocalRandom.current().nextInt(20, 99 + 1);
|
|
363
|
+ }
|
|
364
|
+
|
|
365
|
+ private boolean isSummer = () -> {
|
|
366
|
+ int i = 0;
|
|
367
|
+ return () -> {
|
|
368
|
+ i = i + 1;
|
|
369
|
+ return (i >= _3);
|
|
370
|
+ };
|
|
371
|
+ };
|
|
372
|
+
|
|
373
|
+ private void sendEmergencyText(String mesg, String phone) {}
|
|
374
|
+ private void tryServerRestart(String mesg, String phone) {}
|
|
375
|
+ int serverStatus = 5;
|
|
376
|
+ private boolean serverIsRunning() {return (serverStatus > 20);}
|
|
377
|
+ private void waitFor(int interval) {serverStatus += interval;}
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+}
|