Learning Target for this Assignment:
You will be able to:
- Re-use methods to create variations on a story (e.g. call them in different orders and/or call a method more than one time).
Experience how the desire to re-use a method (e.g. call it more than once) can lead to the need to re-design or change code in small ways.
|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-Lab3StoriesFromPieces.
Insert two lines of comments:
// First Name Last Name, First Name Last Name, Sweetwater High School
// Purpose: Reusing methods to create variations on a story
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: Re-ordering Our Story
One of the benefits of organizing code into meaningful methods is that we can change our storyline through a simple re-organization of our calls to our methods. In this lab we will use the code you finished in the lab for Module 2.
NOTE: This is a common practice in real software engineering jobs. You always need to be careful to be sure your code is fully working, because you never know when your boss will ask you to modify it to add a new feature!
Play the Alice world to remember what it does. Replay it as many times as you like.
Before you do anything, discuss question QL3.1 with one another. After discussion, answer the question together in your document.
- Now drag the hawkAttacks tile to be the first method call, and play the code to see if you imagined correctly.
Discuss question QL3.2 with one another. After discussion, answer the question together in your document.
We can fix the problem identified in QL3.2 by adding a line of code to make the camera get a good look at the frog BEFORE we have the hawk swoop. We could put that code in my first method before we call hawkAttacks. But, since the hawkAttacks method looks funny unless the camera is already getting a good look at frog, let’s add it as the first line in the hawkAttacks method. Play the world to see if the problem is fixed.
Part 2: Repeating Things to Make a New Story
Another great thing about methods is you can put the work into writing them once, but then you can get the computer to “do more” - by executing those methods multiple times. However, just like we say in Part 1, sometimes we may need to modify our code slightly so that things work when called multiple times. This will depend, for example, on where objects are in the world just before you call the method.
Suppose you want the hawk to attack twice. In my first method drag in a second call to hawkAttacks so it goes:
Woah! Look in the code of the hawkAttacks method to try to get an idea of what is going on... Having trouble seeing what is going on? When you are moving the camera around, things can get tricky to follow.
One common debugging technique programmers use to help them find bugs (or errors) in their code is to put in simple commands to help them recognize where they “are” in the code. It’s best if these commands are not something that normally is part of the program/story. (Why are errors called bugs? Read about the etymology and Grace Hopper-- one of the first female computer scientists).
- It’s hard to tell when one call to (execution of) the hawkAttacks method is complete. Debug this by inserting something unusual in between the two calls to hawkAttacks in my first method. Maybe have the frog say “after hawkAttacks call 1”.
Still confusing? Let’s look more closely at where the hawk ends up after hawkAttack is called once.
Discuss questions QL3.3 and QL3.4 with one another. After discussion, answer the questions together in your document.
- It seems like having the hawk move forward 10 meters was a simple way to have him appear to swoop down, but instead of swooping back up, he disappears into (under) the ground. You can play around to convince yourself this is true. Try modifying the code so the hawk only moves forward 5 meters. Try 6. Try 7. Ahh... In actuality the hawk was originally somewhere between 6 and 7 meters away from the frog.
Let’s use an animation trick to fix this. Our goal is to make sure the hawk returns to the same place he started before we called hawkAttacks. One super easy way is to create another “hidden” object (we’ll make it be invisible) in the world setup -- and have that object be in exactly the same place as where the hawk starts. This isn’t changing the program yet, we’ll just modify the setup of the world.
- Click on the green Add Objects button.
- I like to always use a Shapes > Box because it’s easy to get to and I don’t generally have boxes in my worlds. Click to add the box to the world. Then click Done.
- We need to move the box to be exactly where the hawk is in the setup of the world. In the upper left corner, right click on box, select methods > move to > hawk. (Again, this isn’t in your program, we are using these methods to control the world’s setup).
- Now, since the hawk is out of frame, we can’t see our box anymore. But since the camera is going to be moving around, maybe we might later. More generally for this “technique” we would have then box object be hidden. In the upper left corner, right click on box again, and under methods, chose set opacity to 0% (invisible).
Finally -- now that we have our hidden box object keeping track of where our hawk originally was, we’ll add some code at the end of hawkAttacks to make the hawk return to that spot.
- First, fix the changes we made while debugging (change hawk move to 10 meters, remove the code in my first method between the 2 calls to hawkAttacks). Bet you forgot that!
- In the hawkAttacks method, we want to add a line of code to make the hawk invisible - we don’t want to see him flying backward)
- Click on the hawk object.
- In the details pane, click on the properties tab (rather than the methods tab).
- Click and drag the opacity tile out into the code as the last instruction. Choose 0 (invisible).
- In the details pane, click on the methods tab again, and add an instruction to move the hawk back to his starting point. hawk move to box.
- Play your program.
Did you not see the hawk again? That’s right -- we forgot to make him visible again! At the hawkAttacks add one more thing. Set hawk’s opacity to 1. (Click on the hawk object, then the properties tab, then drag out the opacity tile).
Finally, if things seem to be moving slowly -- change the duration of the method calls to set opacity and move to box to be 0 seconds.
Discuss FINAL QUESTIONS with one another. After discussion, answer the questions together in your document.
When you are finished, submit both your Alice World and Google Questions (PDF).
- 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 Lab2StoriesFromPieces file to your Google Drive and delete the file off the Mac.