Seth пре 6 година
родитељ
комит
ef01242ff3
3 измењених фајлова са 144 додато и 56 уклоњено
  1. 38
    23
      WriteIFs.java
  2. 66
    33
      WriteLoops.java
  3. 40
    0
      WriteLoopsTest.java

+ 38
- 23
WriteIFs.java Прегледај датотеку

5
  * @author (your name)
5
  * @author (your name)
6
  * @version (a version number or a date)
6
  * @version (a version number or a date)
7
  */
7
  */
8
-public class WriteIFs
9
-{
8
+public class WriteIFs{
9
+   int x;
10
+   int tt_t;
11
+   int tt_s;
12
+   int oo1, oo2;
13
+   String ss; 
14
+
15
+
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;
26
+    }
27
+
10
  
28
  
11
     public void playerDied(boolean player1) {
29
     public void playerDied(boolean player1) {
12
         // Write an IF statement that checks “player1.isAlive()” 
30
         // Write an IF statement that checks “player1.isAlive()” 
13
         // and if that’s false, calls “displayGameOver(player1)”
31
         // and if that’s false, calls “displayGameOver(player1)”
14
-        if (player1.isAlive(false)) {
15
-        
32
+        if (!isAlive(player1)) {
33
+            displayGameOver(player1);
16
         }
34
         }
17
      
35
      
18
     }
36
     }
21
         // Write an IF statement that checks the 
39
         // Write an IF statement that checks the 
22
         // “temperature(room)” and if that check is less than 70, 
40
         // “temperature(room)” and if that check is less than 70, 
23
         // calls “heatOn()” else calls “coolOn()”
41
         // calls “heatOn()” else calls “coolOn()”
24
-
25
-
42
+        if(tempurature(room) < 70) {
43
+            heatOn();
44
+        
45
+        } else {
46
+            coolOn();
47
+        }
48
+            
26
         
49
         
27
         return this.ss;
50
         return this.ss;
28
     }
51
     }
33
         // AND 
56
         // AND 
34
         // “insideTemp()” is less than 62, 
57
         // “insideTemp()” is less than 62, 
35
         // calls “startAFire(fireplace1)”
58
         // calls “startAFire(fireplace1)”
36
-
59
+        if(outsideTemp() < 50 || insideTemp() < 62) {
60
+            startAFire(fireplace1);
61
+        }
37
     }
62
     }
38
 
63
 
39
     public void checkFuel(double fuelLevel) {
64
     public void checkFuel(double fuelLevel) {
40
         // Write an IF statement that checks “fuelLevel” 
65
         // Write an IF statement that checks “fuelLevel” 
41
         // and if that check is less than 0.08, calls “refuel()”
66
         // and if that check is less than 0.08, calls “refuel()”
42
-
67
+        if(fuelLevel < 0.08) {
68
+        refuel();
69
+        
70
+        }
43
     }
71
     }
44
 
72
 
45
 
73
 
50
      * 
78
      * 
51
      * instance variables
79
      * instance variables
52
      * / 
80
      * / 
53
-   int x;
54
-   int tt_t;
55
-   int tt_s;
56
-   int oo1, oo2;
57
-   String ss;
81
+   
58
 
82
 
59
 
83
 
60
   /**
84
   /**
61
    * Constructor for objects of class WriteIFs
85
    * Constructor for objects of class WriteIFs
62
    */
86
    */
63
-  public WriteIFs()
64
-  {
65
-      // initialise instance variables
66
-      x = 0;
67
-      tt_t = 0;
68
-      tt_s = 1;
69
-      ss = "";
70
-      oo1 = 61;
71
-      oo2 = 49;
72
-  }
87
+  
73
 
88
 
74
     // associated routines
89
     // associated routines
75
     public boolean isAlive(boolean p) {
90
     public boolean isAlive(boolean p) {

+ 66
- 33
WriteLoops.java Прегледај датотеку

97
         // Write a nested FOR loop(s), where one counts from
97
         // Write a nested FOR loop(s), where one counts from
98
         // 0 to less than 20 and the inner one counts from 0 to 4
98
         // 0 to less than 20 and the inner one counts from 0 to 4
99
         // calling
99
         // calling
100
-        for(int i=0; i>20; ++i) {
101
-            for(int j=0; j>=4; ++j) {
102
-                w += 1;
100
+        for(int i=0; i<20; ++i) {
101
+            for(int j=0; j<=4; ++j) {
102
+                w = w + 1;
103
             }
103
             }
104
         }
104
         }
105
 
105
 
116
         // loop index counter and if it’s greater than 51,
116
         // loop index counter and if it’s greater than 51,
117
         // prints “Hello Zipcode” instead of the statement w = w + 1;
117
         // prints “Hello Zipcode” instead of the statement w = w + 1;
118
         for(int i=5; i<=105; i++) {
118
         for(int i=5; i<=105; i++) {
119
-           if(i <= 51){
120
-               w += 1;
119
+            if(i <= 51){
120
+                w += 1;
121
             } else {
121
             } else {
122
                 System.out.println("Hello Zipcode");
122
                 System.out.println("Hello Zipcode");
123
             }
123
             }
124
         }
124
         }
125
         // calling
125
         // calling
126
-        
126
+
127
         // each time through the inner loop
127
         // each time through the inner loop
128
 
128
 
129
         return w;
129
         return w;
156
         // you need to use a .equals for two Strings.
156
         // you need to use a .equals for two Strings.
157
         while(gpsCurrentLocation().equals("Home") == false){
157
         while(gpsCurrentLocation().equals("Home") == false){
158
             if(w<6){
158
             if(w<6){
159
-            driveSomeMore();
160
-            w++;
159
+                driveSomeMore();
160
+                w++;
161
             } else {
161
             } else {
162
-            gpsCurrentLocation().equals("Home");
162
+                gpsCurrentLocation().equals("Home");
163
             }
163
             }
164
         }
164
         }
165
-        
165
+
166
         System.out.println("Honey, I’m Home!");
166
         System.out.println("Honey, I’m Home!");
167
         // calling
167
         // calling
168
-        
168
+
169
         // each time through the inner loop
169
         // each time through the inner loop
170
 
170
 
171
         return w;
171
         return w;
182
         int highestScore = 236;
182
         int highestScore = 236;
183
         int currentScore = gameNextScore();
183
         int currentScore = gameNextScore();
184
         int runningScore = 0;
184
         int runningScore = 0;
185
-        
186
-        while(runningScore < currentScore){
187
-             currentScore += runningScore;
188
-             currentScore += gameNextScore();
189
-             w = w + 1;
185
+
186
+        while(runningScore < highestScore){
187
+            runningScore += currentScore;
188
+            currentScore = gameNextScore();
189
+            w = w + 1;
190
         }
190
         }
191
         
191
         
192
-
193
         // calling
192
         // calling
194
-        
195
         // each time through the inner loop
193
         // each time through the inner loop
196
-
197
         return w; //>= 3;
194
         return w; //>= 3;
198
     }
195
     }
199
 
196
 
207
 
204
 
208
         // do your while loop here
205
         // do your while loop here
209
         do {
206
         do {
210
-            currentScore += runningScore;
211
-            currentScore += gameNextScore();
207
+            runningScore += currentScore;
208
+            currentScore = gameNextScore();
212
             w = w + 1;
209
             w = w + 1;
213
         } while(runningScore < highestScore);
210
         } while(runningScore < highestScore);
214
         // calling
211
         // calling
215
         
212
         
213
+
216
         // each time through the inner loop
214
         // each time through the inner loop
217
 
215
 
218
         return w >= 3;
216
         return w >= 3;
226
         int w = 0;
224
         int w = 0;
227
         String adminPhoneNumber = "+1 202 456 1111";
225
         String adminPhoneNumber = "+1 202 456 1111";
228
 
226
 
227
+        while (serverIsRunning() == true) {
228
+            waitFor(5);
229
+            w = w + 1;
230
+        }
231
+
232
+        if(serverIsRunning() == false) {
233
+            sendEmergencyText("Help!", adminPhoneNumber);
234
+
235
+        }
229
         // calling
236
         // calling
230
-        w = w + 1;
237
+
231
         // each time through the inner loop
238
         // each time through the inner loop
232
 
239
 
233
         return w;
240
         return w;
241
         int i = 0;
248
         int i = 0;
242
         // calling
249
         // calling
243
         while(i<50) {
250
         while(i<50) {
244
-        i+=7;
245
-        w = w + 1;
251
+            i+=7;
252
+            w = w + 1;
246
         }
253
         }
247
-        
254
+
248
         // each time through the inner loop
255
         // each time through the inner loop
249
 
256
 
250
         return w;
257
         return w;
278
     public int rewriteFooAsFor() {
285
     public int rewriteFooAsFor() {
279
         int w = 0;
286
         int w = 0;
280
         int sumOfThrees = 0;
287
         int sumOfThrees = 0;
281
-
288
+        
289
+        for(int i = 0; i < threes_array.length; i++){
290
+            sumOfThrees += threes_array[i];
291
+            w = w + 1;
292
+        }
282
         // calling
293
         // calling
283
-        w = w + 1;
294
+        
284
         // each time through the inner loop
295
         // each time through the inner loop
285
 
296
 
286
         System.out.print("The Sum is ");
297
         System.out.print("The Sum is ");
294
     public int rewriteFooAsWhile() {
305
     public int rewriteFooAsWhile() {
295
         int w = 0;
306
         int w = 0;
296
         int sumOfThrees = 0;
307
         int sumOfThrees = 0;
297
-
308
+        int idx = 0;
309
+        
310
+        while(idx<threes_array.length) {
311
+            sumOfThrees += threes_array[idx];
312
+            idx++;
313
+            w = w + 1;
314
+        }
298
         // calling
315
         // calling
299
-        w = w + 1;
316
+        
300
         // each time through the inner loop
317
         // each time through the inner loop
301
 
318
 
302
         System.out.print("The Sum is ");
319
         System.out.print("The Sum is ");
315
     public int manageYardAndJunior() {
332
     public int manageYardAndJunior() {
316
         int w = 0;
333
         int w = 0;
317
         boolean onTime = true;
334
         boolean onTime = true;
335
+        boolean yardNeedsMowed = true;
318
 
336
 
319
         // ADD YOUR CODE here.
337
         // ADD YOUR CODE here.
338
+        while(isSummer()) {
339
+            if(yardNeedsMowed) {
340
+            yellAtJuniorToMowLawn();       
341
+            w = w + 1;
342
+            }
320
 
343
 
344
+        }
321
         // be sure to call
345
         // be sure to call
322
-        w = w + 1;
346
+        sendJuniorBackToSchool("First day of school");
323
         // each time inside the loop
347
         // each time inside the loop
324
 
348
 
325
         return w;
349
         return w;
334
         int w = 0;
358
         int w = 0;
335
         int numberOfVotes = voteTallies.length;
359
         int numberOfVotes = voteTallies.length;
336
 
360
 
337
-        // calling
338
-        w = w + 1;
361
+        for(int i = 0; i < numberOfVotes; i++) {
362
+            System.out.println(voteTallies[i]);
363
+            w = w + 1;
364
+        }// calling
365
+
339
         // each time through the inner loop
366
         // each time through the inner loop
340
 
367
 
341
         return w;
368
         return w;
346
     // where you are.
373
     // where you are.
347
     public int tallyVote2() {
374
     public int tallyVote2() {
348
         int w = 0;
375
         int w = 0;
376
+        int idx = 0;
349
         int numberOfVotes = voteTallies.length;
377
         int numberOfVotes = voteTallies.length;
350
 
378
 
379
+        while(idx < numberOfVotes) {
380
+            System.out.println(voteTallies[idx]);
381
+            idx++;
382
+            w = w + 1;
383
+        }
351
         // calling
384
         // calling
352
-        w = w + 1;
385
+
353
         // each time through the inner loop
386
         // each time through the inner loop
354
 
387
 
355
         return w;
388
         return w;

+ 40
- 0
WriteLoopsTest.java Прегледај датотеку

124
         WriteLoops writeLoo1 = new WriteLoops();
124
         WriteLoops writeLoo1 = new WriteLoops();
125
         assertEquals(3, writeLoo1.checkServerStatus());
125
         assertEquals(3, writeLoo1.checkServerStatus());
126
     }
126
     }
127
+
128
+    @Test
129
+    public void loop50by7()
130
+    {
131
+        WriteLoops writeLoo1 = new WriteLoops();
132
+        assertEquals(8, writeLoo1.loop50by7());
133
+    }
134
+
135
+    @Test
136
+    public void tallyVoteTest1()
137
+    {
138
+        WriteLoops writeLoo1 = new WriteLoops();
139
+        assertEquals(13, writeLoo1.tallyVote1());
140
+    }
141
+
142
+    @Test
143
+    public void tallyVoteTest2()
144
+    {
145
+        WriteLoops writeLoo1 = new WriteLoops();
146
+        assertEquals(13, writeLoo1.tallyVote2());
147
+    }
148
+
149
+    @Test
150
+    public void rewriteFooForTest()
151
+    {
152
+        WriteLoops writeLoo2 = new WriteLoops();
153
+        assertEquals(7, writeLoo2.rewriteFooAsFor());
154
+    }
155
+
156
+    @Test
157
+    public void rewriteFooWhileTest()
158
+    {
159
+        WriteLoops writeLoo1 = new WriteLoops();
160
+        assertEquals(7, writeLoo1.rewriteFooAsWhile());
161
+    }
127
 }
162
 }
128
 
163
 
129
 
164
 
136
 
171
 
137
 
172
 
138
 
173
 
174
+
175
+
176
+
177
+
178
+