Browse Source

Fixed Inverted Fors and completed while loop methods

Trinh Tong 6 years ago
parent
commit
c4477f8fd8
3 changed files with 41 additions and 23 deletions
  1. 37
    19
      WriteLoops.java
  2. 1
    1
      WriteLoopsTest.java
  3. 3
    3
      package.bluej

+ 37
- 19
WriteLoops.java View File

43
     }
43
     }
44
 
44
 
45
     public int startAtTwentyOne() {
45
     public int startAtTwentyOne() {
46
-        int w = 21;
46
+        int w = 0;
47
 
47
 
48
         // Write a FOR loop that makes 10 iterations, start at 21.
48
         // Write a FOR loop that makes 10 iterations, start at 21.
49
-        for (int i = 0; i < 10; i++) {
50
-            w = w - 1;
49
+        for (int i = 0; i <= 10; i++) {
50
+            w = w + 1;
51
             // each time through the loop
51
             // each time through the loop
52
          }
52
          }
53
         
53
         
73
         // Write a FOR loop from 0 to 32 by 2s.
73
         // Write a FOR loop from 0 to 32 by 2s.
74
         for (int i = 32; i > 2; i -= 2) {
74
         for (int i = 32; i > 2; i -= 2) {
75
             // calling
75
             // calling
76
-            //w = w + 1;
76
+            w = w + 1;
77
             // each time through the loop
77
             // each time through the loop
78
         }
78
         }
79
         return w;
79
         return w;
83
         int w = 0;
83
         int w = 0;
84
 
84
 
85
         // Write a FOR loop from 1 to less than 5001 by 11s.
85
         // Write a FOR loop from 1 to less than 5001 by 11s.
86
-        for (int i = 5001; i > 1; i -= 11) {
86
+        for (int i = 1; i < 5001; i += 11) {
87
             // calling
87
             // calling
88
             w = w + 1;
88
             w = w + 1;
89
             // each time through the loop
89
             // each time through the loop
97
 
97
 
98
         // Write a nested FOR loop(s), where one counts from
98
         // Write a nested FOR loop(s), where one counts from
99
         // 0 to less than 20 and the inner one counts from 0 to 4
99
         // 0 to less than 20 and the inner one counts from 0 to 4
100
-        for (int i = 20; i > 0; i--) {
100
+        for (int i = 0; i < 20; i++) {
101
             for (int j = 0; j <= 4; j ++) {// calling
101
             for (int j = 0; j <= 4; j ++) {// calling
102
                 w = w + 1;
102
                 w = w + 1;
103
                 // each time through the inner loop
103
                 // each time through the inner loop
237
     // and if it is, add 7 to “i”
237
     // and if it is, add 7 to “i”
238
     public int loop50by7() {
238
     public int loop50by7() {
239
         int w = 0;
239
         int w = 0;
240
-
241
-
242
-            // calling
243
-            w = w + 1;
244
-            // each time through the inner loop
240
+        int i = 7;
245
         
241
         
242
+        while (i < 50) {
243
+            i += 7;
244
+            w = w + 1;
245
+        }
246
+
246
         return w;
247
         return w;
247
     }
248
     }
248
 
249
 
275
         int w = 0;
276
         int w = 0;
276
         int sumOfThrees = 0;
277
         int sumOfThrees = 0;
277
 
278
 
278
- 
279
+        for (int index = 0; index < 7; index++) {
280
+            sumOfThrees += threes_array[index];
279
             // calling
281
             // calling
280
             w = w + 1;
282
             w = w + 1;
281
             // each time through the inner loop
283
             // each time through the inner loop
282
-        
284
+        }
283
         System.out.print("The Sum is ");
285
         System.out.print("The Sum is ");
284
         System.out.println(sumOfThrees);
286
         System.out.println(sumOfThrees);
285
 
287
 
291
     public int rewriteFooAsWhile() {
293
     public int rewriteFooAsWhile() {
292
         int w = 0;
294
         int w = 0;
293
         int sumOfThrees = 0;
295
         int sumOfThrees = 0;
294
-
295
- 
296
+        int index = 0;
297
+        while (index <= 7) {
298
+            sumOfThrees += threes_array[index];
296
             // calling
299
             // calling
297
             w = w + 1;
300
             w = w + 1;
301
+            index ++;
302
+        }
298
             // each time through the inner loop
303
             // each time through the inner loop
299
         
304
         
300
         System.out.print("The Sum is ");
305
         System.out.print("The Sum is ");
313
     public int manageYardAndJunior() {
318
     public int manageYardAndJunior() {
314
         int w = 0;
319
         int w = 0;
315
         boolean onTime = true;
320
         boolean onTime = true;
321
+        boolean yardNeedsMowed = true;
316
 
322
 
323
+        while (isSummer() == true) {
324
+            if (yardNeedsMowed == true) {
325
+                yellAtJuniorToMowLawn();
326
+            }
327
+        }
328
+        
329
+        sendJuniorBackToSchool("Tuesday");
317
         // ADD YOUR CODE here.
330
         // ADD YOUR CODE here.
318
  
331
  
319
             // be sure to call
332
             // be sure to call
331
     public int tallyVote1() {
344
     public int tallyVote1() {
332
         int w = 0;
345
         int w = 0;
333
         int numberOfVotes = voteTallies.length;
346
         int numberOfVotes = voteTallies.length;
334
-
335
- 
347
+        for (int i = 0; i < numberOfVotes; i++) {
348
+            System.out.print(voteTallies[i]);
336
             // calling
349
             // calling
337
             w = w + 1;
350
             w = w + 1;
338
             // each time through the inner loop
351
             // each time through the inner loop
352
+        }
353
+ 
339
         
354
         
340
         return w;
355
         return w;
341
     }
356
     }
346
     public int tallyVote2() {
361
     public int tallyVote2() {
347
         int w = 0;
362
         int w = 0;
348
         int numberOfVotes = voteTallies.length;
363
         int numberOfVotes = voteTallies.length;
349
-
364
+        int index = 0;
365
+        while (index < numberOfVotes) {
366
+            System.out.print(voteTallies[index]);
367
+            index++;
350
 
368
 
351
             // calling
369
             // calling
352
             w = w + 1;
370
             w = w + 1;
353
             // each time through the inner loop
371
             // each time through the inner loop
354
-        
372
+        }
355
         return w;
373
         return w;
356
     }
374
     }
357
 
375
 

+ 1
- 1
WriteLoopsTest.java View 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(15, writeLoo1.byTwoTo32());
75
     }
75
     }
76
 
76
 
77
     @Test
77
     @Test

+ 3
- 3
package.bluej View File

6
 dependency2.to=WriteIFs
6
 dependency2.to=WriteIFs
7
 dependency2.type=UsesDependency
7
 dependency2.type=UsesDependency
8
 editor.fx.0.height=722
8
 editor.fx.0.height=722
9
-editor.fx.0.width=989
10
-editor.fx.0.x=371
11
-editor.fx.0.y=118
9
+editor.fx.0.width=800
10
+editor.fx.0.x=320
11
+editor.fx.0.y=75
12
 objectbench.height=111
12
 objectbench.height=111
13
 objectbench.width=1241
13
 objectbench.width=1241
14
 package.divider.horizontal=0.6
14
 package.divider.horizontal=0.6