Browse Source

Merge branch 'solns' of kristofer/FirstSaturday into master

Kristofer Younger 6 years ago
parent
commit
6265cc214d
3 changed files with 187 additions and 86 deletions
  1. 2
    0
      .gitignore
  2. 108
    84
      WriteLoops.java
  3. 77
    2
      WriteLoopsTest.java

+ 2
- 0
.gitignore View File

21
 
21
 
22
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23
 hs_err_pid*
23
 hs_err_pid*
24
+
25
+.idea/*

+ 108
- 84
WriteLoops.java View File

1
 import com.sun.org.apache.xpath.internal.SourceTree;
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
  * Writeloops get you thinking about how to do different things with loops.
8
  * Writeloops get you thinking about how to do different things with loops.
5
  *
9
  *
6
  * @author kyounger
10
  * @author kyounger
7
- * @version 1.1
11
+ * @version 1.2
8
  */
12
  */
9
 public class WriteLoops {
13
 public class WriteLoops {
10
 
14
 
14
         int w = 0;
18
         int w = 0;
15
 
19
 
16
         // Write a FOR loop that counts from 1 to 10.
20
         // Write a FOR loop that counts from 1 to 10.
17
-        for (int i = 1; i <= 5; i++) {
18
             // calling
21
             // calling
19
             w = w + 1;
22
             w = w + 1;
20
             // each time through the loop
23
             // each time through the loop
21
-        }
22
 
24
 
23
         // this will tell the test how many times the loop executed.
25
         // this will tell the test how many times the loop executed.
24
         return w;
26
         return w;
28
         int w = 0;
30
         int w = 0;
29
 
31
 
30
         // Write a FOR loop that counts from 1 to 10.
32
         // Write a FOR loop that counts from 1 to 10.
31
-
32
         // calling
33
         // calling
33
         w = w + 1;
34
         w = w + 1;
34
         // each time through the loop
35
         // each time through the loop
35
-
36
+        
36
         return w;
37
         return w;
37
     }
38
     }
38
 
39
 
40
         int w = 0;
41
         int w = 0;
41
 
42
 
42
         // Write a FOR loop that makes 10 iterations, start at 21.
43
         // Write a FOR loop that makes 10 iterations, start at 21.
43
-
44
         // calling
44
         // calling
45
         w = w + 1;
45
         w = w + 1;
46
         // each time through the loop
46
         // each time through the loop
47
-
47
+        
48
         return w;
48
         return w;
49
     }
49
     }
50
 
50
 
52
         int w = 0;
52
         int w = 0;
53
 
53
 
54
         // Write a FOR loop that counts down from 100 to 0.
54
         // Write a FOR loop that counts down from 100 to 0.
55
-
56
         // calling
55
         // calling
57
         w = w + 1;
56
         w = w + 1;
58
         // each time through the loop
57
         // each time through the loop
59
-
58
+        
60
         return w;
59
         return w;
61
     }
60
     }
62
 
61
 
64
         int w = 0;
63
         int w = 0;
65
 
64
 
66
         // Write a FOR loop from 0 to 32 by 2s.
65
         // Write a FOR loop from 0 to 32 by 2s.
67
-
68
         // calling
66
         // calling
69
         w = w + 1;
67
         w = w + 1;
70
         // each time through the loop
68
         // each time through the loop
71
-
72
         return w;
69
         return w;
73
     }
70
     }
74
 
71
 
76
         int w = 0;
73
         int w = 0;
77
 
74
 
78
         // Write a FOR loop from 1 to less than 5001 by 11s.
75
         // Write a FOR loop from 1 to less than 5001 by 11s.
79
-
80
         // calling
76
         // calling
81
         w = w + 1;
77
         w = w + 1;
82
         // each time through the loop
78
         // each time through the loop
83
-
79
+        
84
         return w;
80
         return w;
85
     }
81
     }
86
 
82
 
89
 
85
 
90
         // Write a nested FOR loop(s), where one counts from
86
         // 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
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
         return w;
92
         return w;
98
     }
93
     }
105
         // loop index counter and if it’s greater than 51,
100
         // loop index counter and if it’s greater than 51,
106
         // prints “Hello Zipcode” instead of the statement w = w + 1;
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
         return w;
107
         return w;
113
     }
108
     }
114
 
109
 
138
 
133
 
139
         // you need to use a .equals for two Strings.
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
     // Getting harder...
144
     // Getting harder...
150
     // First declare and set “highestScore” to 236. Then set “currentScore” to
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
     // is less than “highestScore” and if it is, adds “currentScore” to
147
     // is less than “highestScore” and if it is, adds “currentScore” to
153
     // "runningScore"
148
     // "runningScore"
154
     // and then sets “currentScore” to “gameNextScore()”
149
     // and then sets “currentScore” to “gameNextScore()”
155
-    public boolean checkGameScore() {
150
+    public int checkGameScore() {
156
         int w = 0;
151
         int w = 0;
157
         int highestScore = 236;
152
         int highestScore = 236;
158
         int currentScore = gameNextScore();
153
         int currentScore = gameNextScore();
159
         int runningScore = 0;
154
         int runningScore = 0;
160
 
155
 
161
         // do your while loop here
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
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
165
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
179
 
172
 
180
         // do your while loop here
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
     // Write a WHILE loop that checks “serverIsRunning()” and if true
182
     // Write a WHILE loop that checks “serverIsRunning()” and if true
194
     // and also calls “tryServerRestart()”
185
     // and also calls “tryServerRestart()”
195
     public int checkServerStatus() {
186
     public int checkServerStatus() {
196
         int w = 0;
187
         int w = 0;
197
-        String adminPhoneNumber = "+1 202 456 1111"
188
+        String adminPhoneNumber = "+1 202 456 1111";
189
+        
190
+
198
         // calling
191
         // calling
199
         w = w + 1;
192
         w = w + 1;
200
         // each time through the inner loop
193
         // each time through the inner loop
201
-
194
+        
202
         return w;
195
         return w;
203
     }
196
     }
204
 
197
 
208
     public int loop50by7() {
201
     public int loop50by7() {
209
         int w = 0;
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
         return w;
209
         return w;
216
     }
210
     }
217
 
211
 
212
+    int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
213
+
218
     // Foo is method that add the first 7 factors of three together and prints
214
     // Foo is method that add the first 7 factors of three together and prints
219
     // out the sum of them all.
215
     // out the sum of them all.
220
     public int foo() {
216
     public int foo() {
221
         int w = 0;
217
         int w = 0;
222
         // this is an array of ints. it is of length 7 (from 0 -> 6)
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
         int sumOfThrees = 0;
219
         int sumOfThrees = 0;
225
 
220
 
226
         // this is a so called Enhanced for loop
221
         // this is a so called Enhanced for loop
243
         int w = 0;
238
         int w = 0;
244
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
246
         System.out.print("The Sum is ");
251
         System.out.println(sumOfThrees);
247
         System.out.println(sumOfThrees);
252
 
248
 
259
         int w = 0;
255
         int w = 0;
260
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
263
         System.out.print("The Sum is ");
267
         System.out.println(sumOfThrees);
264
         System.out.println(sumOfThrees);
268
 
265
 
274
     // inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
271
     // inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
275
     // “yellAtJuniorToMowLawn()”
272
     // “yellAtJuniorToMowLawn()”
276
     // After loop, call
273
     // After loop, call
277
-    // “sendJuniorBackToSchool(onTime)”
274
+    // “sendJuniorBackToSchool()” with an argument that decribes the day junior goes
275
+    // back.
278
     public int manageYardAndJunior() {
276
     public int manageYardAndJunior() {
279
         int w = 0;
277
         int w = 0;
280
         boolean onTime = true;
278
         boolean onTime = true;
281
 
279
 
282
         // ADD YOUR CODE here.
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
         return w;
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
     // Given an array voteTallies[], write a FOR loop that prints out each value in
292
     // Given an array voteTallies[], write a FOR loop that prints out each value in
296
     // the array.
293
     // the array.
297
     public int tallyVote1() {
294
     public int tallyVote1() {
298
         int w = 0;
295
         int w = 0;
299
         int numberOfVotes = voteTallies.length;
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
         return w;
303
         return w;
306
     }
304
     }
307
 
305
 
312
         int w = 0;
310
         int w = 0;
313
         int numberOfVotes = voteTallies.length;
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
         return w;
318
         return w;
320
     }
319
     }
321
 
320
 
362
         return this.scr = this.scr + ThreadLocalRandom.current().nextInt(20, 99 + 1);
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
     int serverStatus = 5;
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
 }

+ 77
- 2
WriteLoopsTest.java View File

51
     public void TestOneToTen()
51
     public void TestOneToTen()
52
     {
52
     {
53
         WriteLoops writeLoo1 = new WriteLoops();
53
         WriteLoops writeLoo1 = new WriteLoops();
54
-        assertEquals(5, writeLoo1.oneToTen());
54
+        assertEquals(10, writeLoo1.oneToTen());
55
     }
55
     }
56
     @Test
56
     @Test
57
     public void TestStartAtTwentyOne()
57
     public void TestStartAtTwentyOne()
58
     {
58
     {
59
         WriteLoops writeLoo1 = new WriteLoops();
59
         WriteLoops writeLoo1 = new WriteLoops();
60
-        assertEquals(5, writeLoo1.startAtTwentyOne());
60
+        assertEquals(11, writeLoo1.startAtTwentyOne());
61
+    }
62
+
63
+    @Test
64
+    public void TestCountDown()
65
+    {
66
+        WriteLoops writeLoo1 = new WriteLoops();
67
+        assertEquals(100, writeLoo1.countDown());
68
+    }
69
+
70
+    @Test
71
+    public void Test2to32()
72
+    {
73
+        WriteLoops writeLoo1 = new WriteLoops();
74
+        assertEquals(0, writeLoo1.byTwoTo32());
75
+    }
76
+
77
+    @Test
78
+    public void TestCountDownFrom5000()
79
+    {
80
+        WriteLoops writeLoo1 = new WriteLoops();
81
+        assertEquals(455, writeLoo1.countDownFrom5000());
82
+    }
83
+
84
+    @Test
85
+    public void TestNestedFors()
86
+    {
87
+        WriteLoops writeLoo1 = new WriteLoops();
88
+        assertEquals(100, writeLoo1.nestedFors());
89
+    }
90
+
91
+    @Test
92
+    public void TestHelloZipCode()
93
+    {
94
+        WriteLoops writeLoo1 = new WriteLoops();
95
+        assertEquals(47, writeLoo1.helloZipCode());
96
+    }
97
+
98
+    @Test
99
+    public void TestDriveHome()
100
+    {
101
+        WriteLoops writeLoo1 = new WriteLoops();
102
+        assertEquals(6, writeLoo1.driveHome());
103
+    }
104
+
105
+
106
+
107
+    @Test
108
+    public void TestCheckGameScore()
109
+    {
110
+        WriteLoops writeLoo1 = new WriteLoops();
111
+        assertEquals(3, writeLoo1.checkGameScore());
112
+    }
113
+
114
+    @Test
115
+    public void TestCheckGameScoreDoWhile()
116
+    {
117
+        WriteLoops writeLoo1 = new WriteLoops();
118
+        assertEquals(true, writeLoo1.checkGameScoreDoWhile());
119
+    }
120
+
121
+    @Test
122
+    public void TestCheckServer()
123
+    {
124
+        WriteLoops writeLoo1 = new WriteLoops();
125
+        assertEquals(3, writeLoo1.checkServerStatus());
61
     }
126
     }
62
 }
127
 }
63
 
128
 
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+