PersonHandler.java 2.0KB

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