Heigh Ho: We're Dancing with the Stars
In this lab you are going to be reading, studying and trying to understand code written by other people. And then you're going to write similar code yourself.
Nobody writes computer code in a vacuum. Before you start any project it is important to see what already exists to help you on the way. Reading and understanding code that other people have written is a valuable skill to develop.
You will be grateful if the other people, whose code you are examining, were courteous enough to include good comments along with their program code. Since you want other people, especially the grader, to understand the code that you write, you will, of course, be careful to include your own comments in your own code from now on, won't you!
In fact, from this lab onwards the quality of your comments is going to affect your lab grade.
Heigh Ho
Look at the words of the song The Farmer in the Dell, or alternatively watch it on Youtube.
Excercise 1
Create an Alice program in which a Mad Scientist (from the local gallery), or any character of your desire, sings the song. This will be a very easy exercise if:
Save your Alice program as exercise1.a2w
- You write a singVerse method for the Mad Scientist that has
- two String parameters (say who and what) and a
- String variable (say line)
- Use concatenation (or, in Alice, the joined with operator) so that line acquires the value "The who takes a what"
- Have the Mad Scientist say line three times with a "Heigh-ho the derry-o" in between.
- Treat the first and last verse separately, since they don't quite fit the pattern of the other verses.
- Your madScientist.singVerse will look like:
- madScientist.firstVerse
- madScientist.singVerse(who=farmer, what=wife)
- madScientist.singVerse(who=wife, what=child)
- etc.
- madScientist.lastVerse
Don't worry about the misprints in the song as given, nor about the fact that the song has the cheese. If your Mad Scientist says "a cheese" instead, that is fine.
Walking & talking
Take a look at some of the methods that are provided with the Mad Scientist and that you are allowed to edit. In particular I want you to study the walking and talking methods.
Make a copy of your Alice program from Exercise 1 by saving it as exercise2.a2w. You will be editing exercise2.a2w to complete the following exercise.
Excercise 2
Enhance your Farmer in the Dell program by having the Mad Scientist, or the character of your choice, walk and talk at the same time as he is saying the words of the song.
Feel free to decorate the world to represent your creative imagination.
Dancing with the Stars
I want you to create two characters using the hebuilder and the shebuilder. Suit yourself as to how they are going to look, and give them good names. (I called mine Bruno and CarrieAnn.)
Here's the outline of a story. Feel free to enhance it by adding more:
- Bruno turns to face CarrieAnn and bows
- CarrieAnn turns to face Bruno and curtsies
- CarrieAnn walks towards Bruno until she is a decent dancing distance from him
- (hint: At this point CarrieAnn should adopt Bruno as her vehicle -- or vice-versa depending on who you think should lead.)
- Bruno and CarrieAnn walk together in a dancelike formation to a starting point.
- Simultaneously, a dance song starts to play (Alice can play any .mp3 file you happen to own, and the ground has a play sound method) and the couple begins to dance.
Dancing requires synchronized movements. As Bruno moves a leg forward CarrieAnn should move the other leg back, and so on. I strongly urge you to thoroughly study the walking code that you saw for the mad Scientist as well as the walk methods that hebuilder and shebuilder provided for your couple.
Read in the textbook about the setpose method and that will help you understand the walk code that hebuilder and shebuilder made for your characters.
For your dance method you just need to create a few steps. You can do with as few as:
but you will want to use ideas you learned by studying the Mad Scientist walking and the he/shebuilder walk methods to make the movement much more convincing. Moving individual body parts and using setpose effectively will make for better movies.
- Bruno moves left leg forward while CarrieAnn moves right leg back
- Bruno moves right leg forward while CarrieAnn moves left leg back
Once you've made a step or two that you are pleased with, you can make your dance method repeat itself indefinitely by using a technique called recursion. Let me explain.
Suppose you have a dance method that works nicely for a step or two. It will look like:
do together bruno.movesomewhere bruno.dosomestuff carrieAnn.doyourstuffNote that I am using considerable shorthand here. Instead of specifying the intricate body movements that consitute a dance step for Bruno, I am simply writing bruno.dosomestuff. Similarly carrieAnn.doyourstuff is short for a lot of statements that specify CarrieAnn's intricate movements. To make it look like a plausible dance you will probably want to use some randomness. The world has some functions that can produce random numbers between a minimum and a maximum value. If you have Bruno move forward a random distance between, say negative 0.2 and 0.2 then half the time (on average) he will actually be moving backwards! You won't need to move CarrieAnn of course since she has acquired Bruno as her vehicle!
Now back to the dance method. You have written a complicated manouver that causes a random movement of both dancers together with some synchronized wigglings of body parts. I have shortened your code to the following sketch:
do together bruno.movesomewhere bruno.dosomestuff carrieAnn.doyourstuffWhat we would like to do is to have the dance method keep repeating over and over while the music plays.
- Preferred Method: One way to make the dance repeat indefinitely is to add one line at the bottom that calls the dance method again!
Assuming your dance method is a world method, all you need do is change your:
do together bruno.movesomewhere bruno.dosomestuff carrieAnn.doyourstuffto
do in order do together bruno.movesomewhere bruno.dosomestuff carrieAnn.doyourstuff danceAlice will prompt you with a warning that you are using "recursion" and you need to know what you're doing. Well, you do. So tell Alice so.
- or use loop: It's also possible to use the loop construct to cause repetition. But you're not supposed to know about that until Chapter 7.
Exercise 3
Write an Alice program that tells the "Dancing with the stars" story as described above and, perhaps, enhanced creatively by you. Save your program as exercise3.a2w.
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 'Lab4' folder copy your solutions to the exercises: Exercise1.a2w, Exercise2.a2w, and Exercise3.a2w.
Next, go back up to your folder 'CS053-Spring10', right click on your folder 'Lab4' and compress it either in a or file.
Your newly compressed file should be named:
- FirstName_LastName_Lab_4.zip or
- FirstName_LastName_Lab_4.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.