浏览代码

completed lab2

Xzavia Cuello 6 年前
父节点
当前提交
1cbd451400
共有 2 个文件被更改,包括 120 次插入71 次删除
  1. 119
    70
      WriteLoops.java
  2. 1
    1
      WriteLoopsTest.java

+ 119
- 70
WriteLoops.java 查看文件

@@ -19,11 +19,14 @@ public class WriteLoops {
19 19
         int w = 0;
20 20
 
21 21
         // Write a FOR loop that counts from 1 to 10.
22
-            // calling
22
+        // calling
23
+        for(int i = 1; i < 6; i++) {
23 24
             w = w + 1;
24 25
             // each time through the loop
25 26
 
26
-        // this will tell the test how many times the loop executed.
27
+            // this will tell the test how many times the loop executed.
28
+
29
+        }
27 30
         return w;
28 31
     }
29 32
 
@@ -32,9 +35,11 @@ public class WriteLoops {
32 35
 
33 36
         // Write a FOR loop that counts from 1 to 10.
34 37
         // calling
35
-        w = w + 1;
36
-        // each time through the loop
37
-        
38
+        for(int i = 1; i < 11; i++) {
39
+            w = w + 1;
40
+            // each time through the loop
41
+
42
+        }
38 43
         return w;
39 44
     }
40 45
 
@@ -43,9 +48,11 @@ public class WriteLoops {
43 48
 
44 49
         // Write a FOR loop that makes 10 iterations, start at 21.
45 50
         // calling
46
-        w = w + 1;
47
-        // each time through the loop
48
-        
51
+        for(int i = 21; i < 32; i++) {
52
+            w = w + 1;
53
+            // each time through the loop
54
+
55
+        }
49 56
         return w;
50 57
     }
51 58
 
@@ -54,9 +61,12 @@ public class WriteLoops {
54 61
 
55 62
         // Write a FOR loop that counts down from 100 to 0.
56 63
         // calling
57
-        w = w + 1;
58
-        // each time through the loop
59
-        
64
+        for(int i = 100; i > 0; i--) {
65
+
66
+            w = w + 1;
67
+            // each time through the loop
68
+
69
+        }
60 70
         return w;
61 71
     }
62 72
 
@@ -65,8 +75,10 @@ public class WriteLoops {
65 75
 
66 76
         // Write a FOR loop from 0 to 32 by 2s.
67 77
         // calling
68
-        w = w + 1;
69
-        // each time through the loop
78
+        for(int i = 0; i < 32; i += 2) {
79
+            w = w + 1;
80
+            // each time through the loop
81
+        }
70 82
         return w;
71 83
     }
72 84
 
@@ -75,9 +87,10 @@ public class WriteLoops {
75 87
 
76 88
         // Write a FOR loop from 1 to less than 5001 by 11s.
77 89
         // calling
78
-        w = w + 1;
90
+        for(int i = 1; i < 5001; i += 11)
91
+            w = w + 1;
79 92
         // each time through the loop
80
-        
93
+
81 94
         return w;
82 95
     }
83 96
 
@@ -86,10 +99,14 @@ public class WriteLoops {
86 99
 
87 100
         // Write a nested FOR loop(s), where one counts from
88 101
         // 0 to less than 20 and the inner one counts from 0 to 4
89
-                // calling
102
+        // calling
103
+        for(int i = 0; i < 20; i ++) {
104
+            for(int j = 0; j < 5; j++) {
90 105
                 w = w + 1;
91 106
                 // each time through the inner loop
92 107
 
108
+            }
109
+        }
93 110
         return w;
94 111
     }
95 112
 
@@ -100,11 +117,15 @@ public class WriteLoops {
100 117
         // statement inside the loop that checks the
101 118
         // loop index counter and if it’s greater than 51,
102 119
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103
-
104
-                // calling
120
+        for(int i = 5; i < 106; i++) {
121
+            if( i > 51) {
122
+                System.out.println("Hello Zipcode");
123
+            } else {
105 124
                 w = w + 1;
125
+            }
126
+
106 127
             // each time through the inner loop
107
-        
128
+        }
108 129
         return w;
109 130
     }
110 131
 
@@ -132,14 +153,19 @@ public class WriteLoops {
132 153
     public int driveHome() {
133 154
         int w = 0;
134 155
 
156
+        while(!gpsCurrentLocation().equals("Home")) {
157
+            driveSomeMore();
158
+            w++;
159
+        }
160
+
135 161
         // you need to use a .equals for two Strings.
162
+        System.out.println("Honey, I'm Home!");
136 163
 
137
-            // calling
138
-            w = w + 1;
139
-            // each time through the inner loop
140
-        
164
+        // calling
165
+
166
+        // each time through the inner loop
141 167
 
142
-            return w;
168
+        return w;
143 169
     }
144 170
 
145 171
     // Getting harder...
@@ -155,11 +181,22 @@ public class WriteLoops {
155 181
         int runningScore = 0;
156 182
 
157 183
         // do your while loop here
158
- 
159
-            // calling
160
-            w = w + 1;
161
-            // each time through the inner loop
162
-        
184
+        while(runningScore < highestScore) {
185
+            if(runningScore < highestScore) {
186
+                runningScore += currentScore;
187
+                currentScore = gameNextScore();
188
+
189
+                w++;
190
+            }
191
+
192
+        }
193
+
194
+        currentScore = gameNextScore();
195
+
196
+        // calling
197
+
198
+        // each time through the inner loop
199
+
163 200
         return w; // >= 3;
164 201
     }
165 202
 
@@ -172,11 +209,16 @@ public class WriteLoops {
172 209
         int runningScore = 0;
173 210
 
174 211
         // do your while loop here
212
+        do{
213
+            if(runningScore < highestScore) {
214
+                runningScore += currentScore;
175 215
 
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
216
+            }
217
+            w++;
218
+        } while (runningScore < highestScore);
179 219
 
220
+        // calling
221
+        // each time through the inner loop
180 222
         return w >= 3;
181 223
     }
182 224
 
@@ -187,12 +229,23 @@ public class WriteLoops {
187 229
     public int checkServerStatus() {
188 230
         int w = 0;
189 231
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
232
+
233
+        while(serverIsRunning() == true) {
234
+            if(serverIsRunning() == true) {
235
+                waitFor(5);
236
+                
237
+            }
238
+            w++;
239
+}
240
+        if(serverIsRunning() == false) {
241
+            sendEmergencyText("Help!", adminPhoneNumber);
242
+            tryServerRestart("Help!", adminPhoneNumber);
243
+        }
191 244
 
192 245
         // calling
193
-        w = w + 1;
246
+       
194 247
         // each time through the inner loop
195
-        
248
+
196 249
         return w;
197 250
     }
198 251
 
@@ -202,11 +255,10 @@ public class WriteLoops {
202 255
     public int loop50by7() {
203 256
         int w = 0;
204 257
 
258
+        // calling
259
+        w = w + 1;
260
+        // each time through the inner loop
205 261
 
206
-            // calling
207
-            w = w + 1;
208
-            // each time through the inner loop
209
-        
210 262
         return w;
211 263
     }
212 264
 
@@ -239,11 +291,10 @@ public class WriteLoops {
239 291
         int w = 0;
240 292
         int sumOfThrees = 0;
241 293
 
242
- 
243
-            // calling
244
-            w = w + 1;
245
-            // each time through the inner loop
246
-        
294
+        // calling
295
+        w = w + 1;
296
+        // each time through the inner loop
297
+
247 298
         System.out.print("The Sum is ");
248 299
         System.out.println(sumOfThrees);
249 300
 
@@ -256,11 +307,10 @@ public class WriteLoops {
256 307
         int w = 0;
257 308
         int sumOfThrees = 0;
258 309
 
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
263
-        
310
+        // calling
311
+        w = w + 1;
312
+        // each time through the inner loop
313
+
264 314
         System.out.print("The Sum is ");
265 315
         System.out.println(sumOfThrees);
266 316
 
@@ -279,11 +329,11 @@ public class WriteLoops {
279 329
         boolean onTime = true;
280 330
 
281 331
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
286
-        
332
+
333
+        // be sure to call
334
+        w = w + 1;
335
+        // each time inside the loop
336
+
287 337
         return w;
288 338
     }
289 339
 
@@ -296,11 +346,10 @@ public class WriteLoops {
296 346
         int w = 0;
297 347
         int numberOfVotes = voteTallies.length;
298 348
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
-        
349
+        // calling
350
+        w = w + 1;
351
+        // each time through the inner loop
352
+
304 353
         return w;
305 354
     }
306 355
 
@@ -311,11 +360,10 @@ public class WriteLoops {
311 360
         int w = 0;
312 361
         int numberOfVotes = voteTallies.length;
313 362
 
363
+        // calling
364
+        w = w + 1;
365
+        // each time through the inner loop
314 366
 
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
319 367
         return w;
320 368
     }
321 369
 
@@ -378,14 +426,15 @@ public class WriteLoops {
378 426
     //         return (i >= 3);
379 427
     //     };
380 428
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
429
+    private int summer = 0;
430
+    private boolean isSummer() {
431
+        if (summer == 3) {
432
+            return true;
388 433
         }
434
+        summer++;
435
+        return false;
436
+    }
437
+
389 438
     private void sendEmergencyText(String mesg, String phone) {
390 439
     }
391 440
 

+ 1
- 1
WriteLoopsTest.java 查看文件

@@ -71,7 +71,7 @@ public class WriteLoopsTest
71 71
     public void Test2to32()
72 72
     {
73 73
         WriteLoops writeLoo1 = new WriteLoops();
74
-        assertEquals(0, writeLoo1.byTwoTo32());
74
+        assertEquals(16, writeLoo1.byTwoTo32());
75 75
     }
76 76
 
77 77
     @Test