Xzavia Cuello 6 anni fa
parent
commit
1cbd451400
2 ha cambiato i file con 120 aggiunte e 71 eliminazioni
  1. 119
    70
      WriteLoops.java
  2. 1
    1
      WriteLoopsTest.java

+ 119
- 70
WriteLoops.java Vedi File

19
         int w = 0;
19
         int w = 0;
20
 
20
 
21
         // Write a FOR loop that counts from 1 to 10.
21
         // Write a FOR loop that counts from 1 to 10.
22
-            // calling
22
+        // calling
23
+        for(int i = 1; i < 6; i++) {
23
             w = w + 1;
24
             w = w + 1;
24
             // each time through the loop
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
         return w;
30
         return w;
28
     }
31
     }
29
 
32
 
32
 
35
 
33
         // Write a FOR loop that counts from 1 to 10.
36
         // Write a FOR loop that counts from 1 to 10.
34
         // calling
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
         return w;
43
         return w;
39
     }
44
     }
40
 
45
 
43
 
48
 
44
         // Write a FOR loop that makes 10 iterations, start at 21.
49
         // Write a FOR loop that makes 10 iterations, start at 21.
45
         // calling
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
         return w;
56
         return w;
50
     }
57
     }
51
 
58
 
54
 
61
 
55
         // Write a FOR loop that counts down from 100 to 0.
62
         // Write a FOR loop that counts down from 100 to 0.
56
         // calling
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
         return w;
70
         return w;
61
     }
71
     }
62
 
72
 
65
 
75
 
66
         // Write a FOR loop from 0 to 32 by 2s.
76
         // Write a FOR loop from 0 to 32 by 2s.
67
         // calling
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
         return w;
82
         return w;
71
     }
83
     }
72
 
84
 
75
 
87
 
76
         // Write a FOR loop from 1 to less than 5001 by 11s.
88
         // Write a FOR loop from 1 to less than 5001 by 11s.
77
         // calling
89
         // calling
78
-        w = w + 1;
90
+        for(int i = 1; i < 5001; i += 11)
91
+            w = w + 1;
79
         // each time through the loop
92
         // each time through the loop
80
-        
93
+
81
         return w;
94
         return w;
82
     }
95
     }
83
 
96
 
86
 
99
 
87
         // Write a nested FOR loop(s), where one counts from
100
         // Write a nested FOR loop(s), where one counts from
88
         // 0 to less than 20 and the inner one counts from 0 to 4
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
                 w = w + 1;
105
                 w = w + 1;
91
                 // each time through the inner loop
106
                 // each time through the inner loop
92
 
107
 
108
+            }
109
+        }
93
         return w;
110
         return w;
94
     }
111
     }
95
 
112
 
100
         // statement inside the loop that checks the
117
         // statement inside the loop that checks the
101
         // loop index counter and if it’s greater than 51,
118
         // loop index counter and if it’s greater than 51,
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
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
                 w = w + 1;
124
                 w = w + 1;
125
+            }
126
+
106
             // each time through the inner loop
127
             // each time through the inner loop
107
-        
128
+        }
108
         return w;
129
         return w;
109
     }
130
     }
110
 
131
 
132
     public int driveHome() {
153
     public int driveHome() {
133
         int w = 0;
154
         int w = 0;
134
 
155
 
156
+        while(!gpsCurrentLocation().equals("Home")) {
157
+            driveSomeMore();
158
+            w++;
159
+        }
160
+
135
         // you need to use a .equals for two Strings.
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
     // Getting harder...
171
     // Getting harder...
155
         int runningScore = 0;
181
         int runningScore = 0;
156
 
182
 
157
         // do your while loop here
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
         return w; // >= 3;
200
         return w; // >= 3;
164
     }
201
     }
165
 
202
 
172
         int runningScore = 0;
209
         int runningScore = 0;
173
 
210
 
174
         // do your while loop here
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
         return w >= 3;
222
         return w >= 3;
181
     }
223
     }
182
 
224
 
187
     public int checkServerStatus() {
229
     public int checkServerStatus() {
188
         int w = 0;
230
         int w = 0;
189
         String adminPhoneNumber = "+1 202 456 1111";
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
         // calling
245
         // calling
193
-        w = w + 1;
246
+       
194
         // each time through the inner loop
247
         // each time through the inner loop
195
-        
248
+
196
         return w;
249
         return w;
197
     }
250
     }
198
 
251
 
202
     public int loop50by7() {
255
     public int loop50by7() {
203
         int w = 0;
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
         return w;
262
         return w;
211
     }
263
     }
212
 
264
 
239
         int w = 0;
291
         int w = 0;
240
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
298
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
299
         System.out.println(sumOfThrees);
249
 
300
 
256
         int w = 0;
307
         int w = 0;
257
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
314
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
315
         System.out.println(sumOfThrees);
266
 
316
 
279
         boolean onTime = true;
329
         boolean onTime = true;
280
 
330
 
281
         // ADD YOUR CODE here.
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
         return w;
337
         return w;
288
     }
338
     }
289
 
339
 
296
         int w = 0;
346
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
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
         return w;
353
         return w;
305
     }
354
     }
306
 
355
 
311
         int w = 0;
360
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
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
         return w;
367
         return w;
320
     }
368
     }
321
 
369
 
378
     //         return (i >= 3);
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
     private void sendEmergencyText(String mesg, String phone) {
438
     private void sendEmergencyText(String mesg, String phone) {
390
     }
439
     }
391
 
440
 

+ 1
- 1
WriteLoopsTest.java Vedi File

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