1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
-
-
- /**
- * Created by leon on 1/24/18.
- */
- public class PersonHandler {
- private final Person[] personArray;
- Person currentPerson;
- StringBuilder build = new StringBuilder();
-
- public PersonHandler(Person[] personArray) {
- this.personArray = personArray;
- }
-
- public String whileLoop() {
- String result = "";
- // assume there is a `counter`
- int counter = 0;
- // while `counter` is less than length of array
- while(counter < personArray.length) {
- currentPerson = personArray[counter];
- result = build.append(currentPerson).toString();
- counter ++;
- }
-
- // begin loop
-
- // use `counter` to identify the `current Person` in the array
- // get `string Representation` of `currentPerson`
- // append `stringRepresentation` to `result` variable
-
- // end loop
- return result;
- }
-
-
-
- public String forLoop() {
- String result = "";
- // identify initial value
- // identify terminal condition
- // identify increment
- for (int i = 0; i < personArray.length; i++) {
- currentPerson = personArray[i];
- result = build.append(currentPerson).toString();
- }
-
- // use the above clauses to declare for-loop signature
- // begin loop
- // use `counter` to identify the `current Person` in the array
- // get `string Representation` of `currentPerson`
- // append `stringRepresentation` to `result` variable
- // end loop
-
- return result;
- }
-
-
-
- public String forEachLoop() {
- String result = "";
- // identify array's type
- // identify array's variable-name
- for (Person current : personArray) {
- currentPerson = current;
- result = build.append(currentPerson).toString();
- }
-
- // use the above discoveries to declare for-each-loop signature
- // begin loop
- // get `string Representation` of `currentPerson`
- // append `stringRepresentation` to `result` variable
- // end loop
-
- return result;
- }
-
-
- public Person[] getPersonArray() {
- return personArray;
- }
- }
|