Browse Source

tests passed up to driveHome()

Jennifer Chao 6 years ago
parent
commit
7cac89488c
2 changed files with 95 additions and 81 deletions
  1. 86
    72
      WriteLoops.java
  2. 9
    9
      package.bluej

+ 86
- 72
WriteLoops.java View 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 <= 5; 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.
27
         return w;
28
         return w;
28
     }
29
     }
32
 
33
 
33
         // Write a FOR loop that counts from 1 to 10.
34
         // Write a FOR loop that counts from 1 to 10.
34
         // calling
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
         return w;
40
         return w;
39
     }
41
     }
40
 
42
 
43
 
45
 
44
         // Write a FOR loop that makes 10 iterations, start at 21.
46
         // Write a FOR loop that makes 10 iterations, start at 21.
45
         // calling
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
         return w;
52
         return w;
50
     }
53
     }
51
 
54
 
54
 
57
 
55
         // Write a FOR loop that counts down from 100 to 0.
58
         // Write a FOR loop that counts down from 100 to 0.
56
         // calling
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
         return w;
64
         return w;
61
     }
65
     }
62
 
66
 
65
 
69
 
66
         // Write a FOR loop from 0 to 32 by 2s.
70
         // Write a FOR loop from 0 to 32 by 2s.
67
         // calling
71
         // calling
68
-        w = w + 1;
72
+        for (int i = 0; i <= 32; i += 2) {
73
+            w = w + 1;
74
+        }
69
         // each time through the loop
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
     public int countDownFrom5000() {
79
     public int countDownFrom5000() {
75
 
81
 
76
         // Write a FOR loop from 1 to less than 5001 by 11s.
82
         // Write a FOR loop from 1 to less than 5001 by 11s.
77
         // calling
83
         // calling
78
-        w = w + 1;
84
+        for (int i = 1; i < 5001; i += 11) {
85
+            w = w + 1;
86
+        }
79
         // each time through the loop
87
         // each time through the loop
80
-        
88
+
81
         return w;
89
         return w;
82
     }
90
     }
83
 
91
 
86
 
94
 
87
         // Write a nested FOR loop(s), where one counts from
95
         // 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
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
                 w = w + 1;
100
                 w = w + 1;
91
-                // each time through the inner loop
101
+            }
102
+        }
103
+        // each time through the inner loop
92
 
104
 
93
         return w;
105
         return w;
94
     }
106
     }
101
         // loop index counter and if it’s greater than 51,
113
         // loop index counter and if it’s greater than 51,
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
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
                 w = w + 1;
121
                 w = w + 1;
122
+            }
106
             // each time through the inner loop
123
             // each time through the inner loop
107
-        
124
+        }
108
         return w;
125
         return w;
109
     }
126
     }
110
 
127
 
124
             i = i - 1;
141
             i = i - 1;
125
         } while (i > 0);
142
         } while (i > 0);
126
         // what's the primary difference between them?!?
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
     // Write a WHILE loop that checks “gpsCurrentLocation()”
148
     // Write a WHILE loop that checks “gpsCurrentLocation()”
131
     // After the loop is done, print “Honey, I’m Home!”
150
     // After the loop is done, print “Honey, I’m Home!”
132
     public int driveHome() {
151
     public int driveHome() {
133
         int w = 0;
152
         int w = 0;
134
-
135
         // you need to use a .equals for two Strings.
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
     // Getting harder...
164
     // Getting harder...
155
         int runningScore = 0;
174
         int runningScore = 0;
156
 
175
 
157
         // do your while loop here
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
         return w; // >= 3;
182
         return w; // >= 3;
164
     }
183
     }
165
 
184
 
173
 
192
 
174
         // do your while loop here
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
         return w >= 3;
199
         return w >= 3;
181
     }
200
     }
187
     public int checkServerStatus() {
206
     public int checkServerStatus() {
188
         int w = 0;
207
         int w = 0;
189
         String adminPhoneNumber = "+1 202 456 1111";
208
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191
 
209
 
192
         // calling
210
         // calling
193
         w = w + 1;
211
         w = w + 1;
194
         // each time through the inner loop
212
         // each time through the inner loop
195
-        
213
+
196
         return w;
214
         return w;
197
     }
215
     }
198
 
216
 
202
     public int loop50by7() {
220
     public int loop50by7() {
203
         int w = 0;
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
         return w;
227
         return w;
211
     }
228
     }
212
 
229
 
239
         int w = 0;
256
         int w = 0;
240
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
263
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
264
         System.out.println(sumOfThrees);
249
 
265
 
256
         int w = 0;
272
         int w = 0;
257
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
279
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
280
         System.out.println(sumOfThrees);
266
 
281
 
279
         boolean onTime = true;
294
         boolean onTime = true;
280
 
295
 
281
         // ADD YOUR CODE here.
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
         return w;
302
         return w;
288
     }
303
     }
289
 
304
 
296
         int w = 0;
311
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
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
         return w;
318
         return w;
305
     }
319
     }
306
 
320
 
311
         int w = 0;
325
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
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
         return w;
332
         return w;
320
     }
333
     }
321
 
334
 
378
     //         return (i >= 3);
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
     private void sendEmergencyText(String mesg, String phone) {
403
     private void sendEmergencyText(String mesg, String phone) {
390
     }
404
     }
391
 
405
 

+ 9
- 9
package.bluej View File

9
 editor.fx.0.width=800
9
 editor.fx.0.width=800
10
 editor.fx.0.x=560
10
 editor.fx.0.x=560
11
 editor.fx.0.y=118
11
 editor.fx.0.y=118
12
-objectbench.height=101
13
-objectbench.width=740
12
+objectbench.height=111
13
+objectbench.width=577
14
 package.divider.horizontal=0.6
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
 package.numDependencies=2
22
 package.numDependencies=2
23
 package.numTargets=4
23
 package.numTargets=4
24
 package.showExtends=true
24
 package.showExtends=true