Pārlūkot izejas kodu

loops and ifs completed

Jacob Andersen 6 gadus atpakaļ
vecāks
revīzija
ec06e37b80
4 mainītis faili ar 109 papildinājumiem un 46 dzēšanām
  1. 32
    19
      WriteIFs.java
  2. 61
    11
      WriteLoops.java
  3. 1
    1
      WriteLoopsTest.java
  4. 15
    15
      package.bluej

+ 32
- 19
WriteIFs.java Parādīt failu

@@ -6,18 +6,39 @@
6 6
  * @version (a version number or a date)
7 7
  */
8 8
 public class WriteIFs
9
-{
9
+{  int x;
10
+   int tt_t;
11
+   int tt_s;
12
+   int oo1, oo2;
13
+   String ss;
14
+ public WriteIFs()
15
+  {
16
+      // initialise instance variables
17
+      x = 0;
18
+      tt_t = 0;
19
+      tt_s = 1;
20
+      String ss = "";
21
+      oo1 = 61;
22
+      oo2 = 49;
23
+ }
10 24
  
11 25
     public void playerDied(boolean player1) {
12 26
         // Write an IF statement that checks “player1.isAlive()” 
13 27
         // and if that’s false, calls “displayGameOver(player1)”
14
-     
15
-    }
28
+        if (!isAlive(player1)){
29
+            displayGameOver(player1);
30
+         }
31
+}
16 32
     
17 33
     public String thermoSTAT(int room) {
18 34
         // Write an IF statement that checks the 
19 35
         // “temperature(room)” and if that check is less than 70, 
20 36
         // calls “heatOn()” else calls “coolOn()”
37
+        if(tempurature(room)<70){
38
+            heatOn();
39
+        } else {
40
+            coolOn();
41
+        }
21 42
 
22 43
 
23 44
         
@@ -30,12 +51,17 @@ public class WriteIFs
30 51
         // AND 
31 52
         // “insideTemp()” is less than 62, 
32 53
         // calls “startAFire(fireplace1)”
33
-
54
+if (outsideTemp()<50||insideTemp()<62){
55
+    startAFire(fireplace1);
34 56
     }
57
+}
35 58
 
36 59
     public void checkFuel(double fuelLevel) {
37 60
         // Write an IF statement that checks “fuelLevel” 
38 61
         // and if that check is less than 0.08, calls “refuel()”
62
+        if (fuelLevel<0.08){
63
+            refuel();
64
+        }
39 65
 
40 66
     }
41 67
 
@@ -47,26 +73,13 @@ public class WriteIFs
47 73
      * 
48 74
      * instance variables
49 75
      * / 
50
-   int x;
51
-   int tt_t;
52
-   int tt_s;
53
-   int oo1, oo2;
54
-   String ss;
76
+   
55 77
 
56 78
 
57 79
   /**
58 80
    * Constructor for objects of class WriteIFs
59 81
    */
60
-  public WriteIFs()
61
-  {
62
-      // initialise instance variables
63
-      x = 0;
64
-      tt_t = 0;
65
-      tt_s = 1;
66
-      ss = "";
67
-      oo1 = 61;
68
-      oo2 = 49;
69
-  }
82
+  
70 83
 
71 84
     // associated routines
72 85
     public boolean isAlive(boolean p) {

+ 61
- 11
WriteLoops.java Parādīt failu

@@ -20,7 +20,9 @@ public class WriteLoops {
20 20
 
21 21
         // Write a FOR loop that counts from 1 to 10.
22 22
             // calling
23
+            for(int i=1;i<6;i++){
23 24
             w = w + 1;
25
+        }
24 26
             // each time through the loop
25 27
 
26 28
         // this will tell the test how many times the loop executed.
@@ -32,7 +34,9 @@ public class WriteLoops {
32 34
 
33 35
         // Write a FOR loop that counts from 1 to 10.
34 36
         // calling
37
+        for(int i=1;i<11;i++){
35 38
         w = w + 1;
39
+    }
36 40
         // each time through the loop
37 41
         
38 42
         return w;
@@ -43,7 +47,9 @@ public class WriteLoops {
43 47
 
44 48
         // Write a FOR loop that makes 10 iterations, start at 21.
45 49
         // calling
50
+        for(int i=21;i<32;i++){
46 51
         w = w + 1;
52
+    }
47 53
         // each time through the loop
48 54
         
49 55
         return w;
@@ -54,7 +60,9 @@ public class WriteLoops {
54 60
 
55 61
         // Write a FOR loop that counts down from 100 to 0.
56 62
         // calling
63
+        for(int i=100;i>0;i--){
57 64
         w = w + 1;
65
+    }
58 66
         // each time through the loop
59 67
         
60 68
         return w;
@@ -65,7 +73,10 @@ public class WriteLoops {
65 73
 
66 74
         // Write a FOR loop from 0 to 32 by 2s.
67 75
         // calling
68
-        w = w + 1;
76
+        for(int i=0;i<=32;i+=2){
77
+            w=w+1;
78
+    }
79
+    
69 80
         // each time through the loop
70 81
         return w;
71 82
     }
@@ -75,7 +86,9 @@ public class WriteLoops {
75 86
 
76 87
         // Write a FOR loop from 1 to less than 5001 by 11s.
77 88
         // calling
89
+        for(int i=1;i<5001;i+=11){
78 90
         w = w + 1;
91
+    }
79 92
         // each time through the loop
80 93
         
81 94
         return w;
@@ -87,7 +100,12 @@ public class WriteLoops {
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 102
                 // calling
103
+                for(int y=0;y<20;y++){
104
+                for(int x=0;x<5;x++){
90 105
                 w = w + 1;
106
+            }
107
+        }
108
+    
91 109
                 // each time through the inner loop
92 110
 
93 111
         return w;
@@ -102,7 +120,13 @@ public class WriteLoops {
102 120
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103 121
 
104 122
                 // calling
105
-                w = w + 1;
123
+                for(int i=5;i<106;i++){
124
+                    if (i<52){
125
+                       w = w + 1;
126
+                    } else {
127
+                        System.out.println("Hello Zipcode");
128
+                    }
129
+                }
106 130
             // each time through the inner loop
107 131
         
108 132
         return w;
@@ -135,12 +159,18 @@ public class WriteLoops {
135 159
         // you need to use a .equals for two Strings.
136 160
 
137 161
             // calling
138
-            w = w + 1;
162
+            while (!gpsCurrentLocation().equals("Home")){
163
+                driveSomeMore();
164
+                w = w + 1;
165
+            }
166
+            System.out.println("Honey, I'm Home!");
167
+        return w;
168
+        }
139 169
             // each time through the inner loop
140 170
         
141 171
 
142
-            return w;
143
-    }
172
+            
173
+    
144 174
 
145 175
     // Getting harder...
146 176
     // First declare and set “highestScore” to 236. Then set “currentScore” to
@@ -157,7 +187,12 @@ public class WriteLoops {
157 187
         // do your while loop here
158 188
  
159 189
             // calling
160
-            w = w + 1;
190
+            while (runningScore<highestScore){
191
+                runningScore+=currentScore;
192
+                currentScore=gameNextScore();
193
+                w = w + 1;
194
+        }
195
+            
161 196
             // each time through the inner loop
162 197
         
163 198
         return w; // >= 3;
@@ -172,12 +207,15 @@ public class WriteLoops {
172 207
         int runningScore = 0;
173 208
 
174 209
         // do your while loop here
210
+        do  { currentScore=gameNextScore();
211
+            runningScore+=currentScore;
212
+            w = w + 1;
213
+        } while (runningScore<=highestScore);
175 214
 
176 215
             // calling
177
-            w = w + 1;
216
+          
178 217
             // each time through the inner loop
179
-
180
-        return w >= 3;
218
+return w >= 3;
181 219
     }
182 220
 
183 221
     // Write a WHILE loop that checks “serverIsRunning()” and if true
@@ -188,9 +226,18 @@ public class WriteLoops {
188 226
         int w = 0;
189 227
         String adminPhoneNumber = "+1 202 456 1111";
190 228
         
229
+        while(serverIsRunning()) {
230
+            waitFor(5);
231
+            w = w + 1;
232
+        }
233
+        if (!serverIsRunning()){
234
+            sendEmergencyText("Help!",adminPhoneNumber);
235
+            tryServerRestart("Help!",adminPhoneNumber);
236
+        }
237
+            
191 238
 
192 239
         // calling
193
-        w = w + 1;
240
+        
194 241
         // each time through the inner loop
195 242
         
196 243
         return w;
@@ -201,8 +248,11 @@ public class WriteLoops {
201 248
     // and if it is, add 7 to “i”
202 249
     public int loop50by7() {
203 250
         int w = 0;
251
+        int i=7;
204 252
 
205
-
253
+while(i<50){
254
+    i+=7;
255
+}
206 256
             // calling
207 257
             w = w + 1;
208 258
             // each time through the inner loop

+ 1
- 1
WriteLoopsTest.java Parādīt failu

@@ -71,7 +71,7 @@ public class WriteLoopsTest
71 71
     public void Test2to32()
72 72
     {
73 73
         WriteLoops writeLoo1 = new WriteLoops();
74
-        assertEquals(0, writeLoo1.byTwoTo32());
74
+        assertEquals(17, writeLoo1.byTwoTo32());
75 75
     }
76 76
 
77 77
     @Test

+ 15
- 15
package.bluej Parādīt failu

@@ -1,23 +1,23 @@
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=722
9
-editor.fx.0.width=800
10
-editor.fx.0.x=560
11
-editor.fx.0.y=118
12
-objectbench.height=101
13
-objectbench.width=740
8
+editor.fx.0.height=977
9
+editor.fx.0.width=1920
10
+editor.fx.0.x=0
11
+editor.fx.0.y=23
12
+objectbench.height=118
13
+objectbench.width=1241
14 14
 package.divider.horizontal=0.6
15
-package.divider.vertical=0.8625954198473282
16
-package.editor.height=671
15
+package.divider.vertical=0.8043818466353677
16
+package.editor.height=507
17 17
 package.editor.width=1139
18
-package.editor.x=112
19
-package.editor.y=89
20
-package.frame.height=844
18
+package.editor.x=0
19
+package.editor.y=23
20
+package.frame.height=697
21 21
 package.frame.width=1265
22 22
 package.numDependencies=2
23 23
 package.numTargets=4