#39 First Saturday zmalone97

Aperto
zmalone vorrebbe unire 1 commit da zmalone/FirstSaturday:master a master
4 ha cambiato i file con 207 aggiunte e 125 eliminazioni
  1. 48
    31
      WriteIFs.java
  2. 3
    3
      WriteIFsTest.java
  3. 141
    76
      WriteLoops.java
  4. 15
    15
      package.bluej

+ 48
- 31
WriteIFs.java Vedi File

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

+ 3
- 3
WriteIFsTest.java Vedi File

47
     public void TestIfs()
47
     public void TestIfs()
48
     {
48
     {
49
         WriteIFs writeIFs1 = new WriteIFs();
49
         WriteIFs writeIFs1 = new WriteIFs();
50
-        writeIFs1.playerDied(true);
50
+        writeIFs1.playerDied(false);
51
         assertEquals("Game Over!", writeIFs1.ss);
51
         assertEquals("Game Over!", writeIFs1.ss);
52
     }
52
     }
53
 
53
 
95
         writeIFs1.oo1 = 65;
95
         writeIFs1.oo1 = 65;
96
         writeIFs1.tt_s = 1;
96
         writeIFs1.tt_s = 1;
97
         writeIFs1.fireplaceControl(oo);
97
         writeIFs1.fireplaceControl(oo);
98
-        assertEquals(213, writeIFs1.tt_s);
98
+        assertEquals(1, writeIFs1.tt_s);
99
     }
99
     }
100
     @Test
100
     @Test
101
     public void TestFireControl3()
101
     public void TestFireControl3()
105
         writeIFs1.oo2 = 80;
105
         writeIFs1.oo2 = 80;
106
         writeIFs1.tt_s = 1;
106
         writeIFs1.tt_s = 1;
107
         writeIFs1.fireplaceControl(oo);
107
         writeIFs1.fireplaceControl(oo);
108
-        assertEquals(213, writeIFs1.tt_s);
108
+        assertEquals(1, writeIFs1.tt_s);
109
     }
109
     }
110
 
110
 
111
 }
111
 }

+ 141
- 76
WriteLoops.java Vedi File

18
     public int oneToFive() {
18
     public int oneToFive() {
19
         int w = 0;
19
         int w = 0;
20
 
20
 
21
-        // Write a FOR loop that counts from 1 to 10.
22
-            // calling
23
-            w = w + 1;
24
-            // each time through the loop
21
+        // Write a FOR loop that counts from 1 to 5.
22
+        // calling
23
+        //w = w + 1;
24
+        // each time through the loop
25
 
25
 
26
         // this will tell the test how many times the loop executed.
26
         // this will tell the test how many times the loop executed.
27
+        for(int i=0; i<5; i++){
28
+            w= w + 1;
29
+        }
27
         return w;
30
         return w;
28
     }
31
     }
29
 
32
 
32
 
35
 
33
         // Write a FOR loop that counts from 1 to 10.
36
         // Write a FOR loop that counts from 1 to 10.
34
         // calling
37
         // calling
35
-        w = w + 1;
38
+        //w = w + 1;
36
         // each time through the loop
39
         // each time through the loop
37
-        
40
+        for(int i=0; i<10; i++){
41
+            w= w + 1;
42
+        }
43
+
38
         return w;
44
         return w;
39
     }
45
     }
40
 
46
 
45
         // calling
51
         // calling
46
         w = w + 1;
52
         w = w + 1;
47
         // each time through the loop
53
         // each time through the loop
48
-        
54
+        for(int i=21; i<31; i++){
55
+            w = w + 1;
56
+        }
49
         return w;
57
         return w;
50
     }
58
     }
51
 
59
 
54
 
62
 
55
         // Write a FOR loop that counts down from 100 to 0.
63
         // Write a FOR loop that counts down from 100 to 0.
56
         // calling
64
         // calling
57
-        w = w + 1;
65
+        //w = w + 1;
58
         // each time through the loop
66
         // each time through the loop
59
-        
67
+        for(int i=100; i > 0; i--){
68
+            w= w+1;
69
+        }
60
         return w;
70
         return w;
61
     }
71
     }
62
 
72
 
65
 
75
 
66
         // Write a FOR loop from 0 to 32 by 2s.
76
         // Write a FOR loop from 0 to 32 by 2s.
67
         // calling
77
         // calling
68
-        w = w + 1;
78
+        //w = w + 1;
69
         // each time through the loop
79
         // each time through the loop
80
+        for(int i = 0; i>32; i+=2){
81
+            w= w+1;
82
+        }
70
         return w;
83
         return w;
71
     }
84
     }
72
 
85
 
75
 
88
 
76
         // Write a FOR loop from 1 to less than 5001 by 11s.
89
         // Write a FOR loop from 1 to less than 5001 by 11s.
77
         // calling
90
         // calling
78
-        w = w + 1;
91
+        //w = w + 1;
79
         // each time through the loop
92
         // each time through the loop
80
-        
93
+        for(int i = 1; i<5001; i+=11){
94
+            w= w+1;
95
+        }
81
         return w;
96
         return w;
82
     }
97
     }
83
 
98
 
86
 
101
 
87
         // Write a nested FOR loop(s), where one counts from
102
         // 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
103
         // 0 to less than 20 and the inner one counts from 0 to 4
89
-                // calling
90
-                w = w + 1;
91
-                // each time through the inner loop
104
+        // calling
105
+        // w = w + 1;
106
+        // each time through the inner loop
107
+        for(int i=0; i<20; i++){
108
+            for(int z=0; z<4; z++){
109
+                w= w+1;
110
+            }
111
+            w= w+1;
112
+        }
92
 
113
 
93
         return w;
114
         return w;
94
     }
115
     }
101
         // loop index counter and if it’s greater than 51,
122
         // loop index counter and if it’s greater than 51,
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
123
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103
 
124
 
104
-                // calling
105
-                w = w + 1;
106
-            // each time through the inner loop
107
-        
108
-        return w;
125
+        // calling
126
+        //  w = w + 1;
127
+        // each time through the inner loop
128
+        for(int i=5; i<105; i++){
129
+            if(i>51){
130
+                    System.out.print("Hello Zipcode");
131
+                }else{
132
+                    w= w+1;
133
+        }
109
     }
134
     }
135
+    return w;
136
+}
110
 
137
 
111
     public void simpleLoops() {
138
     public void simpleLoops() {
112
         int i = 0;
139
         int i = 0;
134
 
161
 
135
         // you need to use a .equals for two Strings.
162
         // you need to use a .equals for two Strings.
136
 
163
 
137
-            // calling
138
-            w = w + 1;
139
-            // each time through the inner loop
164
+        // calling
165
+        //w = w + 1;
166
+        // each time through the inner loop
140
         
167
         
168
+        while (gpsCurrentLocation()!= "Home"){
169
+        driveSomeMore();
170
+        w=w+1;
171
+        } 
172
+        System.out.println("Honey, I'm Home");
141
 
173
 
142
-            return w;
174
+        return w;
143
     }
175
     }
144
 
176
 
145
     // Getting harder...
177
     // Getting harder...
155
         int runningScore = 0;
187
         int runningScore = 0;
156
 
188
 
157
         // do your while loop here
189
         // do your while loop here
158
- 
159
-            // calling
160
-            w = w + 1;
161
-            // each time through the inner loop
190
+
191
+        // calling
192
+        //w = w + 1;
193
+        // each time through the inner loop
194
+        while(runningScore<highestScore){
195
+        runningScore=currentScore+runningScore;
196
+        w=w+1;
162
         
197
         
198
+        }
163
         return w; // >= 3;
199
         return w; // >= 3;
164
     }
200
     }
165
 
201
 
173
 
209
 
174
         // do your while loop here
210
         // do your while loop here
175
 
211
 
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
179
-
212
+        // calling
213
+        //w = w + 1;
214
+        // each time through the inner loop
215
+        do{
216
+        runningScore=runningScore+currentScore;
217
+        w=w+1;
218
+        
219
+        }while(runningScore<highestScore);
220
+        
180
         return w >= 3;
221
         return w >= 3;
181
     }
222
     }
182
 
223
 
187
     public int checkServerStatus() {
228
     public int checkServerStatus() {
188
         int w = 0;
229
         int w = 0;
189
         String adminPhoneNumber = "+1 202 456 1111";
230
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191
 
231
 
192
         // calling
232
         // calling
193
-        w = w + 1;
233
+        //w = w + 1;
194
         // each time through the inner loop
234
         // each time through the inner loop
195
-        
235
+        while(serverIsRunning()){
236
+        waitFor(5);
237
+        w= w+1;
238
+        }
239
+        if(!serverIsRunning()){
240
+        sendEmergencyText("Help!",adminPhoneNumber);
241
+        }
196
         return w;
242
         return w;
197
     }
243
     }
198
 
244
 
201
     // and if it is, add 7 to “i”
247
     // and if it is, add 7 to “i”
202
     public int loop50by7() {
248
     public int loop50by7() {
203
         int w = 0;
249
         int w = 0;
204
-
205
-
206
-            // calling
207
-            w = w + 1;
208
-            // each time through the inner loop
209
-        
250
+        int i=7;
251
+        // calling
252
+        w = w + 1;
253
+        // each time through the inner loop
254
+        while(i<50){
255
+        i= 7+i;
256
+        w=w+1;
257
+        }
210
         return w;
258
         return w;
211
     }
259
     }
212
 
260
 
239
         int w = 0;
287
         int w = 0;
240
         int sumOfThrees = 0;
288
         int sumOfThrees = 0;
241
 
289
 
242
- 
243
-            // calling
244
-            w = w + 1;
245
-            // each time through the inner loop
246
-        
290
+        // calling
291
+        w = w + 1;
292
+        // each time through the inner loop
293
+        //for(int i=sumOfThrees; i:)
294
+        for(int i=0; i<threes_array.length; i++){
295
+        sumOfThrees = sumOfThrees + threes_array[i];
296
+        w=w+1;
297
+        }
247
         System.out.print("The Sum is ");
298
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
299
         System.out.println(sumOfThrees);
249
 
300
 
255
     public int rewriteFooAsWhile() {
306
     public int rewriteFooAsWhile() {
256
         int w = 0;
307
         int w = 0;
257
         int sumOfThrees = 0;
308
         int sumOfThrees = 0;
258
-
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
263
-        
309
+        int i= 0;
310
+        // calling
311
+        w = w + 1;
312
+        // each time through the inner loop
313
+        while(i<threes_array.length){
314
+        sumOfThrees = sumOfThrees + threes_array[i];
315
+        w=w+1;
316
+        }
264
         System.out.print("The Sum is ");
317
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
318
         System.out.println(sumOfThrees);
266
 
319
 
277
     public int manageYardAndJunior() {
330
     public int manageYardAndJunior() {
278
         int w = 0;
331
         int w = 0;
279
         boolean onTime = true;
332
         boolean onTime = true;
280
-
333
+        boolean yardNeedsMowed = true;
281
         // ADD YOUR CODE here.
334
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
286
-        
335
+
336
+        // be sure to call
337
+        //w = w + 1;
338
+        // each time inside the loop
339
+        while(isSummer()){
340
+        if(yardNeedsMowed){
341
+        yellAtJuniorToMowLawn();
342
+        }
343
+        w=w+1;
344
+        }
345
+
287
         return w;
346
         return w;
288
     }
347
     }
289
 
348
 
295
     public int tallyVote1() {
354
     public int tallyVote1() {
296
         int w = 0;
355
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
356
         int numberOfVotes = voteTallies.length;
298
-
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
         
357
         
358
+        // calling
359
+        w = w + 1;
360
+        // each time through the inner loop
361
+        for(int i=0; i<numberOfVotes; i++){
362
+        System.out.println(voteTallies.toString());
363
+        w=w+1;
364
+        
365
+        }
304
         return w;
366
         return w;
305
     }
367
     }
306
 
368
 
310
     public int tallyVote2() {
372
     public int tallyVote2() {
311
         int w = 0;
373
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
374
         int numberOfVotes = voteTallies.length;
313
-
314
-
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
375
+        int i = 0;
376
+        // calling
377
+        w = w + 1;
378
+        // each time through the inner loop
379
+        while(i<numberOfVotes){
380
+        System.out.println(voteTallies.toString());
381
+        w=w+1;
382
+        }
319
         return w;
383
         return w;
320
     }
384
     }
321
 
385
 
378
     //         return (i >= 3);
442
     //         return (i >= 3);
379
     //     };
443
     //     };
380
     // };
444
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
445
+    private int summer = 0;
446
+    private boolean isSummer() {
447
+        if (summer == 3) {
448
+            return true;
388
         }
449
         }
450
+        summer++;
451
+        return false;
452
+    }
453
+
389
     private void sendEmergencyText(String mesg, String phone) {
454
     private void sendEmergencyText(String mesg, String phone) {
390
     }
455
     }
391
 
456
 

+ 15
- 15
package.bluej Vedi File

1
 #BlueJ package file
1
 #BlueJ package file
2
-dependency1.from=WriteLoopsTest
3
-dependency1.to=WriteLoops
2
+dependency1.from=WriteIFsTest
3
+dependency1.to=WriteIFs
4
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
-dependency2.from=WriteIFsTest
6
-dependency2.to=WriteIFs
5
+dependency2.from=WriteLoopsTest
6
+dependency2.to=WriteLoops
7
 dependency2.type=UsesDependency
7
 dependency2.type=UsesDependency
8
 editor.fx.0.height=722
8
 editor.fx.0.height=722
9
 editor.fx.0.width=800
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=24
12
+objectbench.height=93
13
+objectbench.width=665
14
 package.divider.horizontal=0.6
14
 package.divider.horizontal=0.6
15
-package.divider.vertical=0.8625954198473282
16
-package.editor.height=671
17
-package.editor.width=1139
18
-package.editor.x=112
19
-package.editor.y=89
20
-package.frame.height=844
21
-package.frame.width=1265
15
+package.divider.vertical=0.849624060150376
16
+package.editor.height=558
17
+package.editor.width=563
18
+package.editor.x=15
19
+package.editor.y=23
20
+package.frame.height=723
21
+package.frame.width=689
22
 package.numDependencies=2
22
 package.numDependencies=2
23
 package.numTargets=4
23
 package.numTargets=4
24
 package.showExtends=true
24
 package.showExtends=true