PersonHandler.java 2.1KB

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