123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
-
- public class PersonHandler {
- private final Person[] personArray;
-
- public PersonHandler(Person[] personArray) {
- this.personArray = personArray;
- }
-
- public String whileLoop() {
- //String result = "";
- // assume there is a `counter`
- // while `counter` is less than length of array
- // begin loop
-
- // use `counter` to identify the `current Person` in the array
- // get `string Representation` of `currentPerson`
- // append `stringRepresentation` to `result` variable
-
- // end loop
-
- int counter = 0;
-
- String currentPerson = "";
-
- while(counter < personArray.length){
-
- currentPerson += personArray[counter].toString();
-
- counter++;
-
- }
-
- return currentPerson;
- }
-
- //Use string builder
-
- public String forLoop() {
- String result = "";
- // identify initial value
- // identify terminal condition
- // identify increment
-
- // 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
-
- int counter = 0;
-
- StringBuilder sb = new StringBuilder();
-
-
- String currentPerson = "";
-
- for(int i = 0; i < personArray.length; i++){
-
- //currentPerson += personArray[i].toString();
-
- sb.append(personArray[i].toString());
-
-
- }
-
-
- return sb.toString();
- }
-
-
-
-
- public String forEachLoop() {
- String result = "";
- // identify array's type = person
- // identify array's variable-name = personArray
-
- // use the above discoveries to declare for-each-loop signature
- // begin loop
- // get `string Representation` of `currentPerson`
- // append `stringRepresentation` to `result` variable
- // end loop
-
- for(Person person : personArray){
-
- result += person.toString();
-
- }
-
-
- return result;
- }
-
-
- public Person[] getPersonArray() {
- return personArray;
- }
- }
|