Browse Source

writeLoops

Yesoda Sanka 6 years ago
parent
commit
c583132ef3
2 changed files with 162 additions and 82 deletions
  1. 154
    74
      WriteLoops.java
  2. 8
    8
      package.bluej

+ 154
- 74
WriteLoops.java View File

16
     private static final int _3 = 3;
16
     private static final int _3 = 3;
17
 
17
 
18
     public int oneToFive() {
18
     public int oneToFive() {
19
-        int w = 0;
20
-
19
+        int w=0;
21
         // Write a FOR loop that counts from 1 to 10.
20
         // Write a FOR loop that counts from 1 to 10.
22
-            // calling
23
-            w = w + 1;
24
-            // each time through the loop
21
+        // calling
22
+        for(int i=0;i<=5;i++)
23
+        {
24
+            w=i;
25
+        }
26
+        // each time through the loop
25
 
27
 
26
         // this will tell the test how many times the loop executed.
28
         // this will tell the test how many times the loop executed.
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;
38
+        for(int i=0;i<10;i++)
39
+        {
40
+            w = w + 1;
41
+        }
36
         // each time through the loop
42
         // each time through the loop
37
-        
43
+
38
         return w;
44
         return w;
39
     }
45
     }
40
 
46
 
43
 
49
 
44
         // Write a FOR loop that makes 10 iterations, start at 21.
50
         // Write a FOR loop that makes 10 iterations, start at 21.
45
         // calling
51
         // calling
46
-        w = w + 1;
47
-        // each time through the loop
48
-        
52
+        for(int i=21;i<32;i++)
53
+        {
54
+            w = w + 1;
55
+        }// each time through the loop
56
+
49
         return w;
57
         return w;
50
     }
58
     }
51
 
59
 
54
 
62
 
55
         // Write a FOR loop that counts down from 100 to 0.
63
         // Write a FOR loop that counts down from 100 to 0.
56
         // calling
64
         // calling
57
-        w = w + 1;
58
-        // each time through the loop
59
-        
65
+        for(int i=100;i>0;i--)
66
+        {
67
+            w = w + 1;
68
+            // each time through the loop
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
+        {
80
+            w = w + 1;
81
+        }// each time through the loop
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
+        {
105
+            for(int j=0;j<=4;j++)
106
+            {
90
                 w = w + 1;
107
                 w = w + 1;
91
-                // each time through the inner loop
108
+            }
109
+        }
92
 
110
 
111
+        // each time through the inner loop
93
         return w;
112
         return w;
94
     }
113
     }
95
 
114
 
100
         // statement inside the loop that checks the
119
         // statement inside the loop that checks the
101
         // loop index counter and if it’s greater than 51,
120
         // loop index counter and if it’s greater than 51,
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
121
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103
-
104
-                // calling
122
+        for(int i=5;i<=105;i++)
123
+        { if(i>58)
124
+            {
125
+                System.out.println("Hello Zipocode");
105
                 w = w + 1;
126
                 w = w + 1;
106
-            // each time through the inner loop
107
-        
127
+            }
128
+        }
129
+
108
         return w;
130
         return w;
109
     }
131
     }
110
 
132
 
124
             i = i - 1;
146
             i = i - 1;
125
         } while (i > 0);
147
         } while (i > 0);
126
         // what's the primary difference between them?!?
148
         // what's the primary difference between them?!?
149
+        /*  while loop will excute only the condition is ture ,
150
+         * but do while will excute atleast once even though if the condition is false */
127
     }
151
     }
128
 
152
 
129
     // Write a WHILE loop that checks “gpsCurrentLocation()”
153
     // Write a WHILE loop that checks “gpsCurrentLocation()”
131
     // After the loop is done, print “Honey, I’m Home!”
155
     // After the loop is done, print “Honey, I’m Home!”
132
     public int driveHome() {
156
     public int driveHome() {
133
         int w = 0;
157
         int w = 0;
158
+        String current=gpsCurrentLocation();
159
+        while(!current.equals("home"))
160
+        {
161
+            this.driveSomeMore();
162
+        } 
163
+        System.out.println ("Honey, I'm Home!");
134
 
164
 
135
         // you need to use a .equals for two Strings.
165
         // 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;
166
+        // calling
167
+        // each time through the inner loop
168
+        return w;
143
     }
169
     }
144
 
170
 
145
     // Getting harder...
171
     // Getting harder...
153
         int highestScore = 236;
179
         int highestScore = 236;
154
         int currentScore = gameNextScore();
180
         int currentScore = gameNextScore();
155
         int runningScore = 0;
181
         int runningScore = 0;
182
+        while(runningScore<highestScore)
183
+        {
184
+            runningScore=runningScore+currentScore;
185
+            w=w+1;
186
+        }
156
 
187
 
157
         // do your while loop here
188
         // do your while loop here
158
- 
159
-            // calling
160
-            w = w + 1;
161
-            // each time through the inner loop
162
-        
189
+
190
+        // calling
191
+
192
+        // each time through the inner loop
193
+
163
         return w; // >= 3;
194
         return w; // >= 3;
164
     }
195
     }
165
 
196
 
166
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
197
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
167
     // Notice how the “runningScore” variable usage is different.
198
     // Notice how the “runningScore” variable usage is different.
199
+    /* runningScore  increment first*/
168
     public boolean checkGameScoreDoWhile() {
200
     public boolean checkGameScoreDoWhile() {
169
         int w = 0;
201
         int w = 0;
170
         int highestScore = 236;
202
         int highestScore = 236;
173
 
205
 
174
         // do your while loop here
206
         // do your while loop here
175
 
207
 
176
-            // calling
208
+        // calling
209
+        do {
210
+            runningScore=runningScore+currentScore;
177
             w = w + 1;
211
             w = w + 1;
178
-            // each time through the inner loop
212
+        } while(runningScore<highestScore);
179
 
213
 
180
         return w >= 3;
214
         return w >= 3;
181
     }
215
     }
187
     public int checkServerStatus() {
221
     public int checkServerStatus() {
188
         int w = 0;
222
         int w = 0;
189
         String adminPhoneNumber = "+1 202 456 1111";
223
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191
-
224
+        boolean result=serverIsRunning();
225
+        while(result == true)
226
+        {
227
+            waitFor(5);
228
+        }
229
+        if(result==false)
230
+        {
231
+            sendEmergencyText("help","adminPhoneNumber");
232
+            tryServerRestart("","");
233
+            w=w+1;
234
+        }
192
         // calling
235
         // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
195
         
236
         
237
+        // each time through the inner loop
238
+
196
         return w;
239
         return w;
197
     }
240
     }
198
 
241
 
201
     // and if it is, add 7 to “i”
244
     // and if it is, add 7 to “i”
202
     public int loop50by7() {
245
     public int loop50by7() {
203
         int w = 0;
246
         int w = 0;
204
-
205
-
206
-            // calling
247
+        int i=7;
248
+        while(i<50)
249
+        {i+=7;
207
             w = w + 1;
250
             w = w + 1;
208
-            // each time through the inner loop
251
+        }
252
+        
253
+        // calling
209
         
254
         
255
+        // each time through the inner loop
256
+
210
         return w;
257
         return w;
211
     }
258
     }
212
 
259
 
216
     // out the sum of them all.
263
     // out the sum of them all.
217
     public int foo() {
264
     public int foo() {
218
         int w = 0;
265
         int w = 0;
266
+        
219
         // this is an array of ints. it is of length 7 (from 0 -> 6)
267
         // this is an array of ints. it is of length 7 (from 0 -> 6)
268
+       
220
         int sumOfThrees = 0;
269
         int sumOfThrees = 0;
221
 
270
 
222
         // this is a so called Enhanced for loop
271
         // this is a so called Enhanced for loop
238
     public int rewriteFooAsFor() {
287
     public int rewriteFooAsFor() {
239
         int w = 0;
288
         int w = 0;
240
         int sumOfThrees = 0;
289
         int sumOfThrees = 0;
241
-
242
- 
243
-            // calling
290
+        int i=0;
291
+        for(i=0;i<=threes_array.length;i++)
292
+        { 
244
             w = w + 1;
293
             w = w + 1;
245
-            // each time through the inner loop
246
-        
294
+        }
295
+        // calling
296
+       
297
+        // each time through the inner loop
298
+
247
         System.out.print("The Sum is ");
299
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
300
         System.out.println(sumOfThrees);
249
 
301
 
250
         return w;
302
         return w;
251
     }
303
     }
252
 
304
 
253
-    // Ponder this: can all WHILE loops be rewritten as FOR loops?
305
+    // Ponder this: can all WHILE loops be rewritten as FOR loops? yes
254
     // rewrite the loop inside of "foo()" as a 'while' loop
306
     // rewrite the loop inside of "foo()" as a 'while' loop
255
     public int rewriteFooAsWhile() {
307
     public int rewriteFooAsWhile() {
256
         int w = 0;
308
         int w = 0;
257
         int sumOfThrees = 0;
309
         int sumOfThrees = 0;
258
-
259
- 
260
-            // calling
310
+        int i=0;
311
+        while(i<=threes_array.length)
312
+        { i++;
261
             w = w + 1;
313
             w = w + 1;
262
-            // each time through the inner loop
314
+        }
263
         
315
         
316
+        // calling
317
+        
318
+        // each time through the inner loop
319
+
264
         System.out.print("The Sum is ");
320
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
321
         System.out.println(sumOfThrees);
266
 
322
 
277
     public int manageYardAndJunior() {
333
     public int manageYardAndJunior() {
278
         int w = 0;
334
         int w = 0;
279
         boolean onTime = true;
335
         boolean onTime = true;
336
+        boolean yardNeedsMowed=true;
337
+        while(isSummer())
338
+        {
339
+           if(yardNeedsMowed==true)
340
+           {
341
+               yellAtJuniorToMowLawn();
342
+               w = w + 1;
343
+            }else
344
+            { 
345
+                sendJuniorBackToSchool("");
346
+            }
347
+        }
348
+        
280
 
349
 
281
         // ADD YOUR CODE here.
350
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
351
+
352
+        // be sure to call
286
         
353
         
354
+        // each time inside the loop
355
+
287
         return w;
356
         return w;
288
     }
357
     }
289
 
358
 
295
     public int tallyVote1() {
364
     public int tallyVote1() {
296
         int w = 0;
365
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
366
         int numberOfVotes = voteTallies.length;
298
-
299
- 
300
-            // calling
367
+        int i;
368
+        for(i=0;i<=numberOfVotes;i++)
369
+        {
370
+            System.out.println(voteTallies[i]);
301
             w = w + 1;
371
             w = w + 1;
302
-            // each time through the inner loop
372
+        }
303
         
373
         
374
+        // calling
375
+        
376
+        // each time through the inner loop
377
+
304
         return w;
378
         return w;
305
     }
379
     }
306
 
380
 
310
     public int tallyVote2() {
384
     public int tallyVote2() {
311
         int w = 0;
385
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
386
         int numberOfVotes = voteTallies.length;
313
-
314
-
315
-            // calling
387
+        int i=0;
388
+        while(i<numberOfVotes)
389
+        {
390
+            System.out.println(voteTallies[i]);
391
+            i++;
316
             w = w + 1;
392
             w = w + 1;
317
-            // each time through the inner loop
393
+        }
394
+        // calling
318
         
395
         
396
+        // each time through the inner loop
397
+
319
         return w;
398
         return w;
320
     }
399
     }
321
 
400
 
378
     //         return (i >= 3);
457
     //         return (i >= 3);
379
     //     };
458
     //     };
380
     // };
459
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
460
+    private int summer = 0;
461
+    private boolean isSummer() {
462
+        if (summer == 3) {
463
+            return true;
388
         }
464
         }
465
+        summer++;
466
+        return false;
467
+    }
468
+
389
     private void sendEmergencyText(String mesg, String phone) {
469
     private void sendEmergencyText(String mesg, String phone) {
390
     }
470
     }
391
 
471
 

+ 8
- 8
package.bluej View File

1
 #BlueJ package file
1
 #BlueJ package file
2
-dependency1.from=WriteLoopsTest
3
-dependency1.to=WriteLoops
2
+dependency1.from=WriteIFsTest
3
+dependency1.to=WriteIFs
4
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
-dependency2.from=WriteIFsTest
6
-dependency2.to=WriteIFs
5
+dependency2.from=WriteLoopsTest
6
+dependency2.to=WriteLoops
7
 dependency2.type=UsesDependency
7
 dependency2.type=UsesDependency
8
-editor.fx.0.height=0
9
-editor.fx.0.width=0
10
-editor.fx.0.x=0
11
-editor.fx.0.y=0
8
+editor.fx.0.height=709
9
+editor.fx.0.width=688
10
+editor.fx.0.x=464
11
+editor.fx.0.y=23
12
 objectbench.height=83
12
 objectbench.height=83
13
 objectbench.width=740
13
 objectbench.width=740
14
 package.divider.horizontal=0.6
14
 package.divider.horizontal=0.6