Yesoda Sanka 6 anni fa
parent
commit
c583132ef3
2 ha cambiato i file con 162 aggiunte e 82 eliminazioni
  1. 154
    74
      WriteLoops.java
  2. 8
    8
      package.bluej

+ 154
- 74
WriteLoops.java Vedi File

@@ -16,14 +16,17 @@ public class WriteLoops {
16 16
     private static final int _3 = 3;
17 17
 
18 18
     public int oneToFive() {
19
-        int w = 0;
20
-
19
+        int w=0;
21 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 28
         // this will tell the test how many times the loop executed.
29
+
27 30
         return w;
28 31
     }
29 32
 
@@ -32,9 +35,12 @@ public class WriteLoops {
32 35
 
33 36
         // Write a FOR loop that counts from 1 to 10.
34 37
         // calling
35
-        w = w + 1;
38
+        for(int i=0;i<10;i++)
39
+        {
40
+            w = w + 1;
41
+        }
36 42
         // each time through the loop
37
-        
43
+
38 44
         return w;
39 45
     }
40 46
 
@@ -43,9 +49,11 @@ public class WriteLoops {
43 49
 
44 50
         // Write a FOR loop that makes 10 iterations, start at 21.
45 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 57
         return w;
50 58
     }
51 59
 
@@ -54,9 +62,11 @@ public class WriteLoops {
54 62
 
55 63
         // Write a FOR loop that counts down from 100 to 0.
56 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 70
         return w;
61 71
     }
62 72
 
@@ -65,8 +75,10 @@ public class WriteLoops {
65 75
 
66 76
         // Write a FOR loop from 0 to 32 by 2s.
67 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 82
         return w;
71 83
     }
72 84
 
@@ -75,9 +87,10 @@ public class WriteLoops {
75 87
 
76 88
         // Write a FOR loop from 1 to less than 5001 by 11s.
77 89
         // calling
78
-        w = w + 1;
90
+        for(int i=1;i<5001;i=+11)
91
+            w = w + 1;
79 92
         // each time through the loop
80
-        
93
+
81 94
         return w;
82 95
     }
83 96
 
@@ -86,10 +99,16 @@ public class WriteLoops {
86 99
 
87 100
         // Write a nested FOR loop(s), where one counts from
88 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 107
                 w = w + 1;
91
-                // each time through the inner loop
108
+            }
109
+        }
92 110
 
111
+        // each time through the inner loop
93 112
         return w;
94 113
     }
95 114
 
@@ -100,11 +119,14 @@ public class WriteLoops {
100 119
         // statement inside the loop that checks the
101 120
         // loop index counter and if it’s greater than 51,
102 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 126
                 w = w + 1;
106
-            // each time through the inner loop
107
-        
127
+            }
128
+        }
129
+
108 130
         return w;
109 131
     }
110 132
 
@@ -124,6 +146,8 @@ public class WriteLoops {
124 146
             i = i - 1;
125 147
         } while (i > 0);
126 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 153
     // Write a WHILE loop that checks “gpsCurrentLocation()”
@@ -131,15 +155,17 @@ public class WriteLoops {
131 155
     // After the loop is done, print “Honey, I’m Home!”
132 156
     public int driveHome() {
133 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 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 171
     // Getting harder...
@@ -153,18 +179,24 @@ public class WriteLoops {
153 179
         int highestScore = 236;
154 180
         int currentScore = gameNextScore();
155 181
         int runningScore = 0;
182
+        while(runningScore<highestScore)
183
+        {
184
+            runningScore=runningScore+currentScore;
185
+            w=w+1;
186
+        }
156 187
 
157 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 194
         return w; // >= 3;
164 195
     }
165 196
 
166 197
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
167 198
     // Notice how the “runningScore” variable usage is different.
199
+    /* runningScore  increment first*/
168 200
     public boolean checkGameScoreDoWhile() {
169 201
         int w = 0;
170 202
         int highestScore = 236;
@@ -173,9 +205,11 @@ public class WriteLoops {
173 205
 
174 206
         // do your while loop here
175 207
 
176
-            // calling
208
+        // calling
209
+        do {
210
+            runningScore=runningScore+currentScore;
177 211
             w = w + 1;
178
-            // each time through the inner loop
212
+        } while(runningScore<highestScore);
179 213
 
180 214
         return w >= 3;
181 215
     }
@@ -187,12 +221,21 @@ public class WriteLoops {
187 221
     public int checkServerStatus() {
188 222
         int w = 0;
189 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 235
         // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
195 236
         
237
+        // each time through the inner loop
238
+
196 239
         return w;
197 240
     }
198 241
 
@@ -201,12 +244,16 @@ public class WriteLoops {
201 244
     // and if it is, add 7 to “i”
202 245
     public int loop50by7() {
203 246
         int w = 0;
204
-
205
-
206
-            // calling
247
+        int i=7;
248
+        while(i<50)
249
+        {i+=7;
207 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 257
         return w;
211 258
     }
212 259
 
@@ -216,7 +263,9 @@ public class WriteLoops {
216 263
     // out the sum of them all.
217 264
     public int foo() {
218 265
         int w = 0;
266
+        
219 267
         // this is an array of ints. it is of length 7 (from 0 -> 6)
268
+       
220 269
         int sumOfThrees = 0;
221 270
 
222 271
         // this is a so called Enhanced for loop
@@ -238,29 +287,36 @@ public class WriteLoops {
238 287
     public int rewriteFooAsFor() {
239 288
         int w = 0;
240 289
         int sumOfThrees = 0;
241
-
242
- 
243
-            // calling
290
+        int i=0;
291
+        for(i=0;i<=threes_array.length;i++)
292
+        { 
244 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 299
         System.out.print("The Sum is ");
248 300
         System.out.println(sumOfThrees);
249 301
 
250 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 306
     // rewrite the loop inside of "foo()" as a 'while' loop
255 307
     public int rewriteFooAsWhile() {
256 308
         int w = 0;
257 309
         int sumOfThrees = 0;
258
-
259
- 
260
-            // calling
310
+        int i=0;
311
+        while(i<=threes_array.length)
312
+        { i++;
261 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 320
         System.out.print("The Sum is ");
265 321
         System.out.println(sumOfThrees);
266 322
 
@@ -277,13 +333,26 @@ public class WriteLoops {
277 333
     public int manageYardAndJunior() {
278 334
         int w = 0;
279 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 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 356
         return w;
288 357
     }
289 358
 
@@ -295,12 +364,17 @@ public class WriteLoops {
295 364
     public int tallyVote1() {
296 365
         int w = 0;
297 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 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 378
         return w;
305 379
     }
306 380
 
@@ -310,12 +384,17 @@ public class WriteLoops {
310 384
     public int tallyVote2() {
311 385
         int w = 0;
312 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 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 398
         return w;
320 399
     }
321 400
 
@@ -378,14 +457,15 @@ public class WriteLoops {
378 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 469
     private void sendEmergencyText(String mesg, String phone) {
390 470
     }
391 471
 

+ 8
- 8
package.bluej Vedi File

@@ -1,14 +1,14 @@
1 1
 #BlueJ package file
2
-dependency1.from=WriteLoopsTest
3
-dependency1.to=WriteLoops
2
+dependency1.from=WriteIFsTest
3
+dependency1.to=WriteIFs
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=WriteIFsTest
6
-dependency2.to=WriteIFs
5
+dependency2.from=WriteLoopsTest
6
+dependency2.to=WriteLoops
7 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 12
 objectbench.height=83
13 13
 objectbench.width=740
14 14
 package.divider.horizontal=0.6