Learning Target for this Assignment:
You will be able to divide an Alice program into smaller more meaningful parts (methods) as evidenced by creating your own named methods.
|Navigator, you will be responsible for the directions on this page and the module questions. Save the questions as a copy and insert both partners' names, the date, and your teacher's name where indicated.
Driver, you will open up this Alice world and Save it as: LastName-FirstInitial-LastName-FirstInitial-Lab2DividingAStory.
Insert two lines of comments:
// First Name Last Name, First Name Last Name, Sweetwater High School
// Purpose: Using methods to break down a program into smaller pieces.
Do not do anything else with the world until the Navigator tells you to.
|Navigator, begin the instructions to the driver. Alert the driver when they should begin making changes to your program. Do not touch the mouse or keyboard while you are the navigator.
Driver, listen carefully and actively to the Navigator.
Part 1: Exploring a program someone else wrote
A really good way to learn how to program a computer is to look at and learn from examples that other programmers have written. In doing this you need to ask yourself: Do I know what this instruction does? Can I match it in my head with the program behavior when I play it?
Play the Alice world and observe what happens in the scene. Replay it as many times as you like.
Now spend a few minutes looking at (and playing with) the code that makes up this Frog scene. Make the frogs say different things (change the parameters) and play with other ways to make the camera move through the scene.
Finally, discuss questions QL2.1 and QL2.2 with one another. After discussion, answer the questions together in your document.
Part 2: Organizing code by creating methods
There are a lot of lines of code in this program and it is starting to get difficult to make sense of what the entire program does. If we group the code into methods, we could wind up with two lines of code that control these 2 basic parts of the program (by calling these 2 methods):
By just reading these two descriptive method names we can get a pretty good idea of what the whole program does. If we want to know the details (for example, what the frogs are actually saying to each other), we simply open up one of the methods (double click on it) and read the code that is in there.
I’ll walk you through creating the
frogsChat method and then you can follow the same steps to create the
2.1 Create the frogsChat method
- Click on the world object in the Object List.
- Make sure the methods tab is selected in the details pane.
- Click on the create new method button.
- Type in frogsChat and click OK.
- A new empty code window will open up that is called world.frogsChat. Whatever code that you place in that window will be played when the frogsChat method is called. To make sure you understand this let’s put some code in the frogsChat method. Click on the frog object in the Object List, and then drag the say method into the frogsChat code window. Select Other... from the drop down window and then type in “Testing. 123”.
- Play the program.
- Now answer questions QL2.3 and QL2.4.
The frog doesn’t say “Testing. 123” because we never call the frogsChat method in my first method. Remember, when you hit play, Alice automatically executes the code in my first method.
We created the method and even told Alice what the method should do, but we never call it (e.g. tell Alice to execute it). We know that the code in the my first method method does something, so if we put a call to frogsChat somewhere in that method, our frog should say “Testing. 123”.
- Click on the my first method tab in the code window.
- Click on the world object in the Object list, and then drag frogsChat method into the my first method code window above all of the other lines of code. In other words, make the frogsChat method call be the first line of code in the my first method method.
- Now play the program and you should see the first frog say “Testing.123” before the rest of the scene plays out.
Ok, now we are ready to put the actual “frogs chatting” code into the frogsChat method. The idea here is to group all of the code that has to do with chatting -- all of the code up to the first frog saying “Uh oh!” -- into the frogsChat method.
Before we do this -- we need to remove our “Testing. 123” code -- drag that tile to the garbage can to erase it.
We have a few different options for how we could do this: 1. We could re-create exactly the same code and then erase the original version that is in my first method, 2. we could copy and paste each line of code independently by using the clipboard feature in Alice, or 3. we could be clever and group all of the code into a Do in order block and then copy and paste that block by using the clipboard. Programmers hate to do more work than necessary, so do option 3.
- Drag each of the lines of chatting code into a new Do in order block, making sure that they are in the same order that they were in before.
- Drag it up to the clipboard.
- Click and drag from the clipboard into the frogsChat window. You should see the whole Do in order block with all of the chatting code in your frogsChat method.
- You’ll now need to go back to the my first method code and delete the Do in order block.
- Play your program and you should see exactly the same result.
What we have just done is called a refactoring. The program does exactly the same thing, but the code now looks different. In general you refactor a program to improve its organization either to make it easier to understand or to make it easier for you to do something complicated in the future. This is an example of how we use methods to support abstraction -- we can hide away complex code in methods.
Now answer question QL2.5 in your document.
2.2 Create the
Follow the same steps as above to create a new method called
hawkAttacks that groups together all of the attacking hawk related code. By the end of this step you should only have two method calls in my first method:
The end result of this refactoring should be that the program does exactly the same thing that it did when you opened up the project. However, my first method is now a lot easier to understand.
Now answer question QL2.6 and QL2.7 in your document.
When you are finished, submit both your Alice World and Google Questions.
- Have you answered all questions in complete sentences?
- Did you name your files correctly?
- Did you add your lines of comments to each of your methods in the Alice program?
Last, upload your Lab2DividingAStory file to your Google Drive and delete the file off the Mac.