NedRedmond преди 8 години
родител
ревизия
682a455b5e
променени са 5 файла, в които са добавени 136 реда и са изтрити 87 реда
  1. 21
    8
      WriteIFs.java
  2. 1
    1
      WriteIFsTest.java
  3. 101
    65
      WriteLoops.java
  4. 1
    1
      WriteLoopsTest.java
  5. 12
    12
      package.bluej

+ 21
- 8
WriteIFs.java Целия файл

@@ -7,36 +7,49 @@
7 7
  */
8 8
 public class WriteIFs
9 9
 {
10
- 
11 10
     public void playerDied(boolean player1) {
12 11
         // Write an IF statement that checks “player1.isAlive()” 
12
+        // THE ABOVE SHOULD BE "isAlive(player1)"
13 13
         // and if that’s false, calls “displayGameOver(player1)”
14
-     
14
+        
15
+        //(!player1.isAlive()) gave me error: "Boolean cannot be dereferenced"
16
+        if (!isAlive(player1)) {
17
+            displayGameOver(player1);
18
+        }
15 19
     }
16 20
     
17 21
     public String thermoSTAT(int room) {
18 22
         // Write an IF statement that checks the 
19 23
         // “temperature(room)” and if that check is less than 70, 
20 24
         // calls “heatOn()” else calls “coolOn()”
25
+        if (tempurature(room) < 70) {
26
+            heatOn();
27
+        } else {
28
+            coolOn();
29
+        }
21 30
 
22
-
23
-        
24 31
         return this.ss;
25 32
     }
26 33
 
27
-    public void fireplaceControl(Object fireplace1) {
34
+    public int fireplaceControl(Object fireplace1) {
28 35
         // Write an IF statement that checks 
29 36
         // “outsideTemp()” is less than 50 
30 37
         // AND 
31 38
         // “insideTemp()” is less than 62, 
32 39
         // calls “startAFire(fireplace1)”
33
-
40
+        if (outsideTemp() < 50 && insideTemp() < 62) {
41
+            startAFire(fireplace1);
42
+        }
43
+        
44
+        return this.tt_s;
34 45
     }
35 46
 
36 47
     public void checkFuel(double fuelLevel) {
37 48
         // Write an IF statement that checks “fuelLevel” 
38 49
         // and if that check is less than 0.08, calls “refuel()”
39
-
50
+        if (fuelLevel < 0.08) {
51
+            refuel();
52
+        }
40 53
     }
41 54
 
42 55
 
@@ -46,7 +59,7 @@ public class WriteIFs
46 59
      * 
47 60
      * 
48 61
      * instance variables
49
-     * / 
62
+     */ 
50 63
    int x;
51 64
    int tt_t;
52 65
    int tt_s;

+ 1
- 1
WriteIFsTest.java Целия файл

@@ -105,7 +105,7 @@ public class WriteIFsTest
105 105
         writeIFs1.oo2 = 80;
106 106
         writeIFs1.tt_s = 1;
107 107
         writeIFs1.fireplaceControl(oo);
108
-        assertEquals(213, writeIFs1.tt_s);
108
+        assertEquals(99, writeIFs1.x);
109 109
     }
110 110
 
111 111
 }

+ 101
- 65
WriteLoops.java Целия файл

@@ -19,9 +19,11 @@ public class WriteLoops {
19 19
         int w = 0;
20 20
 
21 21
         // Write a FOR loop that counts from 1 to 10.
22
-            // calling
22
+        // calling
23
+        for (int i = 0; i < 5; i++) {
23 24
             w = w + 1;
24
-            // each time through the loop
25
+        }
26
+        // each time through the loop
25 27
 
26 28
         // this will tell the test how many times the loop executed.
27 29
         return w;
@@ -32,9 +34,11 @@ public class WriteLoops {
32 34
 
33 35
         // Write a FOR loop that counts from 1 to 10.
34 36
         // calling
35
-        w = w + 1;
37
+        for (int i = 0; i < 10; i++) {
38
+            w = w + 1;
39
+        }
36 40
         // each time through the loop
37
-        
41
+
38 42
         return w;
39 43
     }
40 44
 
@@ -43,9 +47,11 @@ public class WriteLoops {
43 47
 
44 48
         // Write a FOR loop that makes 10 iterations, start at 21.
45 49
         // calling
46
-        w = w + 1;
50
+        for (int i = 21; i <= 31; i++) {
51
+            w = w + 1;
52
+        }
47 53
         // each time through the loop
48
-        
54
+
49 55
         return w;
50 56
     }
51 57
 
@@ -54,9 +60,11 @@ public class WriteLoops {
54 60
 
55 61
         // Write a FOR loop that counts down from 100 to 0.
56 62
         // calling
57
-        w = w + 1;
63
+        for (int i = 100; i > 0; i--) {
64
+            w = w + 1;
65
+        }
58 66
         // each time through the loop
59
-        
67
+
60 68
         return w;
61 69
     }
62 70
 
@@ -65,7 +73,9 @@ 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
+        }
69 79
         // each time through the loop
70 80
         return w;
71 81
     }
@@ -75,9 +85,11 @@ public class WriteLoops {
75 85
 
76 86
         // Write a FOR loop from 1 to less than 5001 by 11s.
77 87
         // calling
78
-        w = w + 1;
88
+        for (int i = 1; i < 5001; i += 11) {
89
+            w = w + 1;
90
+        }
79 91
         // each time through the loop
80
-        
92
+
81 93
         return w;
82 94
     }
83 95
 
@@ -86,9 +98,13 @@ public class WriteLoops {
86 98
 
87 99
         // Write a nested FOR loop(s), where one counts from
88 100
         // 0 to less than 20 and the inner one counts from 0 to 4
89
-                // calling
101
+        // calling
102
+        for (int i = 0; i < 20; i++) {
103
+            for (int j = 0; j < 5; j++) {
90 104
                 w = w + 1;
91
-                // each time through the inner loop
105
+            }
106
+        }
107
+        // each time through the inner loop
92 108
 
93 109
         return w;
94 110
     }
@@ -101,10 +117,16 @@ public class WriteLoops {
101 117
         // loop index counter and if it’s greater than 51,
102 118
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103 119
 
104
-                // calling
120
+        // calling
121
+        for (int i = 5; i < 105; i++) {
122
+            if (i > 57) {
123
+                System.out.print("Hello Zipcode");
105 124
                 w = w + 1;
106
-            // each time through the inner loop
107
-        
125
+            }
126
+            
127
+        }
128
+        // each time through the inner loop
129
+
108 130
         return w;
109 131
     }
110 132
 
@@ -134,12 +156,15 @@ public class WriteLoops {
134 156
 
135 157
         // you need to use a .equals for two Strings.
136 158
 
137
-            // calling
159
+        // calling
160
+        while (!gpsCurrentLocation().equals("Home")) {
161
+            driveSomeMore();
138 162
             w = w + 1;
139
-            // each time through the inner loop
140
-        
163
+        }
164
+        System.out.println("Honey, I'm Home!");
165
+        // each time through the inner loop
141 166
 
142
-            return w;
167
+        return w;
143 168
     }
144 169
 
145 170
     // Getting harder...
@@ -155,11 +180,15 @@ public class WriteLoops {
155 180
         int runningScore = 0;
156 181
 
157 182
         // do your while loop here
158
- 
159
-            // calling
183
+        while (runningScore < highestScore) {
160 184
             w = w + 1;
161
-            // each time through the inner loop
185
+            runningScore += currentScore;
186
+            currentScore = gameNextScore();
187
+        }
188
+        // calling
162 189
         
190
+        // each time through the inner loop
191
+
163 192
         return w; // >= 3;
164 193
     }
165 194
 
@@ -172,10 +201,13 @@ public class WriteLoops {
172 201
         int runningScore = 0;
173 202
 
174 203
         // do your while loop here
175
-
176
-            // calling
204
+        do {
177 205
             w = w + 1;
178
-            // each time through the inner loop
206
+            runningScore += currentScore;
207
+            currentScore = gameNextScore();
208
+        } while (runningScore < highestScore);
209
+        // calling
210
+        // each time through the inner loop
179 211
 
180 212
         return w >= 3;
181 213
     }
@@ -187,12 +219,20 @@ public class WriteLoops {
187 219
     public int checkServerStatus() {
188 220
         int w = 0;
189 221
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191 222
 
192 223
         // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
224
+        while (serverIsRunning()) {
225
+            waitFor(5);
226
+            w = w + 1;
227
+        }
228
+        
229
+        if (!serverIsRunning()) {
230
+            sendEmergencyText("Help!", adminPhoneNumber);
231
+            tryServerRestart();
232
+        }
195 233
         
234
+        // each time through the inner loop
235
+
196 236
         return w;
197 237
     }
198 238
 
@@ -202,11 +242,10 @@ public class WriteLoops {
202 242
     public int loop50by7() {
203 243
         int w = 0;
204 244
 
245
+        // calling
246
+        w = w + 1;
247
+        // each time through the inner loop
205 248
 
206
-            // calling
207
-            w = w + 1;
208
-            // each time through the inner loop
209
-        
210 249
         return w;
211 250
     }
212 251
 
@@ -239,11 +278,10 @@ public class WriteLoops {
239 278
         int w = 0;
240 279
         int sumOfThrees = 0;
241 280
 
242
- 
243
-            // calling
244
-            w = w + 1;
245
-            // each time through the inner loop
246
-        
281
+        // calling
282
+        w = w + 1;
283
+        // each time through the inner loop
284
+
247 285
         System.out.print("The Sum is ");
248 286
         System.out.println(sumOfThrees);
249 287
 
@@ -256,11 +294,10 @@ public class WriteLoops {
256 294
         int w = 0;
257 295
         int sumOfThrees = 0;
258 296
 
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
263
-        
297
+        // calling
298
+        w = w + 1;
299
+        // each time through the inner loop
300
+
264 301
         System.out.print("The Sum is ");
265 302
         System.out.println(sumOfThrees);
266 303
 
@@ -279,11 +316,11 @@ public class WriteLoops {
279 316
         boolean onTime = true;
280 317
 
281 318
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
286
-        
319
+
320
+        // be sure to call
321
+        w = w + 1;
322
+        // each time inside the loop
323
+
287 324
         return w;
288 325
     }
289 326
 
@@ -296,11 +333,10 @@ public class WriteLoops {
296 333
         int w = 0;
297 334
         int numberOfVotes = voteTallies.length;
298 335
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
-        
336
+        // calling
337
+        w = w + 1;
338
+        // each time through the inner loop
339
+
304 340
         return w;
305 341
     }
306 342
 
@@ -311,11 +347,10 @@ public class WriteLoops {
311 347
         int w = 0;
312 348
         int numberOfVotes = voteTallies.length;
313 349
 
350
+        // calling
351
+        w = w + 1;
352
+        // each time through the inner loop
314 353
 
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
319 354
         return w;
320 355
     }
321 356
 
@@ -378,18 +413,19 @@ public class WriteLoops {
378 413
     //         return (i >= 3);
379 414
     //     };
380 415
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
416
+    private int summer = 0;
417
+    private boolean isSummer() {
418
+        if (summer == 3) {
419
+            return true;
388 420
         }
421
+        summer++;
422
+        return false;
423
+    }
424
+
389 425
     private void sendEmergencyText(String mesg, String phone) {
390 426
     }
391 427
 
392
-    private void tryServerRestart(String mesg, String phone) {
428
+    private void tryServerRestart() {
393 429
     }
394 430
 
395 431
     int serverStatus = 5;

+ 1
- 1
WriteLoopsTest.java Целия файл

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

+ 12
- 12
package.bluej Целия файл

@@ -6,19 +6,19 @@ dependency2.from=WriteIFsTest
6 6
 dependency2.to=WriteIFs
7 7
 dependency2.type=UsesDependency
8 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
9
+editor.fx.0.width=881
10
+editor.fx.0.x=496
11
+editor.fx.0.y=55
12
+objectbench.height=104
13
+objectbench.width=663
14 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.827639751552795
16
+package.editor.height=526
17
+package.editor.width=561
18
+package.editor.x=130
19
+package.editor.y=71
20
+package.frame.height=702
21
+package.frame.width=687
22 22
 package.numDependencies=2
23 23
 package.numTargets=4
24 24
 package.showExtends=true