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