lots of exercises in java... from https://github.com/exercism/java

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class House {
  2. private static final String[] CHARACTERS = {
  3. "house that Jack built.",
  4. "malt",
  5. "rat",
  6. "cat",
  7. "dog",
  8. "cow with the crumpled horn",
  9. "maiden all forlorn",
  10. "man all tattered and torn",
  11. "priest all shaven and shorn",
  12. "rooster that crowed in the morn",
  13. "farmer sowing his corn",
  14. "horse and the hound and the horn"
  15. };
  16. private static final String[] ACTIONS = {
  17. "lay in",
  18. "ate",
  19. "killed",
  20. "worried",
  21. "tossed",
  22. "milked",
  23. "kissed",
  24. "married",
  25. "woke",
  26. "kept",
  27. "belonged to"
  28. };
  29. String verse(int verseNumber) {
  30. StringBuilder verse = new StringBuilder();
  31. verse.append("This is the " + CHARACTERS[verseNumber - 1]);
  32. for (int i = verseNumber - 2; i >= 0; i--) {
  33. verse.append("\nthat " + ACTIONS[i] + " the " + CHARACTERS[i]);
  34. }
  35. return verse.toString();
  36. }
  37. String verses(int startVerse, int endVerse) {
  38. String[] verses = new String[endVerse - startVerse + 1];
  39. for (int i = startVerse; i <= endVerse; i++) {
  40. verses[i - startVerse] = verse(i);
  41. }
  42. return String.join("\n\n", verses);
  43. }
  44. String sing() {
  45. return verses(1, 12);
  46. }
  47. }