1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Created by leon on 1/24/18.
  3. */
  4. public class PersonHandler {
  5. private final Person[] personArray;
  6. Person currentPerson;
  7. StringBuilder build = new StringBuilder();
  8. public PersonHandler(Person[] personArray) {
  9. this.personArray = personArray;
  10. }
  11. public String whileLoop() {
  12. String result = "";
  13. // assume there is a `counter`
  14. int counter = 0;
  15. // while `counter` is less than length of array
  16. while(counter < personArray.length) {
  17. currentPerson = personArray[counter];
  18. result = build.append(currentPerson).toString();
  19. counter ++;
  20. }
  21. // begin loop
  22. // use `counter` to identify the `current Person` in the array
  23. // get `string Representation` of `currentPerson`
  24. // append `stringRepresentation` to `result` variable
  25. // end loop
  26. return result;
  27. }
  28. public String forLoop() {
  29. String result = "";
  30. // identify initial value
  31. // identify terminal condition
  32. // identify increment
  33. for (int i = 0; i < personArray.length; i++) {
  34. currentPerson = personArray[i];
  35. result = build.append(currentPerson).toString();
  36. }
  37. // use the above clauses to declare for-loop signature
  38. // begin loop
  39. // use `counter` to identify the `current Person` in the array
  40. // get `string Representation` of `currentPerson`
  41. // append `stringRepresentation` to `result` variable
  42. // end loop
  43. return result;
  44. }
  45. public String forEachLoop() {
  46. String result = "";
  47. // identify array's type
  48. // identify array's variable-name
  49. for (Person current : personArray) {
  50. currentPerson = current;
  51. result = build.append(currentPerson).toString();
  52. }
  53. // use the above discoveries to declare for-each-loop signature
  54. // begin loop
  55. // get `string Representation` of `currentPerson`
  56. // append `stringRepresentation` to `result` variable
  57. // end loop
  58. return result;
  59. }
  60. public Person[] getPersonArray() {
  61. return personArray;
  62. }
  63. }