Steffon Williams 6 lat temu
rodzic
commit
5d4a2926c4
3 zmienionych plików z 189 dodań i 129 usunięć
  1. 58
    33
      WriteIFs.java
  2. 120
    85
      WriteLoops.java
  3. 11
    11
      package.bluej

+ 58
- 33
WriteIFs.java Wyświetl plik

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

+ 120
- 85
WriteLoops.java Wyświetl plik

18
     public int oneToFive() {
18
     public int oneToFive() {
19
         int w = 0;
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
             // calling
24
             // calling
23
             w = w + 1;
25
             w = w + 1;
24
             // each time through the loop
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
         return w;
30
         return w;
28
     }
31
     }
29
 
32
 
30
     public int oneToTen() {
33
     public int oneToTen() {
31
         int w = 0;
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
         return w;
43
         return w;
39
     }
44
     }
40
 
45
 
41
     public int startAtTwentyOne() {
46
     public int startAtTwentyOne() {
42
         int w = 0;
47
         int w = 0;
43
-
48
+        for(int i = 21; i <= 31; i++)
44
         // Write a FOR loop that makes 10 iterations, start at 21.
49
         // Write a FOR loop that makes 10 iterations, start at 21.
45
         // calling
50
         // calling
46
-        w = w + 1;
51
+            w = w + 1;
47
         // each time through the loop
52
         // each time through the loop
48
-        
53
+
49
         return w;
54
         return w;
50
     }
55
     }
51
 
56
 
52
     public int countDown() {
57
     public int countDown() {
53
         int w = 0;
58
         int w = 0;
54
-
59
+        for (int i = 100; i >= 1; i--)
55
         // Write a FOR loop that counts down from 100 to 0.
60
         // Write a FOR loop that counts down from 100 to 0.
56
         // calling
61
         // calling
57
-        w = w + 1;
62
+            w = w + 1;
58
         // each time through the loop
63
         // each time through the loop
59
-        
64
+
60
         return w;
65
         return w;
61
     }
66
     }
62
 
67
 
63
     public int byTwoTo32() {
68
     public int byTwoTo32() {
64
         int w = 0;
69
         int w = 0;
70
+        for (int i = 32; i <= 0; i++)
65
 
71
 
66
         // Write a FOR loop from 0 to 32 by 2s.
72
         // Write a FOR loop from 0 to 32 by 2s.
67
         // calling
73
         // calling
68
-        w = w + 1;
74
+            w = w + 1;
69
         // each time through the loop
75
         // each time through the loop
70
         return w;
76
         return w;
71
     }
77
     }
72
 
78
 
73
     public int countDownFrom5000() {
79
     public int countDownFrom5000() {
74
         int w = 0;
80
         int w = 0;
81
+        for (int i = 5000;i >=0; i-=11)
75
 
82
 
76
         // Write a FOR loop from 1 to less than 5001 by 11s.
83
         // Write a FOR loop from 1 to less than 5001 by 11s.
77
         // calling
84
         // calling
78
-        w = w + 1;
85
+            w = w + 1;
79
         // each time through the loop
86
         // each time through the loop
80
-        
87
+
81
         return w;
88
         return w;
82
     }
89
     }
83
 
90
 
84
     public int nestedFors() {
91
     public int nestedFors() {
85
         int w = 0;
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
                 // calling
100
                 // calling
90
-                w = w + 1;
91
-                // each time through the inner loop
92
 
101
 
102
+            }
103
+
104
+        }
93
         return w;
105
         return w;
94
     }
106
     }
95
 
107
 
101
         // loop index counter and if it’s greater than 51,
113
         // loop index counter and if it’s greater than 51,
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
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
         return w;
123
         return w;
109
     }
124
     }
110
 
125
 
124
             i = i - 1;
139
             i = i - 1;
125
         } while (i > 0);
140
         } while (i > 0);
126
         // what's the primary difference between them?!?
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
     // Write a WHILE loop that checks “gpsCurrentLocation()”
145
     // Write a WHILE loop that checks “gpsCurrentLocation()”
131
     // After the loop is done, print “Honey, I’m Home!”
147
     // After the loop is done, print “Honey, I’m Home!”
132
     public int driveHome() {
148
     public int driveHome() {
133
         int w = 0;
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
     // Getting harder...
160
     // Getting harder...
149
     // "runningScore"
164
     // "runningScore"
150
     // and then sets “currentScore” to “gameNextScore()”
165
     // and then sets “currentScore” to “gameNextScore()”
151
     public int checkGameScore() {
166
     public int checkGameScore() {
152
-        int w = 0;
153
-        int highestScore = 236;
167
+        int w = 1;
168
+        int highestScore = 235;
154
         int currentScore = gameNextScore();
169
         int currentScore = gameNextScore();
155
         int runningScore = 0;
170
         int runningScore = 0;
156
 
171
 
157
         // do your while loop here
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
         return w; // >= 3;
182
         return w; // >= 3;
164
     }
183
     }
165
 
184
 
170
         int highestScore = 236;
189
         int highestScore = 236;
171
         int currentScore = gameNextScore();
190
         int currentScore = gameNextScore();
172
         int runningScore = 0;
191
         int runningScore = 0;
192
+        boolean testScore;
173
 
193
 
174
         // do your while loop here
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
     // Write a WHILE loop that checks “serverIsRunning()” and if true
209
     // Write a WHILE loop that checks “serverIsRunning()” and if true
187
     public int checkServerStatus() {
213
     public int checkServerStatus() {
188
         int w = 0;
214
         int w = 0;
189
         String adminPhoneNumber = "+1 202 456 1111";
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
         return w;
228
         return w;
197
     }
229
     }
198
 
230
 
201
     // and if it is, add 7 to “i”
233
     // and if it is, add 7 to “i”
202
     public int loop50by7() {
234
     public int loop50by7() {
203
         int w = 0;
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
     int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
250
     int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
239
         int w = 0;
276
         int w = 0;
240
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
283
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
284
         System.out.println(sumOfThrees);
249
 
285
 
256
         int w = 0;
292
         int w = 0;
257
         int sumOfThrees = 0;
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
         System.out.print("The Sum is ");
299
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
300
         System.out.println(sumOfThrees);
266
 
301
 
279
         boolean onTime = true;
314
         boolean onTime = true;
280
 
315
 
281
         // ADD YOUR CODE here.
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
         return w;
322
         return w;
288
     }
323
     }
289
 
324
 
296
         int w = 0;
331
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
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
         return w;
338
         return w;
305
     }
339
     }
306
 
340
 
311
         int w = 0;
345
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
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
         return w;
352
         return w;
320
     }
353
     }
321
 
354
 
378
     //         return (i >= 3);
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
     private void sendEmergencyText(String mesg, String phone) {
423
     private void sendEmergencyText(String mesg, String phone) {
424
+
390
     }
425
     }
391
 
426
 
392
     private void tryServerRestart(String mesg, String phone) {
427
     private void tryServerRestart(String mesg, String phone) {

+ 11
- 11
package.bluej Wyświetl plik

7
 dependency2.type=UsesDependency
7
 dependency2.type=UsesDependency
8
 editor.fx.0.height=722
8
 editor.fx.0.height=722
9
 editor.fx.0.width=800
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
 package.divider.horizontal=0.6
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
 package.numDependencies=2
22
 package.numDependencies=2
23
 package.numTargets=4
23
 package.numTargets=4
24
 package.showExtends=true
24
 package.showExtends=true