BO(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:

@cindex Beer drinking song

@example
CLASS BeerSong
BEGIN
PD BEGINMAIN
PD PD System.out.println("Five bottles of beer on the wall.");
PD PD System.out.println("Five bottles of beer on the wall.");
PD PD System.out.println("If one bottle of beer should accidentally fall,");
PD PD System.out.println("there'd be four bottles of beer on the wall.");
PD PD System.out.println();
PD PD System.out.println("Four bottles of beer on the wall.");
PD PD System.out.println("Four bottles of beer on the wall.");
PD PD System.out.println("If one bottle of beer should accidentally fall,");
PD PD System.out.println("there'd be three bottles of beer on the wall.");
PD PD System.out.println();
PD PD System.out.println("Three bottles of beer on the wall.");
PD PD System.out.println("Three bottles of beer on the wall.");
PD PD System.out.println("If one bottle of beer should accidentally fall,");
PD PD System.out.println("there'd be two bottles of beer on the wall.");
PD PD System.out.println();
PD PD System.out.println("Two bottles of beer on the wall.");
PD PD System.out.println("Two bottles of beer on the wall.");
PD PD System.out.println("If one bottle of beer should accidentally fall,");
PD PD System.out.println("There'd be one bottle of beer on the wall.");
PD PD System.out.println();
PD PD System.out.println("One bottle of beer on the wall.");
PD PD System.out.println("One bottle of beer on the wall.");
PD PD System.out.println("If one bottle of beer should accidentally fall,");
PD PD System.out.println("there'd be no bottles of beer on the wall.");
PD PD System.out.println();
PD ENDMAIN
END
@end example

BO(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.

@example
CLASS BeerSong
BEGIN
PD FUNCTION song(int n)
PD BEGIN
PD PD SUPERFOR (VAR int i=1 TU n)
PD PD BEGIN
PD PD PD System.out.println(i + " bottles of beer on the wall");
PD PD PD System.out.println(i + " bottles of beer on the wall");
PD PD PD System.out.println("If one bottle of beer should accidentally fall,");
PD PD PD System.out.println("there'd be " + (i-1) + " bottles of beer on the wall");
PD PD PD System.out.println();
PD PD END
PD END

PD BEGINMAIN
PD PD song(5);
PD ENDMAIN
END
@end example

BO(Question 4.5.3:) Finish the TT(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.

@example
FUNCTION String number2string(int n)
BEGIN
PD ASSERT n>=0 : n;
PD ASSERT n<=10: n;
PD if (n == 0) then return "no";
PD if (n == 1) then return "one";
PD if (n == 2) then return "two";
PD /* rest of code goes here */
PD if (n == 9) then return "nine";
PD if (n == 10) then return "ten";
PD ASSERT FALSE;
END
@end example

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

BO(Question 4.5.5:) Add new FUNCTION call TT(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.

BO(Question 4.5.6:) Write a FUNCTION called TT(number2string2) that can
handle values up to but not including 100. Note that you will need
multiple IF DOTS THEN statements to achieve this. Note that if TT(n)
is a number then the following expressions are useful:

@example
PD  VAR int temp1 = n / 10 % 10 results in temp1 holding the tens digit of n and is zero in the case that n<10.
PD  VAR int temp2 = n % 10 results in temp2 holding the ones digit of n.
@end example

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

BO(Question 4.5.7:) Change the song FUNCTION so that the following FUNCTION
call: TT(song(5,"rum");) in the MAIN FUNCTION results in the following
printout:

@example
Five bottles of rum on the wall.

DOTS

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

BO(Question 4.5.8:) Once all the code is working, add the following line to
the MAIN FUNCTION: TT(song(100,"gin");) so that it prints out the
following:

@example
One hundred bottles of gin on the wall.

...

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

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

@example
FUNCTION String textand(String a, String b)
BEGIN
PD IF (a.equals("") or b.equals("")) THEN RETURN a + b;
PD ELSE RETURN a + " and " + b;
END
@end example

BO(Question 4.5.10:) TRICKY Write a new FUNCTION TT(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:

@example
VAR String ones = number2string3(n % 1000);
VAR String thousands = number2string3(n / 1000 % 1000);
VAR String millions = number2string3(n / 1000 / 1000 % 1000);
@end example

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

