Browse Source

lab completed

ThuyKhong 6 years ago
parent
commit
f8e0e666c5
1 changed files with 19 additions and 9 deletions
  1. 19
    9
      src/main/java/com/zipcodewilmington/PersonHandler.java

+ 19
- 9
src/main/java/com/zipcodewilmington/PersonHandler.java View File

11
     }
11
     }
12
 
12
 
13
     public String whileLoop() {
13
     public String whileLoop() {
14
-        String result = "";
14
+        StringBuilder stringResult = new StringBuilder();
15
         // assume there is a `counter`
15
         // assume there is a `counter`
16
         // while `counter` is less than length of array
16
         // while `counter` is less than length of array
17
             // begin loop
17
             // begin loop
18
+        int i = 0;
19
+
20
+       while(i<personArray.length) {
21
+           stringResult.append(personArray[i].toString());
22
+           i++;
23
+       }
18
 
24
 
19
                 // use `counter` to identify the `current Person` in the array
25
                 // use `counter` to identify the `current Person` in the array
20
                 // get `string Representation` of `currentPerson`
26
                 // get `string Representation` of `currentPerson`
21
                 // append `stringRepresentation` to `result` variable
27
                 // append `stringRepresentation` to `result` variable
22
 
28
 
23
             // end loop
29
             // end loop
24
-        return result;
30
+        return stringResult.toString();
25
     }
31
     }
26
 
32
 
27
 
33
 
28
 
34
 
29
     public String forLoop() {
35
     public String forLoop() {
30
-        String result = "";
31
         // identify initial value
36
         // identify initial value
32
         // identify terminal condition
37
         // identify terminal condition
33
         // identify increment
38
         // identify increment
39
+        StringBuilder stringResult = new StringBuilder();
40
+        for (int i = 0; i <personArray.length ; i++) {
41
+            stringResult.append(personArray[i].toString());
42
+        }
34
 
43
 
35
         // use the above clauses to declare for-loop signature
44
         // use the above clauses to declare for-loop signature
36
             // begin loop
45
             // begin loop
39
                 // append `stringRepresentation` to `result` variable
48
                 // append `stringRepresentation` to `result` variable
40
             // end loop
49
             // end loop
41
 
50
 
42
-        return result;
51
+        return stringResult.toString();
43
     }
52
     }
44
 
53
 
45
 
54
 
46
 
55
 
47
     public String forEachLoop() {
56
     public String forEachLoop() {
48
-        String result = "";
49
-        // identify array's type
50
-        // identify array's variable-name
51
-
57
+        StringBuilder stringResult = new StringBuilder();
58
+        // identify array's type (type of data)
59
+        // identify array's variable-name (variable name you want to call that data)
60
+        for (Person name : personArray)
61
+            stringResult.append(name.toString());
52
         // use the above discoveries to declare for-each-loop signature
62
         // use the above discoveries to declare for-each-loop signature
53
             // begin loop
63
             // begin loop
54
                 // get `string Representation` of `currentPerson`
64
                 // get `string Representation` of `currentPerson`
55
                 // append `stringRepresentation` to `result` variable
65
                 // append `stringRepresentation` to `result` variable
56
             // end loop
66
             // end loop
57
 
67
 
58
-        return result;
68
+        return stringResult.toString();
59
     }
69
     }
60
 
70
 
61
 
71