GNU Java Training Wheels 1.1: Tutorial 5 A beer drinking song

Next: Tutorial 6 Class variables, Previous: Tutorial 4 Four looping constructs, Up: J.T.W. Tutorials [Contents][Index]

### 4.5 Tutorial 5 A beer drinking song

Question 4.5.1: Study the following code and then compile and run it to verify that it prints out the lyrics to a popular song:

    class BeerSong
    begin
        beginMain
            System.out.println("Five bottles of beer on the wall.");
            System.out.println("Five bottles of beer on the wall.");
            System.out.println("If one bottle of beer should accidentally fall,");
            System.out.println("there'd be four bottles of beer on the wall.");
            System.out.println();
            System.out.println("Four bottles of beer on the wall.");
            System.out.println("Four bottles of beer on the wall.");
            System.out.println("If one bottle of beer should accidentally fall,");
            System.out.println("there'd be three bottles of beer on the wall.");
            System.out.println();
            System.out.println("Three bottles of beer on the wall.");
            System.out.println("Three bottles of beer on the wall.");
            System.out.println("If one bottle of beer should accidentally fall,");
            System.out.println("there'd be two bottles of beer on the wall.");
            System.out.println();
            System.out.println("Two bottles of beer on the wall.");
            System.out.println("Two bottles of beer on the wall.");
            System.out.println("If one bottle of beer should accidentally fall,");
            System.out.println("There'd be one bottle of beer on the wall.");
            System.out.println();
            System.out.println("One bottle of beer on the wall.");
            System.out.println("One bottle of beer on the wall.");
            System.out.println("If one bottle of beer should accidentally fall,");
            System.out.println("there'd be no bottles of beer on the wall.");
            System.out.println();
        endMain
    end

Question 4.5.2: The following is the first attempt to make the code smaller but to keep the same output: If you compile and run the following code you will notice that it counts up from one rather than down from n. Change the for loop so that it runs down rather than up. For information about how to write the for loop, please consult Tutorial 4.2.

    class BeerSong
    begin
        function song(int n)
        begin
            superfor (var int i=1 to n)
            begin
                System.out.println(i + " bottles of beer on the wall");
                System.out.println(i + " bottles of beer on the wall");
                System.out.println("If one bottle of beer should accidentally fall,");
                System.out.println("there'd be " + (i-1) + " bottles of beer on the wall");
                System.out.println();
            end
        end

        beginMain
            song(5);
        endMain
    end

Question 4.5.3: Finish the `number2string` `function` below and add a new `function` call to this `function` in the song `function` so that it print textual numbers rather than digits.

    function String number2string(int n)
    begin
        assert n>=0 : n;
        assert n<=10: n;
        if (n == 0) then return "no";
        if (n == 1) then return "one";
        if (n == 2) then return "two";
        /* rest of code goes here */
        if (n == 9) then return "nine";
        if (n == 10) then return "ten";
        assert false;
    end

Question 4.5.4: Add a new `function` `String capitalize(String s)` that capitalizes the first word in a `String` and call this `function` from the song `function` so that the first words in each sentence are capitalized. You should find the `function` `Character.toUpperCase` and the methods `charAt` and `substring` in the package `java.lang` helpful for writing this `function`. See the `class` `String` in the `package` `java.lang` at http://docs.oracle.com/javase/1.5.0/docs/api for more details.

Question 4.5.5: Add new `function` call `String plural(int n)` that returns the string "s" if n is not equal to 1 and the empty string "" otherwise. Then call this `function` from the song `function` so that the phrase "bottle" is pluralized when it should be.

Question 4.5.6: Write a `function` called `number2string2` that can handle values up to but not including 100. Note that you will need multiple `if` ... `then` statements to achieve this. Note that if `n` is a number then the following expressions are useful:

         var int temp1 = n / 10 % 10 results in temp1 holding the tens digit of n and is zero in the case that n<10.
         var int temp2 = n % 10 results in temp2 holding the ones digit of n.

Also make it print out "one hundred or more" in the case that n>=100

Question 4.5.7: Change the song `function` so that the following `function` call: `song(5,"rum");` in the `main` `function` results in the following printout:

    Five bottles of rum on the wall.

    ...

    there'd be no bottles of rum on the wall.

Question 4.5.8: Once all the code is working, add the following line to the `main` `function`: `song(100,"gin");` so that it prints out the following:

    One hundred bottles of gin on the wall.

    ...

    there'd be zero bottles of gin on the wall.

Question 4.5.9: Write a new `function` `number2string3` that works like `number2string2` and `number2string` except that it handles numbers up to 999. Internally `number2string3` should call `number2string2`. You might find the following `function` useful:

    function String textand(String a, String b)
    begin
        if (a.equals("") or b.equals("")) then return a + b;
        else return a + " and " + b;
    end

Question 4.5.10: Tricky! Write a new `function` `number2string4` that works like number2string3 execpt that it handles numbers up to nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine, i.e. 999,999,999. The `function` number2string4 should internally call number2string3 like so:

    var String ones = number2string3(n % 1000);
    var String thousands = number2string3(n / 1000 % 1000);
    var String millions = number2string3(n / 1000 / 1000 % 1000);

Note that the variables above will have values from 0 to 999 inclusive.

Next: Tutorial 6 Class variables, Previous: Tutorial 4 Four looping constructs, Up: J.T.W. Tutorials [Contents][Index]