In this lab you are going to create an Alice animation where a character sings a repetitious song about green bottles that will require you to use a loop. You will use conditionals to enhance the performance of the song.
You will try to get numbers to appear in say messages without the annoying ".0" that Alice often inserts. I'll show you my trick for replacing "9.0" by "9", but if you can do better I'll be very pleased.
Then you will create an interactive scenario where you will use a while loop to sum an undetermined number of prices and calculate a sales tax to be added to the total.
Finally, I want you to use loops and conditionals to tell a story you invent.
As always, be sure to write up your experiences -- your frustrations and failures as well as your triumphs -- in your lab notebook. Your lab instructor will want to see it regularly.
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.
Excercise 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 local variables. 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;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.
Excercise 2
Keep the loop. But fix it so that the song goes:
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.
Correct grammar, if ...
You are probably as upset as I am that your character from the previous exercise has such awful grammar. Your character says "1.0 green bottles" instead of the more grammatical "1.0 green bottle".
Excercise 3
Modify your code for world.singVerse as follows. Instead of having numStart be the value of n as a string, change it so that:
Similarly make numLeft be (after decrementing n)
- if the value of n is 1 then numStart will get the value n as a string joined with " green bottle";
- else the value of numStart will be n as a string joined with " green bottles".
depending on the value of n.
- the value n as a string joined with " green bottle";
- n as a string joined with " green bottles".
You will also need to adjust your character's say statements to reflect the fact that "green bottle" or "green bottles" is now incorporated into the numStart and numLeft variables. Save your work as ex3.a2w.
Improve the style
I daresay you're as distressed as I am about all those ".0" that appear after the numbers in the song.
Excercise 4
Improve the style of the song. No more ".0" please. Save your work as ex4.a2w.
How many items?
Make an Alice program to implement the following story.
A character goes into a shop. Speaks to a cashier. The cashier asks "How many items do you have?"
Display a NumberDialog so that the user can answer this question for the character and provide a number of items.
Now the cashier will ask
How much is item 1?and display a NumberDialog to get the price of the first item.Then the cashier will ask
How much is item 2?and display a NumberDialog to get the price of the second item.Then the cashier will ask
How much is item 3?and display a NumberDialog to get the price of the third item....
and so on for the number of times indicated by the answer to the first question ("How many items do you have?") If any amount entered by the user is negative or more than a million dollars, then the cashier will say something rude and ask for that number again (ignoring the unacceptable number).
Finally, the cashier adds up all the individual amounts, adds 10 percent sales tax and announces the grand total. Like this but you don't have to be quite as intricate as Toy's example. Don't worry if Alice makes your money amounts look weird (as is Toy's total in the movie you may have watched).
Excercise 5
Program the above story as an Alice program. Save your work as ex5.a2w.
Your own creation
For your last exercise I want you to us your imagination. I want you to tell a story that:
- Has more than one possible ending, depending on an input from the user;
- Has a character do some action (like spinning or rolling over) a random number times. That number should be between 5 and 10.
Exercise 6
Make an Alice program to tell a story as described above. You're welcome to look at my pathetic attempt. You of course will do far far better.
How to submit your homework
Create a folder on your computer and name it 'CS053-Spring10' (if you do not have it already). Create another folder and name it 'Lab2'. In the newly created 'lab5' folder copy your solutions to the exercises: Exercise1.a2w, Exercise2.a2w, Exercise3.a2w, and so on...
Next, go back up to your folder 'CS053-Spring10', right click on your folder 'lab5' and compress it either in a or file.
Your newly compressed file should be named:
- FirstName_LastName_Lab_5.zip or
- FirstName_LastName_Lab_5.rar
After you have compressed your homework, then proceed to submit it via Blackboard.
Got problems? If you have any problems make sure you clear them with your lab instructor because if you do not follow these requirements for submission your lab homework submissions will not be accepted and you will get zero points.