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.
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;Save your work as ex1.a2w.
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.
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.
10.0 green bottles hanging on the wall;Save your work as ex2.a2w.
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.