1 / 17

Methods in Programming: Organization and Efficiency

Learn about methods in programming, including their types, advantages, and how to create and call your own methods. Explore a hide and seek animation example and discover how methods can help organize your code and save time.

gardnerb
Download Presentation

Methods in Programming: Organization and Efficiency

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods

  2. What will we learn today? • What are Methods? • Types of Methods • Using Methods Example • Creating your Own Method • Calling a Method

  3. Methods • A Method is a coordinated sequence of instructions that will be carried out when requested • Are used to organize several instructions • Allow a programmer to think about a collection of instructions as one instruction (e.g. method for walking and moving legs and arms) • Each method performs its own job, but all methods work together to create the over all animation

  4. Advantages of Methods • Better organizing the instructions to make it easier to read the code • Reduces the amount of program code and saves us time • Once the method is defined, we can tell Alice to run it from one main method (e.g. World.MyFirst method) • Methods can be called from several different places in the program

  5. Types of Methods • We can write two types of methods • World-Level Method: methods where objects are interacting with other objects (e.g. a falcon is hunting a bunny) • Object-Level Method: methods where objects are acting alone, not affecting or being affected by other objects (e.g. a bird is flying in the sky)

  6. Hide and Seek Example • Consider an example where a boy is playing “hide and seek” in the garden with his cat and moving around the tree to look for the cat • We want to write this animation using methods • The first step is to think about the animation in terms of large task, we can write the storyboard like this

  7. Hide and Seek Example • The Storyboard Do In Order Seek Cat- the boy walks towards the tree Move Around- the boy and his cat turn around the tree Get Tired- the boy and his cat set together under the tree

  8. Hide and Seek Example • The next step is to break down each major task of our storyboard into simpler steps Seek Cat Do In Order boy moves forward cat sets under tree boys legs and arms move Move Around Do Together boy and cat turn right boy and cat move a step forward boy and cat move legs Get Tired Do In Order boy sets under the tree cat sets under tree

  9. Making 3 Turns Around the Tree • We want the boy and the cat to make three complete turns around the tree • To make one turn, the Move Around method should be repeated 10-15 times (Hint: copy and paste the code 10-15 times) • To make 2 or 3 complete turns, repeat the previous action two or three times Repeat 2-3 times Repeat 10-15 times Move Around

  10. Create your Own Method • Lets write world-level methods for the three tasks “move around, seek cat, and get tired” • In the object tree • Select the world object • Select “methods” tag in the details panel • Click the “create new method” button • Enter the name of the new method in the popup box • Click the “edit” button to the right of your method. Add the instructions for the method

  11. Calling a Method • When we click on play button, Alice automatically executes world.my first method • So, when a user clicks play button the method starts and world.my first method is called • To call a method • Drag the method from where it is listed in the methods tab of the details panel into world.my first method

  12. Seek Cat Method

  13. Turn Around Methods

  14. Move a Step Methods

  15. Get Tired Method

  16. Calling the Methods

  17. Let’s Work with Alice • Sara and her horse are practicing a jump • Create an initial scene with horse and rider facing a fence (building) • Write two world-level methods • Gallop, horse and rider gallop forward one step, horse’s legs movement should look realistic • Jump, horse and rider jump the fence. Horse should move up far enough to not hit the fence • Write instructions in world.my first method to call the gallop method as many times as needed

More Related