PersonHandler.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import java.lang.StringBuilder;
  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. // assume there is a `counter`
  13. int counter = 0;
  14. // while `counter` is less than length of array
  15. while(counter < personArray.length)
  16. { // begin loop
  17. // use `counter` to identify the `current Person` in the array
  18. Person currentPersonObject = personArray[counter];
  19. // get `string Representation` of `currentPerson`
  20. String currentPersonString = currentPersonObject.toString();
  21. // append `stringRepresentation` to `result` variable
  22. result.append(currentPersonString);
  23. counter++;
  24. } // end loop
  25. return result.toString();
  26. }
  27. public String forLoop() {
  28. StringBuilder result = new StringBuilder();
  29. // identify initial value
  30. //int i = 0;
  31. // identify terminal condition
  32. //i < personArray.length;
  33. // identify increment
  34. //i++
  35. // use the above clauses to declare for-loop signature
  36. for(int i=0; i<personArray.length; i++)
  37. {// begin loop
  38. // use `counter` to identify the `current Person` in the array
  39. Person currentPersonObject = personArray[i];
  40. // get `string Representation` of `currentPerson`
  41. String currentPersonString = currentPersonObject.toString();
  42. // append `stringRepresentation` to `result` variable
  43. result.append(currentPersonString);
  44. }// end loop
  45. return result.toString();
  46. }
  47. public String forEachLoop() {
  48. StringBuilder result = new StringBuilder();
  49. // identify array's type
  50. // array of type Person
  51. // identify array's variable-name
  52. // name is personArray
  53. // use the above discoveries to declare for-each-loop signature
  54. for (Person element : personArray)
  55. {// begin loop
  56. // get `string Representation` of `currentPerson`
  57. String currentPersonString = element.toString();
  58. // append `stringRepresentation` to `result` variable
  59. result.append(currentPersonString);
  60. }// end loop
  61. return result.toString();
  62. }
  63. public Person[] getPersonArray() {
  64. return personArray;
  65. }
  66. }