|
@@ -1,68 +1,356 @@
|
1
|
1
|
# Saturday-Exercises
|
2
|
2
|
|
3
|
|
-some simpler exercises for IFs and loops.
|
|
3
|
+some simpler exercises for IFs and loops.
|
4
|
4
|
|
5
|
5
|
# Happy 1st Saturday, Newbies!
|
6
|
6
|
|
7
|
|
-Yes, I am writing these on my phone, from I-70west in Ohio. No, I am not currently driving.
|
|
7
|
+Yes, I am writing these on my phone, from I-70west in Ohio. No, I am not currently driving.
|
8
|
8
|
|
9
|
|
-Some extra (easier) exercises.
|
|
9
|
+Some extra (easier) exercises.
|
10
|
10
|
|
11
|
|
-Use them to practice writing both pseudo code and Java. Write each one in both.
|
|
11
|
+Use them to practice writing both pseudo code and Java. Write each one in both.
|
12
|
12
|
|
13
|
13
|
Strive to feel good about the practice as you complete each one.
|
14
|
14
|
|
15
|
|
-I suggest you write them into a notebook or onto paper. Once, you written them all, go thru them and type them into this file. You can use “atom” for that.
|
|
15
|
+I suggest you write them into a notebook or onto paper. Once, you written them all, go thru them and type them into this file. You can use “atom” for that.
|
16
|
16
|
|
17
|
|
-Then, if you’d like comments on your work, do a Pull Request and make the title of the PR “comments please”.
|
|
17
|
+Then, if you’d like comments on your work, do a Pull Request and make the title of the PR “comments please”.
|
18
|
18
|
|
19
|
19
|
|
20
|
20
|
## IFs
|
21
|
21
|
|
22
|
22
|
Write an IF statement that checks “player1.isAlive()” and if that’s false, calls “displayGameOver(player1)”
|
23
|
23
|
|
|
24
|
+ pseudo:
|
|
25
|
+ - test the state of player1.isAlive()
|
|
26
|
+ - if true
|
|
27
|
+ - do nothing
|
|
28
|
+ - if false
|
|
29
|
+ - call displayGameOver(player1)
|
|
30
|
+Java
|
|
31
|
+
|
|
32
|
+if(player1.isAlive() != true) {
|
|
33
|
+ displayGameOver(player1);
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+
|
24
|
37
|
Write an IF statement that checks the “temperature(room)” and if that check is less than 70, calls “heatOn()” else calls “coolOn()”
|
25
|
38
|
|
|
39
|
+pseudo:
|
|
40
|
+- test the result of temperature(room)
|
|
41
|
+ - if < 70
|
|
42
|
+ - call heatOn(room)
|
|
43
|
+ - else
|
|
44
|
+ - call heatOn(room)
|
|
45
|
+
|
|
46
|
+Java:
|
|
47
|
+
|
|
48
|
+if(temperature(room) < 70) {
|
|
49
|
+ heatOn(room);
|
|
50
|
+} else { heatOff(room);}
|
|
51
|
+
|
26
|
52
|
Write an IF statement that checks “outsideTemp()” is less than 50 AND “insideTemp()” is less than 62, calls “startAFire(fireplace1)”
|
27
|
53
|
|
|
54
|
+pseudo:
|
|
55
|
+- test if outsideTemp is < 50 AND inside tempTemp is < 62
|
|
56
|
+ - if true
|
|
57
|
+ - call startAFire(fireplace1)
|
|
58
|
+ - if false
|
|
59
|
+ - do nothing
|
|
60
|
+
|
|
61
|
+Java:
|
|
62
|
+
|
|
63
|
+if(outsideTemp() < 50 && insideTemp() < 62) {
|
|
64
|
+ startAFire(fireplace1);
|
|
65
|
+}
|
|
66
|
+
|
28
|
67
|
Write an IF statement that checks “fuelLevel” and if that check is less than 0.08, calls “refuel()”
|
29
|
68
|
|
|
69
|
+pseudo:
|
|
70
|
+- test if fuelLevel is < 0.08
|
|
71
|
+ - if true
|
|
72
|
+ - call refuel();
|
|
73
|
+ - if false
|
|
74
|
+ - do nothing
|
|
75
|
+
|
|
76
|
+Java:
|
|
77
|
+
|
|
78
|
+if(fuelLevel < 0.08) {
|
|
79
|
+ refuel();
|
|
80
|
+}
|
|
81
|
+
|
30
|
82
|
## Loops
|
31
|
83
|
|
32
|
|
-For each loop you write, print “Hello” on each loop iteration.
|
|
84
|
+For each loop you write, print “Hello” on each loop iteration.
|
|
85
|
+
|
|
86
|
+pseudo:
|
|
87
|
+
|
|
88
|
+- Create loop that prints "Hello" each time it iterates
|
|
89
|
+
|
|
90
|
+Java:
|
|
91
|
+
|
|
92
|
+for (int i = 0; i <=10; i++) {
|
|
93
|
+ System.out.println("Hello");
|
|
94
|
+}
|
33
|
95
|
|
34
|
|
-Write a FOR loop that counts from 1 to 10.
|
|
96
|
+Write a FOR loop that counts from 1 to 10.
|
|
97
|
+
|
|
98
|
+pseudo:
|
|
99
|
+- Create a loop that starts at 1
|
|
100
|
+- prints the counter on each iteration
|
|
101
|
+- end when counter is <= 10
|
|
102
|
+
|
|
103
|
+Java:
|
|
104
|
+
|
|
105
|
+for (int i = 1; i <= 10; i++) {
|
|
106
|
+ System.out.println(i);
|
|
107
|
+}
|
35
|
108
|
|
36
|
109
|
Write a FOR loop that makes 10 iterations, start at 21
|
37
|
110
|
|
|
111
|
+pseudo:
|
|
112
|
+- create for loop that starts at i = 21
|
|
113
|
+ - each iteration i++
|
|
114
|
+ - end loop when i >= 30
|
|
115
|
+ - print the counter each iteration
|
|
116
|
+
|
|
117
|
+Java
|
|
118
|
+
|
|
119
|
+for(int i = 21; i <= 30; i++) {
|
|
120
|
+ System.out.println(i);
|
|
121
|
+}
|
|
122
|
+
|
38
|
123
|
Write a FOR loop that counts down from 100 to 0
|
39
|
124
|
|
|
125
|
+pseudo:
|
|
126
|
+- create for loop that starts at 100
|
|
127
|
+ - iterate until at 0
|
|
128
|
+ - counter -- each iteration to reach 0
|
|
129
|
+ - print counter each iteration
|
|
130
|
+
|
|
131
|
+Java:
|
|
132
|
+
|
|
133
|
+for(int i = 100; i <= 0; i--) {
|
|
134
|
+ System.out.println(i);
|
|
135
|
+}
|
|
136
|
+
|
40
|
137
|
Write a FOR loop from 0 to 32 by 2s
|
41
|
138
|
|
42
|
|
-Write a FOR loop from 1 to less than 5001 by 11s.
|
|
139
|
+pseudo:
|
|
140
|
+- create for loop starting at 0
|
|
141
|
+ - stop iterations once counter reaches 32
|
|
142
|
+ - increase counter by 2 after each iteration
|
|
143
|
+
|
|
144
|
+Java:
|
|
145
|
+
|
|
146
|
+for(int i = 0; i >= 32; i +=2) {}
|
|
147
|
+
|
|
148
|
+Write a FOR loop from 1 to less than 5001 by 11s.
|
|
149
|
+
|
|
150
|
+pseudo:
|
|
151
|
+
|
|
152
|
+- create for loop starting at 1
|
|
153
|
+ - stop iterations once counter is greater than or equal to 5000
|
|
154
|
+ - increase counter by 11 after each iteration
|
|
155
|
+
|
|
156
|
+Java:
|
|
157
|
+
|
|
158
|
+for(int i = 1; i < 5001; i += 11) {}
|
|
159
|
+
|
|
160
|
+Write a nested FOR loop(s), where one counts from 0 to less than 20 and the inner one counts from 0 to 4.
|
|
161
|
+
|
|
162
|
+pseudo:
|
|
163
|
+
|
|
164
|
+- create for loop starting at 0
|
|
165
|
+ - end loop once counter is > 19
|
|
166
|
+ - increase count by 1 after each iteration
|
|
167
|
+- inside for loop create for loop 2
|
|
168
|
+ - start for loop 2 at 0
|
|
169
|
+ - end for loop 2 when counter is greater than 4
|
|
170
|
+ - increase counter by 1 after each iteration
|
|
171
|
+
|
|
172
|
+Java:
|
|
173
|
+
|
|
174
|
+for(int i = 0; i < 20 ; i++){
|
|
175
|
+ for(int j = 0; j <= 4; j++){
|
|
176
|
+
|
|
177
|
+ }
|
|
178
|
+}
|
43
|
179
|
|
44
|
|
-Write a nested FOR loop(s), where one counts from 0 to less than 20 and the inner one counts from 0 to 4.
|
45
|
180
|
|
46
|
181
|
Write a FOR loop that counts from 5 to 105. Put an IF statement inside the loop that checks the loop index counter and if it’s greater than 51, prints “Hello Zipcode” instead on “hello”
|
47
|
182
|
|
|
183
|
+pseudo:
|
|
184
|
+
|
|
185
|
+- create for loop that starts at 5
|
|
186
|
+ - end the loop when counter is > 105
|
|
187
|
+ - increase counter by one each iteration
|
|
188
|
+ - create if statement inside for loop
|
|
189
|
+ - if the for loop counter is greater than 51
|
|
190
|
+ - print "Hello Zipcode"
|
|
191
|
+
|
|
192
|
+Java:
|
|
193
|
+
|
|
194
|
+for(int i = 5; i <= 105; i ++) {
|
|
195
|
+ if(i > 51) {
|
|
196
|
+ System.out.println("Hello Zipcode");
|
|
197
|
+ }
|
|
198
|
+}
|
|
199
|
+
|
48
|
200
|
Write a WHILE loop that checks “gps.currentLocation()” and if that is not equal to “Home” then and it calls “driveSomeMore()”. After the loop is done, print “Honey, I’m Home!”
|
49
|
201
|
|
50
|
|
-First set “highestScore” to 0. Then set “currentScore” to “game.nextScore()”. Then write a WHILE loop that checks “currentScore” is greater than “highestScore” and if it is, sets “highestScore” to “currentScore” and then sets “currentScore” to “game.nextScore()”.
|
|
202
|
+pseudo:
|
|
203
|
+- create while loop set to gps.currentLocation() not equal to Home
|
|
204
|
+ - call driveSomeMore() inside while loop
|
|
205
|
+- under while loop print “Honey, I’m Home!”
|
51
|
206
|
|
52
|
|
-Rewrite the previous WHILE loop as a REPEAT..UNTIL loop. Notice how the “currentScore” variable usage is different.
|
|
207
|
+Java:
|
|
208
|
+
|
|
209
|
+while( gps.currentLocation != "Home") {
|
|
210
|
+ driveSomeMore();
|
|
211
|
+}
|
|
212
|
+
|
|
213
|
+System.out.println("Honey, I'm Home!");
|
|
214
|
+
|
|
215
|
+First set “highestScore” to 0. Then set “currentScore” to “game.nextScore()”. Then write a WHILE loop that checks “currentScore” is greater than “highestScore” and if it is, sets “highestScore” to “currentScore” and then sets “currentScore” to “game.nextScore()”.
|
|
216
|
+
|
|
217
|
+pseudo:
|
|
218
|
+
|
|
219
|
+- set highestScore to 0
|
|
220
|
+- set currentScore = game.nextScore()
|
|
221
|
+- create while loop that runs until currentScore is greater than highestScore
|
|
222
|
+ - once while loop ends set highestScore to currentScore and sets currentScore to game.nextScore()
|
|
223
|
+
|
|
224
|
+Java:
|
|
225
|
+
|
|
226
|
+highestScore = 0;
|
|
227
|
+currentScore = game.nextScore();
|
|
228
|
+
|
|
229
|
+while(highestScore > currentScore) {
|
|
230
|
+
|
|
231
|
+ currentScore = game.nextScore();
|
|
232
|
+}
|
|
233
|
+
|
|
234
|
+highestScore = currentScore;
|
|
235
|
+currentScore = game.nextScore();
|
|
236
|
+
|
|
237
|
+Rewrite the previous WHILE loop as a REPEAT..UNTIL loop. Notice how the “currentScore” variable usage is different.
|
|
238
|
+
|
|
239
|
+pseudo:
|
|
240
|
+
|
|
241
|
+- do some code
|
|
242
|
+ - compare IF currentScore < highestScore
|
|
243
|
+ - currentScore = game.nextScore();
|
|
244
|
+ - else highestScore = currentScore;
|
|
245
|
+ - while highestScore > currentScore
|
|
246
|
+
|
|
247
|
+Java:
|
|
248
|
+do {
|
|
249
|
+ if (currentScore < highestScore) {
|
|
250
|
+ currentScore = game.nextScore();
|
|
251
|
+ } else {
|
|
252
|
+ highestScore = currentScore
|
|
253
|
+ }
|
|
254
|
+} while (highestScore > currentScore);
|
53
|
255
|
|
54
|
256
|
Write a WHILE loop that checks “server.isRunning()” and if true calls “waitFor(5)”
|
55
|
257
|
After the loop, write an IF and check “server.isRunning()” is false, and if so, call “sendEmergencyText(“Help!”, admin.iPhoneNumber)” and also calls “tryServerRestart()”
|
56
|
258
|
|
57
|
|
-Declare an “int” i. Set i to 7. Write a WHILE loop that checks “i” is less than 50, and if it is, add 7 to “i”
|
|
259
|
+pseudo:
|
|
260
|
+
|
|
261
|
+- while loop that runs when server.isRunning() is true
|
|
262
|
+ - if server.isRunning() is true
|
|
263
|
+ - then call waitFor(5)
|
|
264
|
+- under while loop
|
|
265
|
+ - if server.isRunning() is false
|
|
266
|
+ - then call sendEmergencyText("Help!", admin.iPhoneNumber) and call tryServerRestart();
|
|
267
|
+
|
|
268
|
+Java:
|
|
269
|
+
|
|
270
|
+while (server.isRunning == true) {
|
|
271
|
+ waitFor(5);
|
|
272
|
+}
|
|
273
|
+
|
|
274
|
+if (server.isRunning != true) {
|
|
275
|
+ sendEmergencyText("Help!", admin.iPhoneNumber);
|
|
276
|
+ tryServerRestart();
|
|
277
|
+}
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+Declare an “int” i. Set i to 7. Write a WHILE loop that checks “i” is less than 50, and if it is, add 7 to “i”
|
|
281
|
+
|
|
282
|
+pseudo:
|
58
|
283
|
|
59
|
|
-Given an array voteTallies[], write a FOR loop that prints out each value in the array.
|
|
284
|
+- declare variable i = 7
|
|
285
|
+- while i is less than 50
|
|
286
|
+ - add seven to the value of i;
|
60
|
287
|
|
61
|
|
-Given an array voteTallies[], write a WHILE loop that prints out each value in the array. You should declare and use an index “idx” to keep track of where you are.
|
|
288
|
+Java:
|
|
289
|
+
|
|
290
|
+int i = 7;
|
|
291
|
+while(i < 50) {
|
|
292
|
+ i += 7;
|
|
293
|
+}
|
|
294
|
+
|
|
295
|
+Given an array voteTallies[], write a FOR loop that prints out each value in the array.
|
|
296
|
+
|
|
297
|
+pseudo:
|
|
298
|
+
|
|
299
|
+- for loop starts counter at 0
|
|
300
|
+ - stops when counter gets to the length of the voteTallies array minus one
|
|
301
|
+ - print index value of voteTallies where counter is the index position
|
|
302
|
+ - add one to counter after each loop
|
|
303
|
+
|
|
304
|
+Java:
|
|
305
|
+
|
|
306
|
+for( int i = 0; i <= voteTallies.length() - 1; i ++) {
|
|
307
|
+ System.out.println(voteTallies[i]);
|
|
308
|
+}
|
|
309
|
+
|
|
310
|
+Given an array voteTallies[], write a WHILE loop that prints out each value in the array. You should declare and use an index “idx” to keep track of where you are.
|
|
311
|
+
|
|
312
|
+pseudo:
|
|
313
|
+
|
|
314
|
+- declare idx variable set to 0
|
|
315
|
+- while idx is less than or equal to (voteTallies.length()-1)
|
|
316
|
+ - print value at voteTallies[idx]; and add one to idx value
|
|
317
|
+
|
|
318
|
+Java:
|
|
319
|
+
|
|
320
|
+int idx = 0;
|
|
321
|
+
|
|
322
|
+while(idx < (voteTallies.length() - 1)) {
|
|
323
|
+ System.out.println(voteTallies[idx]);
|
|
324
|
+ idx ++ ;
|
|
325
|
+}
|
62
|
326
|
|
63
|
327
|
Ponder this: can all FOR loops be rewritten as WHILE loops?
|
|
328
|
+- yes because you can fit all of the parameters of a for loop into the code of a while loop
|
|
329
|
+
|
64
|
330
|
|
65
|
331
|
Ponder this: can all WHILE loops be rewritten as FOR loops?
|
66
|
|
-
|
|
332
|
+ - no because a while loop validates the truthiness as a requirement to operate and that might not translate into the start/stop scenario that a for loop requires to run
|
|
333
|
+
|
67
|
334
|
Set “yardNeedsMowed” to true. Write WHILE loop that checks for “isSummer()”. inside the loop, write an IF that checks “yardNeedsMowed” and if true calls “yellAtJunior(chores.mowLawn())”
|
68
|
335
|
After loop, call “sendJuniorToSchool(onTime)”
|
|
336
|
+
|
|
337
|
+pseudo:
|
|
338
|
+
|
|
339
|
+- declare boolean yardNeedsMowed set to true
|
|
340
|
+- while isSummer() is true
|
|
341
|
+ - if yardNeedsMowed is true
|
|
342
|
+ - call yellAtJunior(chores.mowLawn())
|
|
343
|
+- after while loop
|
|
344
|
+ - call sendJuniorToSchool(onTime)
|
|
345
|
+
|
|
346
|
+ Java:
|
|
347
|
+
|
|
348
|
+ boolean yardNeedsMowed = true;
|
|
349
|
+
|
|
350
|
+ while (isSummer()) {
|
|
351
|
+ if (yardNeedsMowed == true) {
|
|
352
|
+ yellAtJunior(chores.mowLawn());
|
|
353
|
+ }
|
|
354
|
+ }
|
|
355
|
+
|
|
356
|
+ sendJuniorToSchool(onTime);
|