浏览代码

completed lab

父节点
当前提交
e8e4294199
共有 5 个文件被更改,包括 162 次插入151 次删除
  1. 45
    27
      WriteIFs.java
  2. 2
    0
      WriteIFsTest.java
  3. 90
    104
      WriteLoops.java
  4. 6
    1
      WriteLoopsTest.java
  5. 19
    19
      package.bluej

+ 45
- 27
WriteIFs.java 查看文件

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

+ 2
- 0
WriteIFsTest.java 查看文件

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

+ 90
- 104
WriteLoops.java 查看文件

@@ -1,3 +1,5 @@
1
+package FirstSaturday;
2
+
1 3
 import com.sun.org.apache.xpath.internal.SourceTree;
2 4
 
3 5
 import java.awt.SystemTray;
@@ -17,94 +19,74 @@ public class WriteLoops {
17 19
 
18 20
     public int oneToFive() {
19 21
         int w = 0;
20
-
21
-        // Write a FOR loop that counts from 1 to 10.
22
-            // calling
22
+        for (int i = 1; i <= 5; i++){
23 23
             w = w + 1;
24
-            // each time through the loop
25
-
26
-        // this will tell the test how many times the loop executed.
24
+        }
27 25
         return w;
28 26
     }
29 27
 
30 28
     public int oneToTen() {
31 29
         int w = 0;
30
+        for (int i = 1; i <= 10; i++){
31
+            w = w + 1;
32
+        }
32 33
 
33
-        // Write a FOR loop that counts from 1 to 10.
34
-        // calling
35
-        w = w + 1;
36
-        // each time through the loop
37
-        
38 34
         return w;
39 35
     }
40 36
 
41 37
     public int startAtTwentyOne() {
42 38
         int w = 0;
43
-
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
-        
39
+        for (int i = 21; i <=31; i++){
40
+            w = w + 1;
41
+        }
49 42
         return w;
50 43
     }
51 44
 
52 45
     public int countDown() {
53 46
         int w = 0;
54
-
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
-        
47
+        for (int i = 100; i > 0; i--){
48
+            w = w + 1;
49
+        }
60 50
         return w;
61 51
     }
62 52
 
63 53
     public int byTwoTo32() {
64 54
         int w = 0;
65
-
66
-        // Write a FOR loop from 0 to 32 by 2s.
67
-        // calling
68
-        w = w + 1;
69
-        // each time through the loop
55
+        for (int i = 0; i < 32; i += 2){
56
+            w = w + 1;
57
+        }
70 58
         return w;
71 59
     }
72 60
 
73 61
     public int countDownFrom5000() {
74 62
         int w = 0;
75
-
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
-        
63
+        for (int i = 1; i <= 5000; i +=11){
64
+            w = w + 1;
65
+        }
81 66
         return w;
82 67
     }
83 68
 
84 69
     public int nestedFors() {
85 70
         int w = 0;
71
+        for (int i = 0; i < 20; i++){
86 72
 
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
73
+            for (int j = 0; j <= 4; j++){
90 74
                 w = w + 1;
91
-                // each time through the inner loop
75
+            }
92 76
 
77
+        }
93 78
         return w;
94 79
     }
95 80
 
96 81
     public int helloZipCode() {
97 82
         int w = 0;
98
-
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;
103
-
104
-                // calling
83
+        for (int i = 5; i <= 105; i++){
84
+            if (i > 51){
85
+                System.out.print("Hello Zipcode");
86
+            }else{
105 87
                 w = w + 1;
106
-            // each time through the inner loop
107
-        
88
+            }
89
+        }
108 90
         return w;
109 91
     }
110 92
 
@@ -123,7 +105,9 @@ public class WriteLoops {
123 105
             System.out.println("Eww.");
124 106
             i = i - 1;
125 107
         } while (i > 0);
126
-        // what's the primary difference between them?!?
108
+        /** what's the primary difference between them?!? 
109
+         * Unlike the while loop, 
110
+         * the do while loop will run atleast once.*/
127 111
     }
128 112
 
129 113
     // Write a WHILE loop that checks “gpsCurrentLocation()”
@@ -131,15 +115,13 @@ public class WriteLoops {
131 115
     // After the loop is done, print “Honey, I’m Home!”
132 116
     public int driveHome() {
133 117
         int w = 0;
134
-
135
-        // you need to use a .equals for two Strings.
136
-
137
-            // calling
118
+        while (!gpsCurrentLocation().equals("Home")){
119
+            driveSomeMore();
138 120
             w = w + 1;
139
-            // each time through the inner loop
140
-        
141 121
 
142
-            return w;
122
+        }
123
+        System.out.print("Honey, I'm Home!");
124
+        return w;
143 125
     }
144 126
 
145 127
     // Getting harder...
@@ -155,12 +137,12 @@ public class WriteLoops {
155 137
         int runningScore = 0;
156 138
 
157 139
         // do your while loop here
158
- 
159
-            // calling
140
+        while (runningScore < highestScore){
141
+            runningScore = currentScore + runningScore;
160 142
             w = w + 1;
161
-            // each time through the inner loop
162
-        
163
-        return w; // >= 3;
143
+            System.out.print(w);
144
+        }
145
+        return w;
164 146
     }
165 147
 
166 148
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
@@ -172,11 +154,10 @@ public class WriteLoops {
172 154
         int runningScore = 0;
173 155
 
174 156
         // do your while loop here
175
-
176
-            // calling
157
+        do {
158
+            runningScore = currentScore + runningScore;
177 159
             w = w + 1;
178
-            // each time through the inner loop
179
-
160
+        }while (runningScore < highestScore);
180 161
         return w >= 3;
181 162
     }
182 163
 
@@ -187,12 +168,15 @@ public class WriteLoops {
187 168
     public int checkServerStatus() {
188 169
         int w = 0;
189 170
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
171
+        while (serverIsRunning() == true){
172
+            waitFor(5);
173
+            w = w + 1;
174
+        }
175
+        if (serverIsRunning() == false){
176
+            sendEmergencyText("Help!", adminPhoneNumber);
177
+            tryServerRestart("Try reset", adminPhoneNumber);
178
+        }
191 179
 
192
-        // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
195
-        
196 180
         return w;
197 181
     }
198 182
 
@@ -201,12 +185,12 @@ public class WriteLoops {
201 185
     // and if it is, add 7 to “i”
202 186
     public int loop50by7() {
203 187
         int w = 0;
204
-
205
-
206
-            // calling
188
+        int i = 7;
189
+        while (i < 50){
190
+            i += 7;
207 191
             w = w + 1;
208
-            // each time through the inner loop
209
-        
192
+
193
+        }
210 194
         return w;
211 195
     }
212 196
 
@@ -238,12 +222,10 @@ public class WriteLoops {
238 222
     public int rewriteFooAsFor() {
239 223
         int w = 0;
240 224
         int sumOfThrees = 0;
241
-
242
- 
243
-            // calling
225
+        for (int i = 0; i < threes_array.length; i++){
226
+            sumOfThrees = sumOfThrees + threes_array[i];
244 227
             w = w + 1;
245
-            // each time through the inner loop
246
-        
228
+        }
247 229
         System.out.print("The Sum is ");
248 230
         System.out.println(sumOfThrees);
249 231
 
@@ -255,12 +237,12 @@ public class WriteLoops {
255 237
     public int rewriteFooAsWhile() {
256 238
         int w = 0;
257 239
         int sumOfThrees = 0;
240
+        int i = 0;
258 241
 
259
- 
260
-            // calling
242
+        while (i < threes_array.length){
243
+            sumOfThrees = sumOfThrees + threes_array[i];
261 244
             w = w + 1;
262
-            // each time through the inner loop
263
-        
245
+        }
264 246
         System.out.print("The Sum is ");
265 247
         System.out.println(sumOfThrees);
266 248
 
@@ -277,13 +259,17 @@ public class WriteLoops {
277 259
     public int manageYardAndJunior() {
278 260
         int w = 0;
279 261
         boolean onTime = true;
262
+        boolean yardNeedsMowed = true;
280 263
 
281
-        // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
264
+        while (isSummer() == true){
265
+            if (yardNeedsMowed == true){
266
+                yellAtJuniorToMowLawn();
267
+            }
284 268
             w = w + 1;
285
-            // each time inside the loop
286
-        
269
+
270
+        }
271
+        sendJuniorBackToSchool("September 7, 2018");
272
+
287 273
         return w;
288 274
     }
289 275
 
@@ -295,12 +281,11 @@ public class WriteLoops {
295 281
     public int tallyVote1() {
296 282
         int w = 0;
297 283
         int numberOfVotes = voteTallies.length;
298
-
299
- 
300
-            // calling
284
+        for (int i = 0; i <= numberOfVotes-1; i++){
285
+            System.out.println(voteTallies[i]);
301 286
             w = w + 1;
302 287
             // each time through the inner loop
303
-        
288
+        }
304 289
         return w;
305 290
     }
306 291
 
@@ -309,13 +294,13 @@ public class WriteLoops {
309 294
     // where you are.
310 295
     public int tallyVote2() {
311 296
         int w = 0;
297
+        int idx = 0;
312 298
         int numberOfVotes = voteTallies.length;
313
-
314
-
315
-            // calling
299
+        while (idx <= numberOfVotes-1){
300
+            System.out.println(voteTallies[idx]);
301
+            idx++;
316 302
             w = w + 1;
317
-            // each time through the inner loop
318
-        
303
+        }
319 304
         return w;
320 305
     }
321 306
 
@@ -378,14 +363,15 @@ public class WriteLoops {
378 363
     //         return (i >= 3);
379 364
     //     };
380 365
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
366
+    private int summer = 0;
367
+    private boolean isSummer() {
368
+        if (summer == 3) {
369
+            return true;
388 370
         }
371
+        summer++;
372
+        return false;
373
+    }
374
+
389 375
     private void sendEmergencyText(String mesg, String phone) {
390 376
     }
391 377
 

+ 6
- 1
WriteLoopsTest.java 查看文件

@@ -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(16, writeLoo1.byTwoTo32());
75 77
     }
76 78
 
77 79
     @Test
@@ -124,6 +126,8 @@ public class WriteLoopsTest
124 126
         WriteLoops writeLoo1 = new WriteLoops();
125 127
         assertEquals(3, writeLoo1.checkServerStatus());
126 128
     }
129
+
130
+ 
127 131
 }
128 132
 
129 133
 
@@ -136,3 +140,4 @@ public class WriteLoopsTest
136 140
 
137 141
 
138 142
 
143
+

+ 19
- 19
package.bluej 查看文件

@@ -5,19 +5,19 @@ dependency1.type=UsesDependency
5 5
 dependency2.from=WriteIFsTest
6 6
 dependency2.to=WriteIFs
7 7
 dependency2.type=UsesDependency
8
-editor.fx.0.height=722
8
+editor.fx.0.height=709
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=240
11
+editor.fx.0.y=23
12
+objectbench.height=92
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.847926267281106
16
+package.editor.height=545
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=15
19
+package.editor.y=23
20
+package.frame.height=709
21 21
 package.frame.width=1265
22 22
 package.numDependencies=2
23 23
 package.numTargets=4
@@ -44,18 +44,18 @@ target2.type=ClassTarget
44 44
 target2.width=100
45 45
 target2.x=140
46 46
 target2.y=360
47
+target3.association=WriteIFsTest
47 48
 target3.height=110
48
-target3.name=WriteIFsTest
49
+target3.name=WriteIFs
49 50
 target3.showInterface=false
50
-target3.type=UnitTestTargetJunit4
51
+target3.type=ClassTarget
51 52
 target3.width=80
52
-target3.x=180
53
-target3.y=100
54
-target4.association=WriteIFsTest
53
+target3.x=150
54
+target3.y=130
55 55
 target4.height=110
56
-target4.name=WriteIFs
56
+target4.name=WriteIFsTest
57 57
 target4.showInterface=false
58
-target4.type=ClassTarget
58
+target4.type=UnitTestTargetJunit4
59 59
 target4.width=80
60
-target4.x=150
61
-target4.y=130
60
+target4.x=180
61
+target4.y=100