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