Quellcode durchsuchen

Finished First Saturday

Nathan Hall vor 6 Jahren
Ursprung
Commit
355ab4d9a7
5 geänderte Dateien mit 232 neuen und 131 gelöschten Zeilen
  1. 66
    30
      WriteIFs.java
  2. 2
    0
      WriteIFsTest.java
  3. 148
    82
      WriteLoops.java
  4. 3
    1
      WriteLoopsTest.java
  5. 13
    18
      package.bluej

+ 66
- 30
WriteIFs.java Datei anzeigen

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

+ 2
- 0
WriteIFsTest.java Datei anzeigen

@@ -1,3 +1,5 @@
1
+package FirstSaturday;
2
+
1 3
 
2 4
 
3 5
 import static org.junit.Assert.*;

+ 148
- 82
WriteLoops.java Datei anzeigen

@@ -1,3 +1,6 @@
1
+
2
+package FirstSaturday;
3
+
1 4
 import com.sun.org.apache.xpath.internal.SourceTree;
2 5
 
3 6
 import java.awt.SystemTray;
@@ -18,54 +21,59 @@ public class WriteLoops {
18 21
     public int oneToFive() {
19 22
         int w = 0;
20 23
 
21
-        // Write a FOR loop that counts from 1 to 10.
24
+        // Write a FOR loop that counts from 1 to 5.
25
+        for (int i = 0; i < 5; i++){
22 26
             // calling
23 27
             w = w + 1;
24 28
             // each time through the loop
25
-
29
+        }
26 30
         // this will tell the test how many times the loop executed.
27 31
         return w;
28 32
     }
29 33
 
30 34
     public int oneToTen() {
31 35
         int w = 0;
32
-
33
-        // Write a FOR loop that counts from 1 to 10.
34
-        // calling
35
-        w = w + 1;
36
-        // each time through the loop
37
-        
36
+        for (int i = 0; i < 10; i++){
37
+            // Write a FOR loop that counts from 1 to 10.
38
+            // calling
39
+            w = w + 1;
40
+            // each time through the loop
41
+        }
38 42
         return w;
39 43
     }
40 44
 
41 45
     public int startAtTwentyOne() {
42 46
         int w = 0;
43 47
 
44
-        // Write a FOR loop that makes 10 iterations, start at 21.
45
-        // calling
46
-        w = w + 1;
47
-        // each time through the loop
48
-        
48
+        for (int i = 21; i <= 31; i++){
49
+            // Write a FOR loop that makes 10 iterations, start at 21.
50
+            // calling
51
+            w = w + 1;
52
+            // each time through the loop
53
+        }
49 54
         return w;
50 55
     }
51 56
 
52 57
     public int countDown() {
53 58
         int w = 0;
54 59
 
55
-        // Write a FOR loop that counts down from 100 to 0.
56
-        // calling
57
-        w = w + 1;
58
-        // each time through the loop
59
-        
60
+        for (int i = 100; i > 0; i--){
61
+            // Write a FOR loop that counts down from 100 to 0.
62
+            // calling
63
+            w = w + 1;
64
+            // each time through the loop
65
+        }
60 66
         return w;
61 67
     }
62 68
 
63 69
     public int byTwoTo32() {
64 70
         int w = 0;
65 71
 
66
-        // Write a FOR loop from 0 to 32 by 2s.
67
-        // calling
68
-        w = w + 1;
72
+        for (int i = 0; i <= 32; i = i + 2){
73
+            // Write a FOR loop from 0 to 32 by 2s.
74
+            // calling
75
+            w = w + 1;
76
+        }
69 77
         // each time through the loop
70 78
         return w;
71 79
     }
@@ -73,38 +81,50 @@ public class WriteLoops {
73 81
     public int countDownFrom5000() {
74 82
         int w = 0;
75 83
 
76
-        // Write a FOR loop from 1 to less than 5001 by 11s.
77
-        // calling
78
-        w = w + 1;
79
-        // each time through the loop
80
-        
84
+        for (int i = 1; i < 5001; i = i + 11){
85
+            // Write a FOR loop from 1 to less than 5001 by 11s.
86
+            // calling
87
+            w = w + 1;
88
+            // each time through the loop
89
+
90
+        }
81 91
         return w;
82 92
     }
83 93
 
84 94
     public int nestedFors() {
85 95
         int w = 0;
86 96
 
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
89
-                // calling
97
+        for (int i = 0; i < 20; i++){
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
100
+            // calling
101
+            for (int j = 0; j < 5; j++){
90 102
                 w = w + 1;
91
-                // each time through the inner loop
92
-
103
+            }
104
+            // each time through the inner loop
105
+        }
93 106
         return w;
94 107
     }
95 108
 
96 109
     public int helloZipCode() {
97 110
         int w = 0;
98 111
 
99
-        // Write a FOR loop that counts from 5 to 105. Put an IF
100
-        // statement inside the loop that checks the
101
-        // loop index counter and if it’s greater than 51,
102
-        // prints “Hello Zipcode” instead of the statement w = w + 1;
112
+        for (int i = 5; i <= 105; i++){
113
+            // Write a FOR loop that counts from 5 to 105. Put an IF
114
+            // statement inside the loop that checks the
115
+            // loop index counter and if it’s greater than 51,
116
+            // prints “Hello Zipcode” instead of the statement w = w + 1;
103 117
 
104
-                // calling
118
+            System.out.println(w);
119
+
120
+            // calling
121
+            if (i > 51){
122
+                System.out.println("Hello Zipcode");
123
+            } else {
105 124
                 w = w + 1;
125
+            }
106 126
             // each time through the inner loop
107
-        
127
+        }
108 128
         return w;
109 129
     }
110 130
 
@@ -132,14 +152,17 @@ public class WriteLoops {
132 152
     public int driveHome() {
133 153
         int w = 0;
134 154
 
155
+        gpsCurrentLocation();
135 156
         // you need to use a .equals for two Strings.
136 157
 
158
+        while (gpsCurrentLocation().equals("Not Home")){
159
+            driveSomeMore();
160
+
137 161
             // calling
138 162
             w = w + 1;
139 163
             // each time through the inner loop
140
-        
141
-
142
-            return w;
164
+        }
165
+        return w;
143 166
     }
144 167
 
145 168
     // Getting harder...
@@ -155,11 +178,15 @@ public class WriteLoops {
155 178
         int runningScore = 0;
156 179
 
157 180
         // do your while loop here
158
- 
181
+        while (runningScore < highestScore) {
182
+            runningScore += currentScore;
159 183
             // calling
160
-            w = w + 1;
184
+
185
+            if (runningScore < highestScore){
186
+                w = w + 1;
187
+            }
161 188
             // each time through the inner loop
162
-        
189
+        }
163 190
         return w; // >= 3;
164 191
     }
165 192
 
@@ -172,10 +199,18 @@ public class WriteLoops {
172 199
         int runningScore = 0;
173 200
 
174 201
         // do your while loop here
175
-
176
-            // calling
202
+        do {
203
+            runningScore += currentScore;
177 204
             w = w + 1;
178
-            // each time through the inner loop
205
+            if (runningScore < highestScore){
206
+
207
+            }
208
+
209
+        } while (runningScore < highestScore);
210
+
211
+        // calling
212
+
213
+        // each time through the inner loop
179 214
 
180 215
         return w >= 3;
181 216
     }
@@ -187,12 +222,17 @@ public class WriteLoops {
187 222
     public int checkServerStatus() {
188 223
         int w = 0;
189 224
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191 225
 
192
-        // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
195
-        
226
+        while(serverIsRunning()){
227
+            waitFor(5);
228
+            // calling
229
+            if (serverIsRunning() == false){
230
+                sendEmergencyText("Help!", adminPhoneNumber);
231
+                tryServerRestart("Help!", adminPhoneNumber);
232
+            }
233
+            w = w + 1;
234
+            // each time through the inner loop
235
+        }
196 236
         return w;
197 237
     }
198 238
 
@@ -201,12 +241,15 @@ public class WriteLoops {
201 241
     // and if it is, add 7 to “i”
202 242
     public int loop50by7() {
203 243
         int w = 0;
244
+        int i = 7;
204 245
 
205
-
206
-            // calling
246
+        while (i < 50){
247
+            i += 7;
207 248
             w = w + 1;
208
-            // each time through the inner loop
209
-        
249
+        }
250
+        // calling
251
+        // each time through the inner loop
252
+
210 253
         return w;
211 254
     }
212 255
 
@@ -239,11 +282,15 @@ public class WriteLoops {
239 282
         int w = 0;
240 283
         int sumOfThrees = 0;
241 284
 
242
- 
285
+        for (int i = 0; i < threes_array.length; i++){
243 286
             // calling
287
+            sumOfThrees = threes_array[i];
288
+            
244 289
             w = w + 1;
290
+
245 291
             // each time through the inner loop
246
-        
292
+        }
293
+
247 294
         System.out.print("The Sum is ");
248 295
         System.out.println(sumOfThrees);
249 296
 
@@ -255,12 +302,16 @@ public class WriteLoops {
255 302
     public int rewriteFooAsWhile() {
256 303
         int w = 0;
257 304
         int sumOfThrees = 0;
305
+        int i = 0;
258 306
 
259
- 
260
-            // calling
307
+        while (i < threes_array.length){
308
+        // calling
309
+        sumOfThrees = threes_array[i];
310
+            i++;
261 311
             w = w + 1;
262
-            // each time through the inner loop
263
-        
312
+        }
313
+        // each time through the inner loop
314
+
264 315
         System.out.print("The Sum is ");
265 316
         System.out.println(sumOfThrees);
266 317
 
@@ -277,13 +328,22 @@ public class WriteLoops {
277 328
     public int manageYardAndJunior() {
278 329
         int w = 0;
279 330
         boolean onTime = true;
331
+        boolean yardNeedsMowed = true;
332
+        
333
+        while (isSummer()){
334
+            w = w + 1;
335
+            if (yardNeedsMowed){
336
+                yellAtJuniorToMowLawn();
337
+                
338
+            }
339
+        }
280 340
 
281 341
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
342
+
343
+        // be sure to call
286 344
         
345
+        // each time inside the loop
346
+        sendJuniorBackToSchool("September 12");
287 347
         return w;
288 348
     }
289 349
 
@@ -296,11 +356,13 @@ public class WriteLoops {
296 356
         int w = 0;
297 357
         int numberOfVotes = voteTallies.length;
298 358
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
-        
359
+        for (int i = 0; i < numberOfVotes; i++){
360
+        // calling
361
+        System.out.println(voteTallies[i]);
362
+        w = w + 1;
363
+        // each time through the inner loop
364
+
365
+    }
304 366
         return w;
305 367
     }
306 368
 
@@ -310,12 +372,15 @@ public class WriteLoops {
310 372
     public int tallyVote2() {
311 373
         int w = 0;
312 374
         int numberOfVotes = voteTallies.length;
375
+        int idx = 0;
313 376
 
314
-
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
377
+        while(idx < voteTallies.length){
378
+            System.out.println(voteTallies[idx]);
379
+            idx++;
380
+        // calling
381
+        w = w + 1;
382
+        // each time through the inner loop
383
+    }
319 384
         return w;
320 385
     }
321 386
 
@@ -378,14 +443,15 @@ public class WriteLoops {
378 443
     //         return (i >= 3);
379 444
     //     };
380 445
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
446
+    private int summer = 0;
447
+    private boolean isSummer() {
448
+        if (summer == 3) {
449
+            return true;
388 450
         }
451
+        summer++;
452
+        return false;
453
+    }
454
+
389 455
     private void sendEmergencyText(String mesg, String phone) {
390 456
     }
391 457
 

+ 3
- 1
WriteLoopsTest.java Datei anzeigen

@@ -1,3 +1,5 @@
1
+package FirstSaturday;
2
+
1 3
 
2 4
 
3 5
 import static org.junit.Assert.*;
@@ -71,7 +73,7 @@ public class WriteLoopsTest
71 73
     public void Test2to32()
72 74
     {
73 75
         WriteLoops writeLoo1 = new WriteLoops();
74
-        assertEquals(0, writeLoo1.byTwoTo32());
76
+        assertEquals(17, writeLoo1.byTwoTo32());
75 77
     }
76 78
 
77 79
     @Test

+ 13
- 18
package.bluej Datei anzeigen

@@ -1,29 +1,24 @@
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
+objectbench.height=93
9
+objectbench.width=592
14 10
 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
11
+package.divider.vertical=0.8470948012232415
12
+package.editor.height=547
13
+package.editor.width=490
14
+package.editor.x=27
15
+package.editor.y=32
16
+package.frame.height=712
17
+package.frame.width=616
22 18
 package.numDependencies=2
23 19
 package.numTargets=4
24 20
 package.showExtends=true
25 21
 package.showUses=true
26
-project.charset=UTF-8
27 22
 readme.height=58
28 23
 readme.name=@README
29 24
 readme.width=47