Browse Source

finished lab

Connor Dunnigan 6 years ago
parent
commit
134be0beac
3 changed files with 143 additions and 85 deletions
  1. 42
    44
      WriteIFs.java
  2. 0
    2
      WriteIFsTest.java
  3. 101
    39
      WriteLoops.java

+ 42
- 44
WriteIFs.java View File

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

+ 0
- 2
WriteIFsTest.java View File

110
 
110
 
111
 }
111
 }
112
 
112
 
113
-
114
-

+ 101
- 39
WriteLoops.java View File

20
 
20
 
21
         // Write a FOR loop that counts from 1 to 10.
21
         // Write a FOR loop that counts from 1 to 10.
22
             // calling
22
             // calling
23
-            w = w + 1;
23
+            for(int i=1; i<=5; i++){
24
+                w = w + 1;
25
+            }
24
             // each time through the loop
26
             // each time through the loop
25
 
27
 
26
         // this will tell the test how many times the loop executed.
28
         // this will tell the test how many times the loop executed.
32
 
34
 
33
         // Write a FOR loop that counts from 1 to 10.
35
         // Write a FOR loop that counts from 1 to 10.
34
         // calling
36
         // calling
35
-        w = w + 1;
37
+        for(int i=1; i<=10; i++){
38
+                w = w + 1;
39
+            }
36
         // each time through the loop
40
         // each time through the loop
37
         
41
         
38
         return w;
42
         return w;
43
 
47
 
44
         // Write a FOR loop that makes 10 iterations, start at 21.
48
         // Write a FOR loop that makes 10 iterations, start at 21.
45
         // calling
49
         // calling
46
-        w = w + 1;
50
+        for(int i=21; i<=31; i++){
51
+                w = w + 1;
52
+            }
47
         // each time through the loop
53
         // each time through the loop
48
         
54
         
49
         return w;
55
         return w;
54
 
60
 
55
         // Write a FOR loop that counts down from 100 to 0.
61
         // Write a FOR loop that counts down from 100 to 0.
56
         // calling
62
         // calling
57
-        w = w + 1;
63
+        for(int i=100; i>0; i--){
64
+                w = w + 1;
65
+            }
58
         // each time through the loop
66
         // each time through the loop
59
         
67
         
60
         return w;
68
         return w;
65
 
73
 
66
         // Write a FOR loop from 0 to 32 by 2s.
74
         // Write a FOR loop from 0 to 32 by 2s.
67
         // calling
75
         // calling
68
-        w = w + 1;
76
+        for(int i=0; i<32; i+=2){
77
+                w = w + 0;
78
+            }
69
         // each time through the loop
79
         // each time through the loop
70
         return w;
80
         return w;
71
     }
81
     }
75
 
85
 
76
         // Write a FOR loop from 1 to less than 5001 by 11s.
86
         // Write a FOR loop from 1 to less than 5001 by 11s.
77
         // calling
87
         // calling
78
-        w = w + 1;
88
+        for(int i=1; i<5001; i+=11){
89
+                w = w + 1;
90
+            }
79
         // each time through the loop
91
         // each time through the loop
80
         
92
         
81
         return w;
93
         return w;
87
         // Write a nested FOR loop(s), where one counts from
99
         // 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
100
         // 0 to less than 20 and the inner one counts from 0 to 4
89
                 // calling
101
                 // calling
90
-                w = w + 1;
102
+                for(int i=0; i<20; i++){
103
+                    for(int j=0; j<=4; j++){
104
+                        w = w + 1;
105
+                    }           
106
+            }
91
                 // each time through the inner loop
107
                 // each time through the inner loop
92
 
108
 
93
         return w;
109
         return w;
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
118
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103
 
119
 
104
                 // calling
120
                 // calling
105
-                w = w + 1;
121
+                for(int i=5; i<=105; i++){
122
+                    if(i>51){
123
+                        System.out.println("Hello zipcode");
124
+                    } else{
125
+                        w = w + 1;
126
+                    }
127
+                }
106
             // each time through the inner loop
128
             // each time through the inner loop
107
         
129
         
108
         return w;
130
         return w;
135
         // you need to use a .equals for two Strings.
157
         // you need to use a .equals for two Strings.
136
 
158
 
137
             // calling
159
             // calling
138
-            w = w + 1;
160
+            while(!gpsCurrentLocation().equals("Home")){
161
+                w = w + 1;
162
+                driveSomeMore();
163
+            }
139
             // each time through the inner loop
164
             // each time through the inner loop
165
+            System.out.println("Honey, I'm home");
140
         
166
         
141
 
167
 
142
             return w;
168
             return w;
156
 
182
 
157
         // do your while loop here
183
         // do your while loop here
158
  
184
  
159
-            // calling
160
-            w = w + 1;
161
-            // each time through the inner loop
185
+            while(runningScore < highestScore){
186
+                runningScore += currentScore;
187
+                currentScore = gameNextScore();
188
+                w = w + 1;
189
+            }
162
         
190
         
163
         return w; // >= 3;
191
         return w; // >= 3;
164
     }
192
     }
173
 
201
 
174
         // do your while loop here
202
         // do your while loop here
175
 
203
 
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
204
+            do{
205
+                runningScore += currentScore;
206
+                currentScore = gameNextScore();
207
+                w = w + 1;
208
+            }while(runningScore < highestScore);
179
 
209
 
180
         return w >= 3;
210
         return w >= 3;
181
     }
211
     }
189
         String adminPhoneNumber = "+1 202 456 1111";
219
         String adminPhoneNumber = "+1 202 456 1111";
190
         
220
         
191
 
221
 
192
-        // calling
193
-        w = w + 1;
194
-        // each time through the inner loop
222
+        while(serverIsRunning()){
223
+            waitFor(5);
224
+            w = w + 1;
225
+            // each time through the inner loop
226
+        }
227
+        if(!serverIsRunning()){
228
+            sendEmergencyText("Help!", adminPhoneNumber);
229
+            tryServerRestart("Help!", adminPhoneNumber);
230
+        }
195
         
231
         
196
         return w;
232
         return w;
197
     }
233
     }
201
     // and if it is, add 7 to “i”
237
     // and if it is, add 7 to “i”
202
     public int loop50by7() {
238
     public int loop50by7() {
203
         int w = 0;
239
         int w = 0;
204
-
205
-
240
+        int i = 7;
241
+        
242
+        while(i<50){
243
+            i += 7;
206
             // calling
244
             // calling
207
             w = w + 1;
245
             w = w + 1;
208
             // each time through the inner loop
246
             // each time through the inner loop
209
-        
247
+        }
210
         return w;
248
         return w;
211
     }
249
     }
212
 
250
 
240
         int sumOfThrees = 0;
278
         int sumOfThrees = 0;
241
 
279
 
242
  
280
  
281
+            for (int i=0; i<threes_array.length; i++) {
282
+            sumOfThrees = sumOfThrees + threes_array[i];
243
             // calling
283
             // calling
244
             w = w + 1;
284
             w = w + 1;
245
             // each time through the inner loop
285
             // each time through the inner loop
286
+        }
246
         
287
         
247
         System.out.print("The Sum is ");
288
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
289
         System.out.println(sumOfThrees);
255
     public int rewriteFooAsWhile() {
296
     public int rewriteFooAsWhile() {
256
         int w = 0;
297
         int w = 0;
257
         int sumOfThrees = 0;
298
         int sumOfThrees = 0;
258
-
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
299
+        int i = 0;
300
+        
301
+             while(true){
302
+                 if(i < threes_array.length){
303
+                     sumOfThrees = sumOfThrees + threes_array[i];
304
+                     // calling
305
+                     w = w + 1;
306
+                     // each time through the inner loop
307
+                    }
308
+                    else{
309
+                        break;
310
+                    }
311
+                }
263
         
312
         
264
         System.out.print("The Sum is ");
313
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
314
         System.out.println(sumOfThrees);
277
     public int manageYardAndJunior() {
326
     public int manageYardAndJunior() {
278
         int w = 0;
327
         int w = 0;
279
         boolean onTime = true;
328
         boolean onTime = true;
280
-
329
+        
281
         // ADD YOUR CODE here.
330
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
331
+        boolean yardNeedsMowed = true;
332
+        
333
+        while(isSummer()){
334
+            if(yardNeedsMowed){
335
+                yellAtJuniorToMowLawn();
336
+                // be sure to call
337
+                w = w + 1;
338
+                // each time inside the loop
339
+            }
340
+        }
341
+        sendJuniorBackToSchool("First Day of School");
286
         
342
         
287
         return w;
343
         return w;
288
     }
344
     }
296
         int w = 0;
352
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
353
         int numberOfVotes = voteTallies.length;
298
 
354
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
355
+            for(int i=0; i<numberOfVotes; i++){
356
+                System.out.println(voteTallies[i]);
357
+                // calling
358
+                w = w + 1;
359
+                // each time through the inner loop
360
+            }
303
         
361
         
304
         return w;
362
         return w;
305
     }
363
     }
310
     public int tallyVote2() {
368
     public int tallyVote2() {
311
         int w = 0;
369
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
370
         int numberOfVotes = voteTallies.length;
371
+        int idx = 0;
313
 
372
 
314
-
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
373
+            while(idx<numberOfVotes){
374
+                System.out.println(voteTallies[idx]);
375
+                idx++;
376
+                // calling
377
+                w = w + 1;
378
+                // each time through the inner loop
379
+            }
318
         
380
         
319
         return w;
381
         return w;
320
     }
382
     }