Просмотр исходного кода

wrote several loop methods, moved variable initilizers in WriteIFs, wrote one if method

Margaret Pierse 8 лет назад
Родитель
Сommit
5c10a30fc2
4 измененных файлов: 133 добавлений и 98 удалений
  1. 40
    22
      WriteIFs.java
  2. 7
    7
      WriteIFsTest.java
  3. 83
    69
      WriteLoops.java
  4. 3
    0
      WriteLoopsTest.java

+ 40
- 22
WriteIFs.java Просмотреть файл

7
  */
7
  */
8
 public class WriteIFs
8
 public class WriteIFs
9
 {
9
 {
10
+    int x;
11
+    int tt_t;
12
+    int tt_s;
13
+    int oo1, oo2;
14
+    String ss;
15
+
16
+    /**
17
+     * Constructor for objects of class WriteIFs
18
+     */
19
+    public WriteIFs()
20
+    {
21
+        // initialise instance variables
22
+        x = 0;
23
+        tt_t = 0;
24
+        tt_s = 1;
25
+        ss = "";
26
+        oo1 = 61;
27
+        oo2 = 49;
28
+    }
10
 
29
 
11
-    public void playerDied(boolean player1) {
30
+    //public boolean playerDied(boolean player1) {
12
         // Write an IF statement that checks “player1.isAlive()” 
31
         // Write an IF statement that checks “player1.isAlive()” 
13
         // and if that’s false, calls “displayGameOver(player1)”
32
         // and if that’s false, calls “displayGameOver(player1)”
14
 
33
 
15
-        if (player1.isAlive(true) == false) {
16
-            displayGameOver(player1);
17
-        }
34
+        // if (player1.isAlive()) {
35
+            // return displayGameOver(player1);
36
+        // }
18
 
37
 
19
-    }
38
+    //}
20
 
39
 
21
     public String thermoSTAT(int room) {
40
     public String thermoSTAT(int room) {
22
         // Write an IF statement that checks the 
41
         // Write an IF statement that checks the 
42
     public void checkFuel(double fuelLevel) {
61
     public void checkFuel(double fuelLevel) {
43
         // Write an IF statement that checks “fuelLevel” 
62
         // Write an IF statement that checks “fuelLevel” 
44
         // and if that check is less than 0.08, calls “refuel()”
63
         // and if that check is less than 0.08, calls “refuel()”
45
-
64
+        //if(check){}
46
     }
65
     }
47
 
66
 
48
-    
49
     /**
67
     /**
50
      *  Pay no attention to the code below this point.
68
      *  Pay no attention to the code below this point.
51
      * 
69
      * 
52
      * 
70
      * 
53
      * instance variables
71
      * instance variables
54
      * / 
72
      * / 
55
-    int x;
56
-    int tt_t;
57
-    int tt_s;
58
-    int oo1, oo2;
59
-    String ss;
73
+    // int x;
74
+    // int tt_t;
75
+    // int tt_s;
76
+    // int oo1, oo2;
77
+    // String ss;
60
 
78
 
61
     /**
79
     /**
62
      * Constructor for objects of class WriteIFs
80
      * Constructor for objects of class WriteIFs
63
      */
81
      */
64
-    public WriteIFs()
65
-    {
66
-        // initialise instance variables
67
-        x = 0;
68
-        tt_t = 0;
69
-        tt_s = 1;
70
-        ss = "";
71
-        oo1 = 61;
72
-        oo2 = 49;
73
-    }
82
+    // public WriteIFs()
83
+    // {
84
+    // initialise instance variables
85
+    // x = 0;
86
+    // tt_t = 0;
87
+    // tt_s = 1;
88
+    // ss = "";
89
+    // oo1 = 61;
90
+    // oo2 = 49;
91
+    // }
74
 
92
 
75
     // associated routines
93
     // associated routines
76
     public boolean isAlive(boolean p) {
94
     public boolean isAlive(boolean p) {

+ 7
- 7
WriteIFsTest.java Просмотреть файл

43
     {
43
     {
44
     }
44
     }
45
 
45
 
46
-    @Test
47
-    public void TestIfs()
48
-    {
49
-        WriteIFs writeIFs1 = new WriteIFs();
50
-        writeIFs1.playerDied(true);
51
-        assertEquals("Game Over!", writeIFs1.ss);
52
-    }
46
+    // @Test
47
+    // public void TestIfs()
48
+    // {
49
+        // WriteIFs writeIFs1 = new WriteIFs();
50
+        // writeIFs1.playerDied(true);
51
+        // assertEquals("Game Over!", writeIFs1.ss);
52
+    // }
53
 
53
 
54
 
54
 
55
     @Test
55
     @Test

+ 83
- 69
WriteLoops.java Просмотреть файл

19
         int w = 0;
19
         int w = 0;
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
+        for (int i=0; i<5; i++){
23
             w = w + 1;
24
             w = w + 1;
24
-            // each time through the loop
25
+        }
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.
27
         return w;
29
         return w;
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=0; 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;
39
     }
43
     }
40
 
44
 
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=22; i<33; 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;
50
     }
56
     }
51
 
57
 
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;
61
     }
69
     }
62
 
70
 
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=5001; i>0; 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;
82
     }
94
     }
83
 
95
 
86
 
98
 
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
90
-                w = w + 1;
91
-                // each time through the inner loop
101
+        // calling
102
+        for (int i=0; i<20; i++){
103
+            for (int b=0; b<=4; b++){
104
+                w = w + 1;}}
105
+        // each time through the inner loop
92
 
106
 
93
         return w;
107
         return w;
94
     }
108
     }
101
         // loop index counter and if it’s greater than 51,
115
         // loop index counter and if it’s greater than 51,
102
         // prints “Hello Zipcode” instead of the statement w = w + 1;
116
         // prints “Hello Zipcode” instead of the statement w = w + 1;
103
 
117
 
104
-                // calling
105
-                w = w + 1;
106
-            // each time through the inner loop
107
-        
118
+        // calling
119
+        for (int i=0; i<150; i++){
120
+            if (i>46){System.out.println("Hello ZipCode");}
121
+            else{
122
+                w = w + 1;}
123
+        }
124
+        // each time through the inner loop
125
+
108
         return w;
126
         return w;
109
     }
127
     }
110
 
128
 
131
     // After the loop is done, print “Honey, I’m Home!”
149
     // After the loop is done, print “Honey, I’m Home!”
132
     public int driveHome() {
150
     public int driveHome() {
133
         int w = 0;
151
         int w = 0;
134
-
135
-        // you need to use a .equals for two Strings.
136
-
152
+        while (!gpsCurrentLocation().equals("Home")) {
153
+            // you need to use a .equals for two Strings.
154
+            driveSomeMore();
137
             // calling
155
             // calling
138
             w = w + 1;
156
             w = w + 1;
139
             // each time through the inner loop
157
             // each time through the inner loop
140
-        
141
-
142
-            return w;
158
+        } 
159
+        System.out.println("Honey, I'm Home!");
160
+        return w;
143
     }
161
     }
144
 
162
 
145
     // Getting harder...
163
     // Getting harder...
153
         int highestScore = 236;
171
         int highestScore = 236;
154
         int currentScore = gameNextScore();
172
         int currentScore = gameNextScore();
155
         int runningScore = 0;
173
         int runningScore = 0;
156
-
157
-        // do your while loop here
158
- 
174
+        while (runningScore<highestScore){
175
+            // do your while loop here
176
+            runningScore+= currentScore;
159
             // calling
177
             // calling
160
             w = w + 1;
178
             w = w + 1;
161
             // each time through the inner loop
179
             // each time through the inner loop
162
-        
180
+        }
163
         return w; // >= 3;
181
         return w; // >= 3;
164
     }
182
     }
165
 
183
 
173
 
191
 
174
         // do your while loop here
192
         // do your while loop here
175
 
193
 
176
-            // calling
177
-            w = w + 1;
178
-            // each time through the inner loop
194
+        // calling
195
+        w = w + 1;
196
+        // each time through the inner loop
179
 
197
 
180
         return w >= 3;
198
         return w >= 3;
181
     }
199
     }
187
     public int checkServerStatus() {
205
     public int checkServerStatus() {
188
         int w = 0;
206
         int w = 0;
189
         String adminPhoneNumber = "+1 202 456 1111";
207
         String adminPhoneNumber = "+1 202 456 1111";
190
-        
191
 
208
 
192
         // calling
209
         // calling
193
         w = w + 1;
210
         w = w + 1;
194
         // each time through the inner loop
211
         // each time through the inner loop
195
-        
212
+
196
         return w;
213
         return w;
197
     }
214
     }
198
 
215
 
202
     public int loop50by7() {
219
     public int loop50by7() {
203
         int w = 0;
220
         int w = 0;
204
 
221
 
222
+        // calling
223
+        w = w + 1;
224
+        // each time through the inner loop
205
 
225
 
206
-            // calling
207
-            w = w + 1;
208
-            // each time through the inner loop
209
-        
210
         return w;
226
         return w;
211
     }
227
     }
212
 
228
 
239
         int w = 0;
255
         int w = 0;
240
         int sumOfThrees = 0;
256
         int sumOfThrees = 0;
241
 
257
 
242
- 
243
-            // calling
244
-            w = w + 1;
245
-            // each time through the inner loop
246
-        
258
+        // calling
259
+        w = w + 1;
260
+        // each time through the inner loop
261
+
247
         System.out.print("The Sum is ");
262
         System.out.print("The Sum is ");
248
         System.out.println(sumOfThrees);
263
         System.out.println(sumOfThrees);
249
 
264
 
256
         int w = 0;
271
         int w = 0;
257
         int sumOfThrees = 0;
272
         int sumOfThrees = 0;
258
 
273
 
259
- 
260
-            // calling
261
-            w = w + 1;
262
-            // each time through the inner loop
263
-        
274
+        // calling
275
+        w = w + 1;
276
+        // each time through the inner loop
277
+
264
         System.out.print("The Sum is ");
278
         System.out.print("The Sum is ");
265
         System.out.println(sumOfThrees);
279
         System.out.println(sumOfThrees);
266
 
280
 
279
         boolean onTime = true;
293
         boolean onTime = true;
280
 
294
 
281
         // ADD YOUR CODE here.
295
         // ADD YOUR CODE here.
282
- 
283
-            // be sure to call
284
-            w = w + 1;
285
-            // each time inside the loop
286
-        
296
+
297
+        // be sure to call
298
+        w = w + 1;
299
+        // each time inside the loop
300
+
287
         return w;
301
         return w;
288
     }
302
     }
289
 
303
 
290
-    String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams", "Lincoln",
304
+    String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington", 
305
+            "Adams", "Lincoln",
291
             "Washington", "Adams", "Lincoln", "Washington", "Adams", "Roosevelt" };
306
             "Washington", "Adams", "Lincoln", "Washington", "Adams", "Roosevelt" };
292
 
307
 
293
     // Given an array voteTallies[], write a FOR loop that prints out each value in
308
     // Given an array voteTallies[], write a FOR loop that prints out each value in
296
         int w = 0;
311
         int w = 0;
297
         int numberOfVotes = voteTallies.length;
312
         int numberOfVotes = voteTallies.length;
298
 
313
 
299
- 
300
-            // calling
301
-            w = w + 1;
302
-            // each time through the inner loop
303
-        
314
+        // calling
315
+        w = w + 1;
316
+        // each time through the inner loop
317
+
304
         return w;
318
         return w;
305
     }
319
     }
306
 
320
 
311
         int w = 0;
325
         int w = 0;
312
         int numberOfVotes = voteTallies.length;
326
         int numberOfVotes = voteTallies.length;
313
 
327
 
328
+        // calling
329
+        w = w + 1;
330
+        // each time through the inner loop
314
 
331
 
315
-            // calling
316
-            w = w + 1;
317
-            // each time through the inner loop
318
-        
319
         return w;
332
         return w;
320
     }
333
     }
321
 
334
 
378
     //         return (i >= 3);
391
     //         return (i >= 3);
379
     //     };
392
     //     };
380
     // };
393
     // };
381
-        private int summer = 0;
382
-        private boolean isSummer() {
383
-            if (summer == 3) {
384
-                return true;
385
-            }
386
-            summer++;
387
-            return false;
394
+    private int summer = 0;
395
+    private boolean isSummer() {
396
+        if (summer == 3) {
397
+            return true;
388
         }
398
         }
399
+        summer++;
400
+        return false;
401
+    }
402
+
389
     private void sendEmergencyText(String mesg, String phone) {
403
     private void sendEmergencyText(String mesg, String phone) {
390
     }
404
     }
391
 
405
 

+ 3
- 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
+    
127
 }
129
 }
128
 
130
 
129
 
131
 
136
 
138
 
137
 
139
 
138
 
140
 
141
+