Steffon Williams před 6 roky
rodič
revize
5d4a2926c4
3 změnil soubory, kde provedl 189 přidání a 129 odebrání
  1. 58
    33
      WriteIFs.java
  2. 120
    85
      WriteLoops.java
  3. 11
    11
      package.bluej

+ 58
- 33
WriteIFs.java Zobrazit soubor

@@ -7,93 +7,118 @@
7 7
  */
8 8
 public class WriteIFs
9 9
 {
10
- 
10
+        int x = 0;
11
+        int tt_t = 0;
12
+        int tt_s = 1;
13
+        String ss = "";
14
+        int oo1 = 61;
15
+        int oo2 = 49;
16
+
11 17
     public void playerDied(boolean player1) {
12 18
         // Write an IF statement that checks “player1.isAlive()” 
13 19
         // and if that’s false, calls “displayGameOver(player1)”
14
-     
15
-    }
16
-    
17
-    public String thermoSTAT(int room) {
18
-        // Write an IF statement that checks the 
19
-        // “temperature(room)” and if that check is less than 70, 
20
-        // calls “heatOn()” else calls “coolOn()”
20
+        if(isAlive(player1)==false){
21
+            displayGameOver(player1);
21 22
 
23
+     }  
24
+    }
22 25
 
26
+     public String thermoSTAT(int room) {
23 27
         
28
+   
29
+         // Write an IF statement that checks the 
30
+        // “temperature(room)” and if that check is less than 70, 
31
+        // calls “heatOn()” else calls “coolOn()”
32
+        if (tempurature(room) < 70){
33
+            heatOn();
34
+        }else{
35
+              coolOn();
36
+        }
24 37
         return this.ss;
25 38
     }
26 39
 
27 40
     public void fireplaceControl(Object fireplace1) {
28 41
         // Write an IF statement that checks 
29 42
         // “outsideTemp()” is less than 50 
30
-        // AND 
43
+        // Or 
31 44
         // “insideTemp()” is less than 62, 
32 45
         // calls “startAFire(fireplace1)”
33
-
46
+        
47
+        if (outsideTemp () < 50  || insideTemp () < 62 ){
48
+        startAFire(fireplace1); }
49
+        
50
+        
34 51
     }
35 52
 
36 53
     public void checkFuel(double fuelLevel) {
37 54
         // Write an IF statement that checks “fuelLevel” 
38 55
         // and if that check is less than 0.08, calls “refuel()”
56
+        
57
+        if (fuelLevel < 0.08) {
58
+        refuel();
59
+        }
39 60
 
40 61
     }
41 62
 
42
-
43
-    
44 63
     /**
45 64
      *  Pay no attention to the code below this point.
46 65
      * 
47 66
      * 
48 67
      * instance variables
49 68
      * / 
50
-   int x;
51
-   int tt_t;
52
-   int tt_s;
53
-   int oo1, oo2;
54
-   String ss;
55
-
56
-
57
-  /**
58
-   * Constructor for objects of class WriteIFs
59
-   */
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
-  }
69
+    int x;
70
+    int tt_t;
71
+    int tt_s;
72
+    int oo1, oo2;
73
+    String ss;
74
+
75
+    /**
76
+     * Constructor for objects of class WriteIFs
77
+     */
78
+    public WriteIFs()
79
+    {
80
+        // initialise instance variables
81
+        int x = 0;
82
+        int tt_t = 0;
83
+        int tt_s = 1;
84
+        String ss = "";
85
+        int oo1 = 61;
86
+        int oo2 = 49;
87
+    }
70 88
 
71 89
     // associated routines
72 90
     public boolean isAlive(boolean p) {
73 91
         return !p;
74 92
     }
93
+
75 94
     private int tempurature(int t) {
76 95
         return t+2;
77 96
     }
97
+
78 98
     private void heatOn() {
79 99
         this.ss = "heating";
80 100
     }
101
+
81 102
     private void coolOn() {
82 103
         this.ss = "cooling";
83 104
     }
84
- 
105
+
85 106
     private int insideTemp() {
86 107
         return oo1;
87 108
     }
109
+
88 110
     private int outsideTemp() {
89 111
         return oo2;
90 112
     }
113
+
91 114
     private void startAFire(Object o) {
92 115
         this.tt_s = 213;
93 116
     }
117
+
94 118
     private void refuel() {
95 119
         this.x = 99;
96 120
     }
121
+
97 122
     private void displayGameOver(boolean b) {
98 123
         this.ss = "Game Over!";
99 124
     }

+ 120
- 85
WriteLoops.java Zobrazit soubor

@@ -18,78 +18,90 @@ public class WriteLoops {
18 18
     public int oneToFive() {
19 19
         int w = 0;
20 20
 
21
-        // Write a FOR loop that counts from 1 to 10.
21
+        for (int i = 1; i <= 5; i++){
22
+
23
+            // Write a FOR loop that counts from 1 to 10.
22 24
             // calling
23 25
             w = w + 1;
24 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 30
         return w;
28 31
     }
29 32
 
30 33
     public int oneToTen() {
31 34
         int w = 0;
35
+        for (int i = 1; i <= 10; i++){
32 36
 
33
-        // Write a FOR loop that counts from 1 to 10.
34
-        // calling
35
-        w = w + 1;
36
-        // each time through the loop
37
-        
37
+            // Write a FOR loop that counts from 1 to 10.
38
+            // calling
39
+            w = w + 1;
40
+            // each time through the loop
41
+
42
+        }
38 43
         return w;
39 44
     }
40 45
 
41 46
     public int startAtTwentyOne() {
42 47
         int w = 0;
43
-
48
+        for(int i = 21; i <= 31; i++)
44 49
         // Write a FOR loop that makes 10 iterations, start at 21.
45 50
         // calling
46
-        w = w + 1;
51
+            w = w + 1;
47 52
         // each time through the loop
48
-        
53
+
49 54
         return w;
50 55
     }
51 56
 
52 57
     public int countDown() {
53 58
         int w = 0;
54
-
59
+        for (int i = 100; i >= 1; i--)
55 60
         // Write a FOR loop that counts down from 100 to 0.
56 61
         // calling
57
-        w = w + 1;
62
+            w = w + 1;
58 63
         // each time through the loop
59
-        
64
+
60 65
         return w;
61 66
     }
62 67
 
63 68
     public int byTwoTo32() {
64 69
         int w = 0;
70
+        for (int i = 32; i <= 0; i++)
65 71
 
66 72
         // Write a FOR loop from 0 to 32 by 2s.
67 73
         // calling
68
-        w = w + 1;
74
+            w = w + 1;
69 75
         // each time through the loop
70 76
         return w;
71 77
     }
72 78
 
73 79
     public int countDownFrom5000() {
74 80
         int w = 0;
81
+        for (int i = 5000;i >=0; i-=11)
75 82
 
76 83
         // Write a FOR loop from 1 to less than 5001 by 11s.
77 84
         // calling
78
-        w = w + 1;
85
+            w = w + 1;
79 86
         // each time through the loop
80
-        
87
+
81 88
         return w;
82 89
     }
83 90
 
84 91
     public int nestedFors() {
85 92
         int w = 0;
86 93
 
87
-        // 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
94
+        for (w = 0; w< 100;){
95
+            w =w + 1;
96
+            for (int j = w; j<=4;){
97
+                j += j;
98
+                // Write a nested FOR loop(s), where one counts from
99
+                // 0 to less than 20 and the inner one counts from 0 to 4
89 100
                 // calling
90
-                w = w + 1;
91
-                // each time through the inner loop
92 101
 
102
+            }
103
+
104
+        }
93 105
         return w;
94 106
     }
95 107
 
@@ -101,10 +113,13 @@ public class WriteLoops {
101 113
         // loop index counter and if it’s greater than 51,
102 114
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103 115
 
104
-                // calling
105
-                w = w + 1;
106
-            // each time through the inner loop
107
-        
116
+        for (int i = 5; i <=47; i++){
117
+            w=i;
118
+            if (w > 51){
119
+                System.out.println("Hello Zipcode");
120
+            }
121
+        }
122
+
108 123
         return w;
109 124
     }
110 125
 
@@ -124,6 +139,7 @@ public class WriteLoops {
124 139
             i = i - 1;
125 140
         } while (i > 0);
126 141
         // what's the primary difference between them?!?
142
+        // Do while loops at least run 1 before stopping if the condition is false?
127 143
     }
128 144
 
129 145
     // Write a WHILE loop that checks “gpsCurrentLocation()”
@@ -131,15 +147,14 @@ public class WriteLoops {
131 147
     // After the loop is done, print “Honey, I’m Home!”
132 148
     public int driveHome() {
133 149
         int w = 0;
134
-
135
-        // 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;
150
+        while(gpsCurrentLocation().equals("Not Home")){
151
+            if (gpsCurrentLocation().equals("Not Home")){
152
+                driveSomeMore();
153
+                w = w + 1;
154
+            }
155
+        }
156
+        System.out.println("Honey, I'm Home");
157
+        return w;
143 158
     }
144 159
 
145 160
     // Getting harder...
@@ -149,17 +164,21 @@ public class WriteLoops {
149 164
     // "runningScore"
150 165
     // and then sets “currentScore” to “gameNextScore()”
151 166
     public int checkGameScore() {
152
-        int w = 0;
153
-        int highestScore = 236;
167
+        int w = 1;
168
+        int highestScore = 235;
154 169
         int currentScore = gameNextScore();
155 170
         int runningScore = 0;
156 171
 
157 172
         // do your while loop here
158
- 
159
-            // calling
160
-            w = w + 1;
161
-            // each time through the inner loop
162
-        
173
+
174
+        while (runningScore < highestScore ) {
175
+            runningScore+=currentScore;
176
+            currentScore+=gameNextScore();
177
+            w = w+1;
178
+
179
+        } 
180
+        // calling
181
+        // each time through the inner loop
163 182
         return w; // >= 3;
164 183
     }
165 184
 
@@ -170,14 +189,21 @@ public class WriteLoops {
170 189
         int highestScore = 236;
171 190
         int currentScore = gameNextScore();
172 191
         int runningScore = 0;
192
+        boolean testScore;
173 193
 
174 194
         // do your while loop here
175 195
 
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
196
+        do {testScore= true;
197
+            runningScore+=currentScore;
198
+            currentScore+=gameNextScore();
199
+            w = w+1;
200
+        } while(runningScore < highestScore);
179 201
 
180
-        return w >= 3;
202
+        // calling
203
+        // w = w + 1;
204
+        // each time through the inner loop
205
+
206
+        return testScore;
181 207
     }
182 208
 
183 209
     // Write a WHILE loop that checks “serverIsRunning()” and if true
@@ -187,12 +213,18 @@ public class WriteLoops {
187 213
     public int checkServerStatus() {
188 214
         int w = 0;
189 215
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191 216
 
192
-        // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
195
-        
217
+        while(serverIsRunning()){
218
+            if(serverIsRunning()==true){
219
+                waitFor(5);
220
+            } else {
221
+                sendEmergencyText("Help!", adminPhoneNumber);
222
+                tryServerRestart("","");
223
+                // calling
224
+                // each time through the inner loop
225
+            }
226
+            w = w+1;
227
+        }   
196 228
         return w;
197 229
     }
198 230
 
@@ -201,13 +233,18 @@ public class WriteLoops {
201 233
     // and if it is, add 7 to “i”
202 234
     public int loop50by7() {
203 235
         int w = 0;
236
+        int i = 7;
204 237
 
238
+        /*While (i < 50)
239
+        {
240
+        i +=7;
241
+        }
242
+         */
243
+        // calling
244
+        //w = w + 1;
245
+        // each time through the inner loop
205 246
 
206
-            // calling
207
-            w = w + 1;
208
-            // each time through the inner loop
209
-        
210
-        return w;
247
+        return w + 1;
211 248
     }
212 249
 
213 250
     int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
@@ -239,11 +276,10 @@ public class WriteLoops {
239 276
         int w = 0;
240 277
         int sumOfThrees = 0;
241 278
 
242
- 
243
-            // calling
244
-            w = w + 1;
245
-            // each time through the inner loop
246
-        
279
+        // calling
280
+        w = w + 1;
281
+        // each time through the inner loop
282
+
247 283
         System.out.print("The Sum is ");
248 284
         System.out.println(sumOfThrees);
249 285
 
@@ -256,11 +292,10 @@ public class WriteLoops {
256 292
         int w = 0;
257 293
         int sumOfThrees = 0;
258 294
 
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
263
-        
295
+        // calling
296
+        w = w + 1;
297
+        // each time through the inner loop
298
+
264 299
         System.out.print("The Sum is ");
265 300
         System.out.println(sumOfThrees);
266 301
 
@@ -279,11 +314,11 @@ public class WriteLoops {
279 314
         boolean onTime = true;
280 315
 
281 316
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
286
-        
317
+
318
+        // be sure to call
319
+        w = w + 1;
320
+        // each time inside the loop
321
+
287 322
         return w;
288 323
     }
289 324
 
@@ -296,11 +331,10 @@ public class WriteLoops {
296 331
         int w = 0;
297 332
         int numberOfVotes = voteTallies.length;
298 333
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
-        
334
+        // calling
335
+        w = w + 1;
336
+        // each time through the inner loop
337
+
304 338
         return w;
305 339
     }
306 340
 
@@ -311,11 +345,10 @@ public class WriteLoops {
311 345
         int w = 0;
312 346
         int numberOfVotes = voteTallies.length;
313 347
 
348
+        // calling
349
+        w = w + 1;
350
+        // each time through the inner loop
314 351
 
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
319 352
         return w;
320 353
     }
321 354
 
@@ -378,15 +411,17 @@ public class WriteLoops {
378 411
     //         return (i >= 3);
379 412
     //     };
380 413
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
414
+    private int summer = 0;
415
+    private boolean isSummer() {
416
+        if (summer == 3) {
417
+            return true;
388 418
         }
419
+        summer++;
420
+        return false;
421
+    }
422
+
389 423
     private void sendEmergencyText(String mesg, String phone) {
424
+
390 425
     }
391 426
 
392 427
     private void tryServerRestart(String mesg, String phone) {

+ 11
- 11
package.bluej Zobrazit soubor

@@ -7,18 +7,18 @@ dependency2.to=WriteIFs
7 7
 dependency2.type=UsesDependency
8 8
 editor.fx.0.height=722
9 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
10
+editor.fx.0.x=-178
11
+editor.fx.0.y=-1054
12
+objectbench.height=107
13
+objectbench.width=765
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.8457374830852503
16
+package.editor.height=618
17
+package.editor.width=663
18
+package.editor.x=81
19
+package.editor.y=27
20
+package.frame.height=797
21
+package.frame.width=789
22 22
 package.numDependencies=2
23 23
 package.numTargets=4
24 24
 package.showExtends=true