Avatar of Kelly-Ryan

Kelly-Ryan's solution

to Food Chain in the Java Track

Published at Sep 26 2019 · 0 comments
Instructions
Test suite
Solution

Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.

While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.

This is a cumulative song of unknown origin.

This is one of many common variants.

I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a horse.
She's dead, of course!

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Wikipedia http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

FoodChainTest.java

import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class FoodChainTest {
    private FoodChain foodChain;

    @Before
    public void setup() {
        foodChain = new FoodChain();
    }

    @Test
    public void fly() {
        int verse = 1;
        String expected = "I know an old lady who swallowed a fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void spider() {
        int verse = 2;
        String expected = "I know an old lady who swallowed a spider.\n" +
                          "It wriggled and jiggled and tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void bird() {
        int verse = 3;
        String expected = "I know an old lady who swallowed a bird.\n" +
                          "How absurd to swallow a bird!\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void cat() {
        int verse = 4;
        String expected = "I know an old lady who swallowed a cat.\n" +
                          "Imagine that, to swallow a cat!\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }


    @Test
    @Ignore("Remove to run test.")
    public void dog() {
        int verse = 5;
        String expected = "I know an old lady who swallowed a dog.\n" +
                          "What a hog, to swallow a dog!\n" +
                          "She swallowed the dog to catch the cat.\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void goat() {
        int verse = 6;
        String expected = "I know an old lady who swallowed a goat.\n" +
                          "Just opened her throat and swallowed a goat!\n" +
                          "She swallowed the goat to catch the dog.\n" +
                          "She swallowed the dog to catch the cat.\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void cow() {
        int verse = 7;
        String expected = "I know an old lady who swallowed a cow.\n" +
                          "I don't know how she swallowed a cow!\n" +
                          "She swallowed the cow to catch the goat.\n" +
                          "She swallowed the goat to catch the dog.\n" +
                          "She swallowed the dog to catch the cat.\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verse(verse));
    }

    @Test
    @Ignore("Remove to run test.")
    public void horse() {
        int verse = 8;
        String expected = "I know an old lady who swallowed a horse.\n" +
                          "She's dead, of course!";

        assertEquals(expected, foodChain.verse(verse));
    }


    @Test
    @Ignore("Remove to run test.")
    public void multipleVerses() {
        int startVerse = 1;
        int endVerse = 3;
        String expected = "I know an old lady who swallowed a fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a spider.\n" +
                          "It wriggled and jiggled and tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a bird.\n" +
                          "How absurd to swallow a bird!\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.";

        assertEquals(expected, foodChain.verses(startVerse, endVerse));
    }


    @Test
    @Ignore("Remove to run test.")
    public void wholeSong() {
        int startVerse = 1;
        int endVerse = 8;
        String expected = "I know an old lady who swallowed a fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a spider.\n" +
                          "It wriggled and jiggled and tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a bird.\n" +
                          "How absurd to swallow a bird!\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a cat.\n" +
                          "Imagine that, to swallow a cat!\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a dog.\n" +
                          "What a hog, to swallow a dog!\n" +
                          "She swallowed the dog to catch the cat.\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a goat.\n" +
                          "Just opened her throat and swallowed a goat!\n" +
                          "She swallowed the goat to catch the dog.\n" +
                          "She swallowed the dog to catch the cat.\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a cow.\n" +
                          "I don't know how she swallowed a cow!\n" +
                          "She swallowed the cow to catch the goat.\n" +
                          "She swallowed the goat to catch the dog.\n" +
                          "She swallowed the dog to catch the cat.\n" +
                          "She swallowed the cat to catch the bird.\n" +
                          "She swallowed the bird to catch the spider that wriggled and jiggled and " +
                          "tickled inside her.\n" +
                          "She swallowed the spider to catch the fly.\n" +
                          "I don't know why she swallowed the fly. Perhaps she'll die.\n" +
                          "\n" +
                          "I know an old lady who swallowed a horse.\n" +
                          "She's dead, of course!";

        assertEquals(expected, foodChain.verses(startVerse, endVerse));
    }
}
import java.util.Map;
import java.util.HashMap;

class FoodChain {

    String verse(int verseNo){

        StringBuilder song = new StringBuilder();

        Map<Integer, String> map = new HashMap<Integer, String>(8);
        map.put(1, "fly");
        map.put(2, "spider");
        map.put(3, "bird");
        map.put(4, "cat");
        map.put(5, "dog");
        map.put(6, "goat");
        map.put(7, "cow");
        map.put(8, "horse");

        song.append("I know an old lady who swallowed a " + map.get(verseNo) + ".\n");   //first line

        switch(verseNo){
            case 8:
                song.append("She's dead, of course!");
                return song.toString();
            case 7:
                if (verseNo == 7){
                    song.append("I don't know how she swallowed a cow!\n" + "She swallowed the " + map.get(verseNo) + " to catch the " + map.get(verseNo - 1) + ".\n");
                } else {
                    song.append("She swallowed the cow to catch the dog.\n");
                }
            case 6:
                if (verseNo == 6){
                    song.append("Just opened her throat and swallowed a goat!\n" + "She swallowed the " + map.get(verseNo) + " to catch the " + map.get(verseNo - 1) + ".\n");
                } else {
                    song.append("She swallowed the goat to catch the dog.\n");
                }
            case 5:
                if (verseNo == 5){
                    song.append("What a hog, to swallow a dog!\n" + "She swallowed the " + map.get(verseNo) + " to catch the " + map.get(verseNo - 1) + ".\n");
                } else {
                    song.append("She swallowed the dog to catch the cat.\n");
                }
            case 4:
                if (verseNo == 4){
                    song.append("Imagine that, to swallow a cat!\n" + "She swallowed the " + map.get(verseNo) + " to catch the " + map.get(verseNo - 1) + ".\n");
                } else {
                    song.append("She swallowed the cat to catch the bird.\n");
                }
            case 3:
                if (verseNo == 3){
                    song.append("How absurd to swallow a bird!\n" + "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n");
                } else{
                    song.append("She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n");
                }
            case 2:
                if (verseNo == 2) {
                    song.append("It wriggled and jiggled and tickled inside her.\n" + "She swallowed the spider to catch the fly.\n");
                } else {
                    song.append("She swallowed the spider to catch the fly.\n");
                }
        }

        song.append("I don't know why she swallowed the fly. Perhaps she'll die."); //last line

        return song.toString();
    }

    String verses(int startVerse, int endVerse){

        StringBuilder song = new StringBuilder();

        for (int i = startVerse; i <= endVerse; i++){
            song.append(verse(i) + "\n\n");
        }
        return song.toString().trim();
    }

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?