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