PersonHandler.java 2.5KB

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