Browse Source

modified loops

Yesoda Sanka 6 years ago
parent
commit
d34bd8f7ab
4 changed files with 37 additions and 29 deletions
  1. BIN
      .DS_Store
  2. 31
    23
      WriteLoops.java
  3. 1
    1
      WriteLoopsTest.java
  4. 5
    5
      package.bluej

BIN
.DS_Store View File


+ 31
- 23
WriteLoops.java View File

14
 public class WriteLoops {
14
 public class WriteLoops {
15
 
15
 
16
     private static final int _3 = 3;
16
     private static final int _3 = 3;
17
-
17
+    int serverStatus;
18
+    private int gps;
19
+    
18
     public int oneToFive() {
20
     public int oneToFive() {
19
         int w=0;
21
         int w=0;
20
         // Write a FOR loop that counts from 1 to 10.
22
         // Write a FOR loop that counts from 1 to 10.
75
 
77
 
76
         // Write a FOR loop from 0 to 32 by 2s.
78
         // Write a FOR loop from 0 to 32 by 2s.
77
         // calling
79
         // calling
78
-        for(int i=0;i<=32;i=+2)
80
+        for(int i=0;i<32;i=i+2)
79
         {
81
         {
80
-            w = w + 1;
82
+            System.out.println ("byTwoto32 i = " + i);
83
+            w++;
81
         }// each time through the loop
84
         }// each time through the loop
82
         return w;
85
         return w;
83
     }
86
     }
87
 
90
 
88
         // Write a FOR loop from 1 to less than 5001 by 11s.
91
         // Write a FOR loop from 1 to less than 5001 by 11s.
89
         // calling
92
         // calling
90
-        for(int i=1;i<5001;i=+11)
93
+        for(int i=1;i<5001;i=i+11)
91
             w = w + 1;
94
             w = w + 1;
92
         // each time through the loop
95
         // each time through the loop
93
 
96
 
155
     // After the loop is done, print “Honey, I’m Home!”
158
     // After the loop is done, print “Honey, I’m Home!”
156
     public int driveHome() {
159
     public int driveHome() {
157
         int w = 0;
160
         int w = 0;
158
-        String current=gpsCurrentLocation();
159
-        while(!current.equals("home"))
161
+         
162
+        while(!gpsCurrentLocation().equals("Home"))
160
         {
163
         {
161
             this.driveSomeMore();
164
             this.driveSomeMore();
165
+            w=w+1;
162
         } 
166
         } 
163
         System.out.println ("Honey, I'm Home!");
167
         System.out.println ("Honey, I'm Home!");
164
 
168
 
223
     public int checkServerStatus() {
227
     public int checkServerStatus() {
224
         int w = 0;
228
         int w = 0;
225
         String adminPhoneNumber = "+1 202 456 1111";
229
         String adminPhoneNumber = "+1 202 456 1111";
226
-        boolean result=serverIsRunning();
227
-        while(result == true)
230
+        
231
+        while(this.serverIsRunning())
228
         {
232
         {
229
             waitFor(5);
233
             waitFor(5);
234
+            w=w+1;
230
         }
235
         }
231
-        if(result==false)
236
+        if(!this.serverIsRunning())
232
         {
237
         {
233
-            sendEmergencyText("Help","adminPhoneNumber");
234
-            tryServerRestart("","");
235
-            w=w+1;
238
+            sendEmergencyText("Help",adminPhoneNumber);
239
+            tryServerRestart("Help", adminPhoneNumber);
240
+            
236
         }
241
         }
237
         // calling
242
         // calling
238
         
243
         
252
             w = w + 1;
257
             w = w + 1;
253
         }
258
         }
254
         
259
         
255
-        // calling
256
         
260
         
257
-        // each time through the inner loop
258
-
259
         return w;
261
         return w;
260
     }
262
     }
261
 
263
 
283
         return w;
285
         return w;
284
     }
286
     }
285
 
287
 
286
-    // Ponder this: can all FOR loops be rewritten as WHILE loops?
288
+    // Ponder this: can all FOR loops be rewritten as WHILE loops? yes
287
     // rewrite the loop inside of "foo()" as a standard for loop
289
     // rewrite the loop inside of "foo()" as a standard for loop
288
     // with 'i' as its index variable.
290
     // with 'i' as its index variable.
289
     public int rewriteFooAsFor() {
291
     public int rewriteFooAsFor() {
336
         int w = 0;
338
         int w = 0;
337
         boolean onTime = true;
339
         boolean onTime = true;
338
         boolean yardNeedsMowed=true;
340
         boolean yardNeedsMowed=true;
339
-        while(isSummer())
341
+        while(!isSummer())
340
         {
342
         {
343
+            w = w + 1;
341
            if(yardNeedsMowed==true)
344
            if(yardNeedsMowed==true)
342
            {
345
            {
343
                yellAtJuniorToMowLawn();
346
                yellAtJuniorToMowLawn();
344
-               w = w + 1;
347
+               
345
             }else
348
             }else
346
             { 
349
             { 
347
-                sendJuniorBackToSchool("");
350
+                sendJuniorBackToSchool("First Day of School");
348
             }
351
             }
349
         }
352
         }
350
         
353
         
422
     public WriteLoops() {
425
     public WriteLoops() {
423
         // initialise instance variables
426
         // initialise instance variables
424
         x = 0;
427
         x = 0;
428
+        gps = 0;
429
+        serverStatus = 5;
425
     }
430
     }
426
 
431
 
427
-    private int gps = 0;
432
+    
428
 
433
 
429
     private String gpsCurrentLocation() {
434
     private String gpsCurrentLocation() {
430
         if (this.gps > 5) {
435
         if (this.gps > 5) {
474
     private void tryServerRestart(String mesg, String phone) {
479
     private void tryServerRestart(String mesg, String phone) {
475
     }
480
     }
476
 
481
 
477
-    int serverStatus = 5;
482
+    
478
 
483
 
479
     private boolean serverIsRunning() {
484
     private boolean serverIsRunning() {
480
-        return (serverStatus < 20);
485
+        //System.out.println ("ServerIsRunning - serverStatus - " + serverStatus );
486
+        return (this.serverStatus < 20);
481
     }
487
     }
482
 
488
 
483
     private void waitFor(int interval) {
489
     private void waitFor(int interval) {
484
-        serverStatus += interval;
490
+        //System.out.println ("waitfor before serverStatus - " + this.serverStatus);
491
+        this.serverStatus += interval;
492
+        //System.out.println ("waitfor after serverStatus - " + this.serverStatus);
485
     }
493
     }
486
 
494
 
487
 }
495
 }

+ 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(16, writeLoo1.byTwoTo32());
75
     }
75
     }
76
 
76
 
77
     @Test
77
     @Test

+ 5
- 5
package.bluej View File

10
 editor.fx.0.x=0
10
 editor.fx.0.x=0
11
 editor.fx.0.y=23
11
 editor.fx.0.y=23
12
 objectbench.height=83
12
 objectbench.height=83
13
-objectbench.width=740
14
-package.divider.horizontal=0.6
13
+objectbench.width=474
14
+package.divider.horizontal=0.6004993757802747
15
 package.divider.vertical=0.8617511520737328
15
 package.divider.vertical=0.8617511520737328
16
 package.editor.height=554
16
 package.editor.height=554
17
-package.editor.width=1139
18
-package.editor.x=15
17
+package.editor.width=695
18
+package.editor.x=390
19
 package.editor.y=23
19
 package.editor.y=23
20
 package.frame.height=709
20
 package.frame.height=709
21
-package.frame.width=1265
21
+package.frame.width=821
22
 package.numDependencies=2
22
 package.numDependencies=2
23
 package.numTargets=4
23
 package.numTargets=4
24
 package.showExtends=true
24
 package.showExtends=true