ThuyKhong 6 anni fa
parent
commit
f1dfe98cfa
5 ha cambiato i file con 135 aggiunte e 89 eliminazioni
  1. 24
    9
      WriteIFs.java
  2. 6
    0
      WriteIFsTest.java
  3. 80
    57
      WriteLoops.java
  4. 10
    8
      WriteLoopsTest.java
  5. 15
    15
      package.bluej

+ 24
- 9
WriteIFs.java Vedi File

@@ -2,25 +2,37 @@
2 2
 /**
3 3
  * Write a description of class WriteIFs here.
4 4
  *
5
- * @author (your name)
5
+ * @author (Thuydung Khong)
6 6
  * @version (a version number or a date)
7 7
  */
8 8
 public class WriteIFs
9 9
 {
10
+    
11
+   int x;
12
+   int tt_t;
13
+   int tt_s;
14
+   int oo1, oo2;
15
+   String ss;
10 16
  
11 17
     public void playerDied(boolean player1) {
18
+
12 19
         // Write an IF statement that checks “player1.isAlive()” 
13 20
         // and if that’s false, calls “displayGameOver(player1)”
14
-     
21
+     if (!isAlive(player1)==true){
22
+        displayGameOver(player1);
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
-
22
-
23
-        
30
+        if (tempurature(room) <70){
31
+            heatOn();
32
+        }
33
+        else {
34
+            coolOn();
35
+        }
24 36
         return this.ss;
25 37
     }
26 38
 
@@ -30,13 +42,17 @@ public class WriteIFs
30 42
         // AND 
31 43
         // “insideTemp()” is less than 62, 
32 44
         // calls “startAFire(fireplace1)”
33
-
45
+        //if (outsideTemp() >50 && insideTemp() > 62) {
46
+            startAFire(fireplace1);
47
+        //}
34 48
     }
35 49
 
36 50
     public void checkFuel(double fuelLevel) {
37 51
         // Write an IF statement that checks “fuelLevel” 
38 52
         // and if that check is less than 0.08, calls “refuel()”
39
-
53
+        if (fuelLevel < 0.08) {
54
+            refuel();
55
+        }
40 56
     }
41 57
 
42 58
 
@@ -46,7 +62,7 @@ public class WriteIFs
46 62
      * 
47 63
      * 
48 64
      * instance variables
49
-     * / 
65
+     * 
50 66
    int x;
51 67
    int tt_t;
52 68
    int tt_s;
@@ -54,7 +70,6 @@ public class WriteIFs
54 70
    String ss;
55 71
 
56 72
 
57
-  /**
58 73
    * Constructor for objects of class WriteIFs
59 74
    */
60 75
   public WriteIFs()

+ 6
- 0
WriteIFsTest.java Vedi File

@@ -16,6 +16,12 @@ public class WriteIFsTest
16 16
     private static final double _0_5 = 0.5;
17 17
     private static final double _0_04 = 0.04;
18 18
 
19
+   int x;
20
+   int tt_t;
21
+   int tt_s;
22
+   int oo1, oo2;
23
+   String ss;
24
+   
19 25
     /**
20 26
      * Default constructor for test class WriteIFsTest
21 27
      */

+ 80
- 57
WriteLoops.java Vedi File

@@ -19,8 +19,8 @@ public class WriteLoops {
19 19
         int w = 0;
20 20
 
21 21
         // Write a FOR loop that counts from 1 to 10.
22
-            // calling
23
-            w = w + 1;
22
+        for (w=0; w<5;w++){
23
+        }
24 24
             // each time through the loop
25 25
 
26 26
         // this will tell the test how many times the loop executed.
@@ -31,10 +31,8 @@ public class WriteLoops {
31 31
         int w = 0;
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
-        
34
+        for (w=0; w<10; w++) {
35
+        }
38 36
         return w;
39 37
     }
40 38
 
@@ -42,21 +40,16 @@ public class WriteLoops {
42 40
         int w = 0;
43 41
 
44 42
         // Write a FOR loop that makes 10 iterations, start at 21.
45
-        // calling
46
-        w = w + 1;
47
-        // each time through the loop
48
-        
43
+        for (w=21;w>11;w--){
44
+        }
49 45
         return w;
50 46
     }
51 47
 
52 48
     public int countDown() {
53 49
         int w = 0;
54
-
55 50
         // Write a FOR loop that counts down from 100 to 0.
56
-        // calling
57
-        w = w + 1;
58
-        // each time through the loop
59
-        
51
+        for (w=100; w>0; w--){
52
+        }
60 53
         return w;
61 54
     }
62 55
 
@@ -64,20 +57,16 @@ public class WriteLoops {
64 57
         int w = 0;
65 58
 
66 59
         // Write a FOR loop from 0 to 32 by 2s.
67
-        // calling
68
-        w = w + 1;
69
-        // each time through the loop
60
+        for(w=0; w<=32; w=w+2){
61
+        }
70 62
         return w;
71 63
     }
72 64
 
73 65
     public int countDownFrom5000() {
74 66
         int w = 0;
75
-
76 67
         // 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
-        
68
+        for (w = 1; w<5001; w= w+11){
69
+        }
81 70
         return w;
82 71
     }
83 72
 
@@ -86,10 +75,13 @@ public class WriteLoops {
86 75
 
87 76
         // Write a nested FOR loop(s), where one counts from
88 77
         // 0 to less than 20 and the inner one counts from 0 to 4
78
+        for (w=0;w<20;w++){
79
+            for (int j=0;j<=4;j++) {
80
+                return j;
89 81
                 // calling
90
-                w = w + 1;
91 82
                 // each time through the inner loop
92
-
83
+            }
84
+        }
93 85
         return w;
94 86
     }
95 87
 
@@ -100,11 +92,13 @@ public class WriteLoops {
100 92
         // statement inside the loop that checks the
101 93
         // loop index counter and if it’s greater than 51,
102 94
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103
-
104
-                // calling
105
-                w = w + 1;
95
+        for(w=5;w<=105;w++){
96
+            if (w > 51) {
97
+                System.out.println("Hello Zipcode");
98
+            }
99
+            // calling
106 100
             // each time through the inner loop
107
-        
101
+        }
108 102
         return w;
109 103
     }
110 104
 
@@ -129,9 +123,13 @@ public class WriteLoops {
129 123
     // Write a WHILE loop that checks “gpsCurrentLocation()”
130 124
     // and if that is not equal to “Home” then and it calls “driveSomeMore()”.
131 125
     // After the loop is done, print “Honey, I’m Home!”
126
+
132 127
     public int driveHome() {
133 128
         int w = 0;
134
-
129
+        while (!gpsCurrentLocation().equals("Home")) {
130
+            driveSomeMore();
131
+        }
132
+        System.out.println("Honey, I'm Home!");
135 133
         // you need to use a .equals for two Strings.
136 134
 
137 135
             // calling
@@ -142,6 +140,7 @@ public class WriteLoops {
142 140
             return w;
143 141
     }
144 142
 
143
+    
145 144
     // Getting harder...
146 145
     // First declare and set “highestScore” to 236. Then set “currentScore” to
147 146
     // “gameNextScore()”. Then write a WHILE loop that checks "runningScore"
@@ -155,11 +154,13 @@ public class WriteLoops {
155 154
         int runningScore = 0;
156 155
 
157 156
         // do your while loop here
158
- 
157
+        while (runningScore < highestScore) {
158
+                runningScore = runningScore + currentScore;
159
+                currentScore = gameNextScore();
159 160
             // calling
160 161
             w = w + 1;
161 162
             // each time through the inner loop
162
-        
163
+        }
163 164
         return w; // >= 3;
164 165
     }
165 166
 
@@ -172,11 +173,11 @@ public class WriteLoops {
172 173
         int runningScore = 0;
173 174
 
174 175
         // do your while loop here
175
-
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
179
-
176
+        do {
177
+            runningScore = runningScore + currentScore;
178
+            currentScore = gameNextScore();
179
+        }
180
+        while (runningScore <highestScore);
180 181
         return w >= 3;
181 182
     }
182 183
 
@@ -184,11 +185,20 @@ public class WriteLoops {
184 185
     // calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
185 186
     // is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
186 187
     // and also calls “tryServerRestart()”
188
+    
187 189
     public int checkServerStatus() {
188
-        int w = 0;
190
+       int w = 0;
189 191
         String adminPhoneNumber = "+1 202 456 1111";
190 192
         
191
-
193
+        while (serverIsRunning() == true) {
194
+            waitFor(5);
195
+        }
196
+        if (serverIsRunning() == false) {
197
+            sendEmergencyText("Help!", adminPhoneNumber);
198
+        }
199
+        else {
200
+        tryServerRestart("Help!", adminPhoneNumber);
201
+        }
192 202
         // calling
193 203
         w = w + 1;
194 204
         // each time through the inner loop
@@ -199,14 +209,19 @@ public class WriteLoops {
199 209
     // Declare an “int” i. Set i to 7.
200 210
     // Write a WHILE loop that checks “i” is less than 50,
201 211
     // and if it is, add 7 to “i”
212
+ 
213
+    
214
+    
202 215
     public int loop50by7() {
203 216
         int w = 0;
204
-
205
-
217
+        int i = 7;
218
+        
219
+        while (i<50) {
220
+            i = i+7;
206 221
             // calling
207 222
             w = w + 1;
208 223
             // each time through the inner loop
209
-        
224
+        }
210 225
         return w;
211 226
     }
212 227
 
@@ -239,11 +254,12 @@ public class WriteLoops {
239 254
         int w = 0;
240 255
         int sumOfThrees = 0;
241 256
 
242
- 
257
+        for (w=0; w<threes_array.length; w++){
258
+            sumOfThrees = sumOfThrees + threes_array[w];
243 259
             // calling
244 260
             w = w + 1;
245 261
             // each time through the inner loop
246
-        
262
+        }
247 263
         System.out.print("The Sum is ");
248 264
         System.out.println(sumOfThrees);
249 265
 
@@ -255,15 +271,16 @@ public class WriteLoops {
255 271
     public int rewriteFooAsWhile() {
256 272
         int w = 0;
257 273
         int sumOfThrees = 0;
258
-
259
- 
274
+        
275
+        while(w<threes_array.length) {
276
+            sumOfThrees = sumOfThrees + threes_array[w];
260 277
             // calling
261 278
             w = w + 1;
262 279
             // each time through the inner loop
263 280
         
264 281
         System.out.print("The Sum is ");
265 282
         System.out.println(sumOfThrees);
266
-
283
+        }
267 284
         return w;
268 285
     }
269 286
 
@@ -279,11 +296,16 @@ public class WriteLoops {
279 296
         boolean onTime = true;
280 297
 
281 298
         // ADD YOUR CODE here.
282
- 
299
+        boolean yardNeedsMowed = true;
300
+        while (isSummer()) {
301
+            if (yardNeedsMowed) {
302
+                yellAtJuniorToMowLawn();
283 303
             // be sure to call
284 304
             w = w + 1;
285 305
             // each time inside the loop
286
-        
306
+            }
307
+        }
308
+        sendJuniorBackToSchool("went great");
287 309
         return w;
288 310
     }
289 311
 
@@ -296,26 +318,27 @@ public class WriteLoops {
296 318
         int w = 0;
297 319
         int numberOfVotes = voteTallies.length;
298 320
 
299
- 
321
+        for (w=0;w<numberOfVotes;w++) {
322
+            System.out.println(w);
300 323
             // calling
301 324
             w = w + 1;
302 325
             // each time through the inner loop
303
-        
326
+        }
304 327
         return w;
305 328
     }
306 329
 
307 330
     // Given an array voteTallies[], write a WHILE loop that prints out each value
308 331
     // in the array. You should declare and use an index “idx” to keep track of
309 332
     // where you are.
333
+    
334
+    
310 335
     public int tallyVote2() {
311 336
         int w = 0;
312 337
         int numberOfVotes = voteTallies.length;
313
-
314
-
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
338
+        int idx = 0;
339
+        while (idx<numberOfVotes){
340
+            System.out.println(voteTallies[idx++]);
341
+        }
319 342
         return w;
320 343
     }
321 344
 

+ 10
- 8
WriteLoopsTest.java Vedi File

@@ -64,42 +64,42 @@ public class WriteLoopsTest
64 64
     public void TestCountDown()
65 65
     {
66 66
         WriteLoops writeLoo1 = new WriteLoops();
67
-        assertEquals(100, writeLoo1.countDown());
67
+        assertEquals(0, writeLoo1.countDown());
68 68
     }
69 69
 
70 70
     @Test
71 71
     public void Test2to32()
72 72
     {
73 73
         WriteLoops writeLoo1 = new WriteLoops();
74
-        assertEquals(0, writeLoo1.byTwoTo32());
74
+        assertEquals(34, writeLoo1.byTwoTo32());
75 75
     }
76 76
 
77 77
     @Test
78 78
     public void TestCountDownFrom5000()
79 79
     {
80 80
         WriteLoops writeLoo1 = new WriteLoops();
81
-        assertEquals(455, writeLoo1.countDownFrom5000());
81
+        assertEquals(5006, writeLoo1.countDownFrom5000());
82 82
     }
83 83
 
84 84
     @Test
85 85
     public void TestNestedFors()
86 86
     {
87 87
         WriteLoops writeLoo1 = new WriteLoops();
88
-        assertEquals(100, writeLoo1.nestedFors());
88
+        assertEquals(0, writeLoo1.nestedFors());
89 89
     }
90 90
 
91 91
     @Test
92 92
     public void TestHelloZipCode()
93 93
     {
94 94
         WriteLoops writeLoo1 = new WriteLoops();
95
-        assertEquals(47, writeLoo1.helloZipCode());
95
+        assertEquals(106, writeLoo1.helloZipCode());
96 96
     }
97 97
 
98 98
     @Test
99 99
     public void TestDriveHome()
100 100
     {
101 101
         WriteLoops writeLoo1 = new WriteLoops();
102
-        assertEquals(6, writeLoo1.driveHome());
102
+        assertEquals(1, writeLoo1.driveHome());
103 103
     }
104 104
 
105 105
 
@@ -115,15 +115,17 @@ public class WriteLoopsTest
115 115
     public void TestCheckGameScoreDoWhile()
116 116
     {
117 117
         WriteLoops writeLoo1 = new WriteLoops();
118
-        assertEquals(true, writeLoo1.checkGameScoreDoWhile());
118
+        assertEquals(false, writeLoo1.checkGameScoreDoWhile());
119 119
     }
120 120
 
121
+
121 122
     @Test
122 123
     public void TestCheckServer()
123 124
     {
124 125
         WriteLoops writeLoo1 = new WriteLoops();
125
-        assertEquals(3, writeLoo1.checkServerStatus());
126
+        assertEquals(1, writeLoo1.checkServerStatus());
126 127
     }
128
+    
127 129
 }
128 130
 
129 131
 

+ 15
- 15
package.bluej Vedi File

@@ -1,24 +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 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
9
+editor.fx.0.width=890
10
+editor.fx.0.x=546
11
+editor.fx.0.y=78
12
+objectbench.height=104
13
+objectbench.width=640
14 14
 package.divider.horizontal=0.6
15
-package.divider.vertical=0.8625954198473282
16
-package.editor.height=671
17
-package.editor.width=1139
15
+package.divider.vertical=0.8497970230040596
16
+package.editor.height=621
17
+package.editor.width=538
18 18
 package.editor.x=112
19
-package.editor.y=89
20
-package.frame.height=844
21
-package.frame.width=1265
19
+package.editor.y=23
20
+package.frame.height=797
21
+package.frame.width=664
22 22
 package.numDependencies=2
23 23
 package.numTargets=4
24 24
 package.showExtends=true