Green Bottles

Chapter: Green Bottles

A popular song on school bus trips has the words:

10 green bottles hanging on the wall;
10 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 9 green bottles hanging on the wall.

9 green bottles hanging on the wall;
9 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 8 green bottles hanging on the wall.

The song continues in this vein until it gets to:

2 green bottles hanging on the wall;
2 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 1 green bottle hanging on the wall.

1 green bottle hanging on the wall;
1 green bottle hanging on the wall;
And if one green bottle should accident'ly fall
there'd be no green bottles hanging on the wall.

Notice that each verse until the last two is essentially identical except for a number n of bottles. We should make n into a number parameter for a singVerse method.


Exercise 1

Start a world in which a character of your choice will be singing the 10 green bottles song. Create a method singVerse. Create a number parameter n. Additionally add two String parameters. I called mine numStart (for the number of bottles at the start of the verse) and numLeft (for the number of bottles at the end of the verse). Start your code for singVerse by setting the value of numStart to n as a string. (You may want to review page 78 of the textbook to remember how to do this.) Next a line to decrement the value of the parameter n by 1. And then a line similar to the first to set the value of numLeft. Now have your character say, in succession, with suitable waits in between,
numStart joined with " green bottles hanging on the wall;"
numStart joined with " green bottles hanging on the wall;"
"and if one green bottle should accident'ly fall"
"there'd be " joined with numLeft joined with " green bottles hanging on the wall."

Finally, have world.my first method call your singVerse method with a parameter value of 10. When you play this world, your character should sing:

10.0 green bottles hanging on the wall;
10.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 9.0 green bottles hanging on the wall.
Save your work as ex1.a2w.

Now change your world.my first method. Drag in a loop statement, telling it to loop 10 times. Click on the "show complicated version" button. Into the loop insert a world.singVerse statement with the parameter set to the index value that you see in the loop statement.

If you now play the world you will see a rather awful result. Your character will sing:

0.0 green bottles hanging on the wall;
0.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be -1.0 green bottles hanging on the wall.

1.0 green bottles hanging on the wall;
1.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 0.0 green bottles hanging on the wall.

...

Obviously this won't do.


Exercise 2

Keep the loop. But fix it so that the song goes:
10.0 green bottles hanging on the wall;
10.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 9.0 green bottles hanging on the wall.

9.0 green bottles hanging on the wall;
9.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 9.0 green bottles hanging on the wall.

... (six more verses)

2.0 green bottles hanging on the wall;
2.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 1.0 green bottles hanging on the wall.

1.0 green bottles hanging on the wall;
1.0 green bottles hanging on the wall;
And if one green bottle should accident'ly fall
there'd be 0.0 green bottles hanging on the wall.

Save your work as ex2.a2w.


rhyspj@gwu.edu