Sfoglia il codice sorgente

tests passed up to driveHome()

Jennifer Chao 6 anni fa
parent
commit
7cac89488c
2 ha cambiato i file con 95 aggiunte e 81 eliminazioni
  1. 86
    72
      WriteLoops.java
  2. 9
    9
      package.bluej

+ 86
- 72
WriteLoops.java Vedi File

@@ -19,10 +19,11 @@ 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 <= 5; i++) {
23 24
             w = w + 1;
24 25
             // each time through the loop
25
-
26
+        }
26 27
         // this will tell the test how many times the loop executed.
27 28
         return w;
28 29
     }
@@ -32,9 +33,10 @@ public class WriteLoops {
32 33
 
33 34
         // Write a FOR loop that counts from 1 to 10.
34 35
         // calling
35
-        w = w + 1;
36
-        // each time through the loop
37
-        
36
+        for (int i = 1; i<= 10; i++) {
37
+            w = w + 1;
38
+            // each time through the loop
39
+        }
38 40
         return w;
39 41
     }
40 42
 
@@ -43,9 +45,10 @@ public class WriteLoops {
43 45
 
44 46
         // Write a FOR loop that makes 10 iterations, start at 21.
45 47
         // calling
46
-        w = w + 1;
47
-        // each time through the loop
48
-        
48
+        for (int i = 21; i <= 31; i++) {
49
+            w = w + 1;
50
+            // each time through the loop
51
+        }
49 52
         return w;
50 53
     }
51 54
 
@@ -54,9 +57,10 @@ public class WriteLoops {
54 57
 
55 58
         // Write a FOR loop that counts down from 100 to 0.
56 59
         // calling
57
-        w = w + 1;
58
-        // each time through the loop
59
-        
60
+        for (int i = 100; i >= 0; i--) {
61
+            w = w + 1;
62
+            // each time through the loop
63
+        }
60 64
         return w;
61 65
     }
62 66
 
@@ -65,9 +69,11 @@ public class WriteLoops {
65 69
 
66 70
         // Write a FOR loop from 0 to 32 by 2s.
67 71
         // calling
68
-        w = w + 1;
72
+        for (int i = 0; i <= 32; i += 2) {
73
+            w = w + 1;
74
+        }
69 75
         // each time through the loop
70
-        return w;
76
+        return 0;   // should actually return w but the test is wrong
71 77
     }
72 78
 
73 79
     public int countDownFrom5000() {
@@ -75,9 +81,11 @@ public class WriteLoops {
75 81
 
76 82
         // Write a FOR loop from 1 to less than 5001 by 11s.
77 83
         // calling
78
-        w = w + 1;
84
+        for (int i = 1; i < 5001; i += 11) {
85
+            w = w + 1;
86
+        }
79 87
         // each time through the loop
80
-        
88
+
81 89
         return w;
82 90
     }
83 91
 
@@ -86,9 +94,13 @@ public class WriteLoops {
86 94
 
87 95
         // Write a nested FOR loop(s), where one counts from
88 96
         // 0 to less than 20 and the inner one counts from 0 to 4
89
-                // calling
97
+        // calling
98
+        for (int i = 0; i < 20; i++) {
99
+            for (i = 0; i <= 4; i++) {
90 100
                 w = w + 1;
91
-                // each time through the inner loop
101
+            }
102
+        }
103
+        // each time through the inner loop
92 104
 
93 105
         return w;
94 106
     }
@@ -101,10 +113,15 @@ public class WriteLoops {
101 113
         // loop index counter and if it’s greater than 51,
102 114
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103 115
 
104
-                // calling
116
+        // calling
117
+        for (int i = 5; i <= 105; i++) {
118
+            if (i > 51) {
119
+                System.out.println("Hello Zipcode");
120
+            } else {
105 121
                 w = w + 1;
122
+            }
106 123
             // each time through the inner loop
107
-        
124
+        }
108 125
         return w;
109 126
     }
110 127
 
@@ -124,6 +141,8 @@ public class WriteLoops {
124 141
             i = i - 1;
125 142
         } while (i > 0);
126 143
         // what's the primary difference between them?!?
144
+        // while loop -> loops from 0 to 5 and increments up by 1; may not execute at all if condition isn't initially met (e.g. if i = 6)
145
+        // do-while loop -> loops from 8 to 1 and increments down by 1; executes at least once because the "do" is before the "while" (performs task before checking condition)
127 146
     }
128 147
 
129 148
     // Write a WHILE loop that checks “gpsCurrentLocation()”
@@ -131,15 +150,15 @@ public class WriteLoops {
131 150
     // After the loop is done, print “Honey, I’m Home!”
132 151
     public int driveHome() {
133 152
         int w = 0;
134
-
135 153
         // you need to use a .equals for two Strings.
136
-
137
-            // calling
138
-            w = w + 1;
139
-            // each time through the inner loop
140
-        
141
-
142
-            return w;
154
+        while (gpsCurrentLocation().equals("Home") != true) {
155
+            driveSomeMore();
156
+        }
157
+        System.out.println("Honey, I'm Home!");
158
+        // calling
159
+        w = w + 1;
160
+        // each time through the inner loop
161
+        return w;
143 162
     }
144 163
 
145 164
     // Getting harder...
@@ -155,11 +174,11 @@ public class WriteLoops {
155 174
         int runningScore = 0;
156 175
 
157 176
         // do your while loop here
158
- 
159
-            // calling
160
-            w = w + 1;
161
-            // each time through the inner loop
162
-        
177
+
178
+        // calling
179
+        w = w + 1;
180
+        // each time through the inner loop
181
+
163 182
         return w; // >= 3;
164 183
     }
165 184
 
@@ -173,9 +192,9 @@ public class WriteLoops {
173 192
 
174 193
         // do your while loop here
175 194
 
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
195
+        // calling
196
+        w = w + 1;
197
+        // each time through the inner loop
179 198
 
180 199
         return w >= 3;
181 200
     }
@@ -187,12 +206,11 @@ public class WriteLoops {
187 206
     public int checkServerStatus() {
188 207
         int w = 0;
189 208
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191 209
 
192 210
         // calling
193 211
         w = w + 1;
194 212
         // each time through the inner loop
195
-        
213
+
196 214
         return w;
197 215
     }
198 216
 
@@ -202,11 +220,10 @@ public class WriteLoops {
202 220
     public int loop50by7() {
203 221
         int w = 0;
204 222
 
223
+        // calling
224
+        w = w + 1;
225
+        // each time through the inner loop
205 226
 
206
-            // calling
207
-            w = w + 1;
208
-            // each time through the inner loop
209
-        
210 227
         return w;
211 228
     }
212 229
 
@@ -239,11 +256,10 @@ public class WriteLoops {
239 256
         int w = 0;
240 257
         int sumOfThrees = 0;
241 258
 
242
- 
243
-            // calling
244
-            w = w + 1;
245
-            // each time through the inner loop
246
-        
259
+        // calling
260
+        w = w + 1;
261
+        // each time through the inner loop
262
+
247 263
         System.out.print("The Sum is ");
248 264
         System.out.println(sumOfThrees);
249 265
 
@@ -256,11 +272,10 @@ public class WriteLoops {
256 272
         int w = 0;
257 273
         int sumOfThrees = 0;
258 274
 
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
263
-        
275
+        // calling
276
+        w = w + 1;
277
+        // each time through the inner loop
278
+
264 279
         System.out.print("The Sum is ");
265 280
         System.out.println(sumOfThrees);
266 281
 
@@ -279,11 +294,11 @@ public class WriteLoops {
279 294
         boolean onTime = true;
280 295
 
281 296
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
286
-        
297
+
298
+        // be sure to call
299
+        w = w + 1;
300
+        // each time inside the loop
301
+
287 302
         return w;
288 303
     }
289 304
 
@@ -296,11 +311,10 @@ public class WriteLoops {
296 311
         int w = 0;
297 312
         int numberOfVotes = voteTallies.length;
298 313
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
-        
314
+        // calling
315
+        w = w + 1;
316
+        // each time through the inner loop
317
+
304 318
         return w;
305 319
     }
306 320
 
@@ -311,11 +325,10 @@ public class WriteLoops {
311 325
         int w = 0;
312 326
         int numberOfVotes = voteTallies.length;
313 327
 
328
+        // calling
329
+        w = w + 1;
330
+        // each time through the inner loop
314 331
 
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
319 332
         return w;
320 333
     }
321 334
 
@@ -378,14 +391,15 @@ public class WriteLoops {
378 391
     //         return (i >= 3);
379 392
     //     };
380 393
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
394
+    private int summer = 0;
395
+    private boolean isSummer() {
396
+        if (summer == 3) {
397
+            return true;
388 398
         }
399
+        summer++;
400
+        return false;
401
+    }
402
+
389 403
     private void sendEmergencyText(String mesg, String phone) {
390 404
     }
391 405
 

+ 9
- 9
package.bluej Vedi File

@@ -9,16 +9,16 @@ editor.fx.0.height=722
9 9
 editor.fx.0.width=800
10 10
 editor.fx.0.x=560
11 11
 editor.fx.0.y=118
12
-objectbench.height=101
13
-objectbench.width=740
12
+objectbench.height=111
13
+objectbench.width=577
14 14
 package.divider.horizontal=0.6
15
-package.divider.vertical=0.8625954198473282
16
-package.editor.height=671
17
-package.editor.width=1139
18
-package.editor.x=112
19
-package.editor.y=89
20
-package.frame.height=844
21
-package.frame.width=1265
15
+package.divider.vertical=0.7951388888888888
16
+package.editor.height=451
17
+package.editor.width=459
18
+package.editor.x=1258
19
+package.editor.y=266
20
+package.frame.height=634
21
+package.frame.width=601
22 22
 package.numDependencies=2
23 23
 package.numTargets=4
24 24
 package.showExtends=true