Jennifer Chao 8 年之前
父節點
當前提交
42243ed14a
共有 4 個文件被更改,包括 102 次插入46 次删除
  1. 55
    28
      WriteIFs.java
  2. 12
    3
      WriteIFsTest.java
  3. 26
    6
      WriteLoops.java
  4. 9
    9
      package.bluej

+ 55
- 28
WriteIFs.java 查看文件

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

+ 12
- 3
WriteIFsTest.java 查看文件

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -13,6 +12,12 @@ import org.junit.Test;
13 12
  */
14 13
 public class WriteIFsTest
15 14
 {
15
+    int x;
16
+    int tt_t;
17
+    int tt_s;
18
+    int oo1, oo2;
19
+    String ss;
20
+
16 21
     private static final double _0_5 = 0.5;
17 22
     private static final double _0_04 = 0.04;
18 23
 
@@ -51,19 +56,20 @@ public class WriteIFsTest
51 56
         assertEquals("Game Over!", writeIFs1.ss);
52 57
     }
53 58
 
54
-
55 59
     @Test
56 60
     public void TestTherm()
57 61
     {
58 62
         WriteIFs writeIFs1 = new WriteIFs();
59 63
         assertEquals("heating", writeIFs1.thermoSTAT(62));
60 64
     }
65
+
61 66
     @Test
62 67
     public void TestTherm1()
63 68
     {
64 69
         WriteIFs writeIFs1 = new WriteIFs();
65 70
         assertEquals("cooling", writeIFs1.thermoSTAT(81));
66 71
     }
72
+
67 73
     @Test
68 74
     public void TestCheckFuel1()
69 75
     {
@@ -71,6 +77,7 @@ public class WriteIFsTest
71 77
         writeIFs1.checkFuel(_0_5);
72 78
         assertEquals(0, writeIFs1.x);
73 79
     }
80
+
74 81
     @Test
75 82
     public void TestCheckFuel2()
76 83
     {
@@ -78,6 +85,7 @@ public class WriteIFsTest
78 85
         writeIFs1.checkFuel(_0_04);
79 86
         assertEquals(99, writeIFs1.x);
80 87
     }
88
+
81 89
     @Test
82 90
     public void TestFireControl()
83 91
     {
@@ -87,6 +95,7 @@ public class WriteIFsTest
87 95
         writeIFs1.fireplaceControl(oo);
88 96
         assertEquals(213, writeIFs1.tt_s);
89 97
     }
98
+
90 99
     @Test
91 100
     public void TestFireControl2()
92 101
     {
@@ -97,6 +106,7 @@ public class WriteIFsTest
97 106
         writeIFs1.fireplaceControl(oo);
98 107
         assertEquals(213, writeIFs1.tt_s);
99 108
     }
109
+
100 110
     @Test
101 111
     public void TestFireControl3()
102 112
     {
@@ -111,4 +121,3 @@ public class WriteIFsTest
111 121
 }
112 122
 
113 123
 
114
-

+ 26
- 6
WriteLoops.java 查看文件

@@ -57,7 +57,7 @@ public class WriteLoops {
57 57
 
58 58
         // Write a FOR loop that counts down from 100 to 0.
59 59
         // calling
60
-        for (int i = 100; i >= 0; i--) {
60
+        for (int i = 100; i > 0; i--) {
61 61
             w = w + 1;
62 62
             // each time through the loop
63 63
         }
@@ -96,7 +96,7 @@ public class WriteLoops {
96 96
         // 0 to less than 20 and the inner one counts from 0 to 4
97 97
         // calling
98 98
         for (int i = 0; i < 20; i++) {
99
-            for (i = 0; i <= 4; i++) {
99
+            for (int j = 0; j <= 4; j++) {
100 100
                 w = w + 1;
101 101
             }
102 102
         }
@@ -300,9 +300,13 @@ public class WriteLoops {
300 300
     public int rewriteFooAsWhile() {
301 301
         int w = 0;
302 302
         int sumOfThrees = 0;
303
+        int index = 0;
303 304
 
304 305
         // calling
305
-        w = w + 1;
306
+        while (index < 3) {
307
+            sumOfThrees = sumOfThrees + threes_array[index];
308
+            w = w + 1;
309
+        }
306 310
         // each time through the inner loop
307 311
 
308 312
         System.out.print("The Sum is ");
@@ -321,11 +325,19 @@ public class WriteLoops {
321 325
     public int manageYardAndJunior() {
322 326
         int w = 0;
323 327
         boolean onTime = true;
328
+        boolean yardNeedsMowed = true;
324 329
 
325 330
         // ADD YOUR CODE here.
331
+        while (isSummer() == true) {
332
+            if (yardNeedsMowed == true) {
333
+                yellAtJuniorToMowLawn();
334
+                w = w + 1;
335
+            }
336
+        }
337
+
338
+        sendJuniorBackToSchool("First Day of School");
326 339
 
327 340
         // be sure to call
328
-        w = w + 1;
329 341
         // each time inside the loop
330 342
 
331 343
         return w;
@@ -341,7 +353,10 @@ public class WriteLoops {
341 353
         int numberOfVotes = voteTallies.length;
342 354
 
343 355
         // calling
344
-        w = w + 1;
356
+        for (int i = 0; i < numberOfVotes; i++) {
357
+            System.out.println(voteTallies[i]);
358
+            w = w + 1;
359
+        }
345 360
         // each time through the inner loop
346 361
 
347 362
         return w;
@@ -353,9 +368,14 @@ public class WriteLoops {
353 368
     public int tallyVote2() {
354 369
         int w = 0;
355 370
         int numberOfVotes = voteTallies.length;
371
+        int idx = 0;
356 372
 
357 373
         // calling
358
-        w = w + 1;
374
+        while (idx < numberOfVotes) {
375
+            System.out.println(voteTallies[idx]);
376
+            w = w + 1;
377
+        }
378
+
359 379
         // each time through the inner loop
360 380
 
361 381
         return w;

+ 9
- 9
package.bluej 查看文件

@@ -5,18 +5,18 @@ dependency1.type=UsesDependency
5 5
 dependency2.from=WriteIFsTest
6 6
 dependency2.to=WriteIFs
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=111
8
+editor.fx.0.height=918
9
+editor.fx.0.width=1131
10
+editor.fx.0.x=457
11
+editor.fx.0.y=42
12
+objectbench.height=112
13 13
 objectbench.width=577
14 14
 package.divider.horizontal=0.6
15
-package.divider.vertical=0.7951388888888888
16
-package.editor.height=451
15
+package.divider.vertical=0.7934027777777778
16
+package.editor.height=450
17 17
 package.editor.width=459
18
-package.editor.x=1258
19
-package.editor.y=266
18
+package.editor.x=892
19
+package.editor.y=163
20 20
 package.frame.height=634
21 21
 package.frame.width=601
22 22
 package.numDependencies=2