Chapter 6 Methods Functions and More about Variables

Chapter 6: Methods, Functions, and More about Variables Starting Out with Alice: A Visual Introduction to Programming Third Edition by Tony Gaddis Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

5. 1 Writing Custom Class-Level Methods • If a class does not provide a method that is needed, a custom method can be written for an object of that class • Primitive methods are available for various classes • Move, Turn, Roll, Say, Think, etc. • Some classes have customized methods written just for them • wing_flap, hop, walk, jump Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -2

5. 1 Writing Custom Class-Level Methods • Methods that are part of a class • Create an instance of the class • Select the instance • Create new method • Enter a name for the new method (remember to use camel. Case!) Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -3

5. 2 Saving an Object to a New Class • Custom class-level methods are only a single instance of the class • If each additional object should have this new method, the object must be saved as a NEW class • Saving as a new class, adds the new method to that object Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -4

5. 2 Saving an Object to a New Class • IMPORTANT!!! Right-click and Rename • Make Right-click Save the object in and the object sure that when an object becomes a new class (and Object… • can The new class ends has that is it independent of the tree be used in other worlds) the new extension: • world From File, choose. a 2 c. a 2 w likepress Alicethe Import… • If it(NOT Select file and depends on anything in the world and you use it in worlds and programs!) Import button the new world, it may not work since it can’t “find” that old world! Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -5

Task #1 • Create Tutorial 6 -1 on page 214 -216 called Creating a class-level method And • Create Tutorial 6 -2 on pages 218 -220 called saving an object to a class. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -6

5. 2 Inheritance • New methods were added and a new class was created. • The new class still contained the methods that had previously been associated with the original object and class (such as Move, Turn, Say) • The new class also contains the properties that had previously been associated with the original object and class (such as Color, Opacity, Vehicle) • The new class inherited all the original methods and properties from the original class Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -7

5. 3 Stepwise Refinement • Sometimes the algorithm does NOT have enough detail • The algorithm must be refined and have more detail added to it • This may mean that the algorithm becomes several methods Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -8

5. 3 Stepwise Refine Example Original Algorithm: Raise the right leg Loop 10 times: Do together: Lower the right leg Raise the left leg End Do together Lower the left leg Raise the right leg End Do together End Loop Lower the right leg Modified Algorithm: Call the right. Leg. Up method Loop 10 times: Do together: call the right. Leg. Down method call the left. Leg. Up method End Do together call the leftt. Leg. Down method call the right. Leg. Up method End Do together End Loop call the right. Leg. Down method Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -9

5. 3 Stepwise Refinement • The algorithm is not necessarily incorrect, it just needs more detail • Development and use of new methods means that code can sometimes be reused • Development and use of new methods reduces the amount of code written Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -10

5. 4 Passing Arguments • Methods are written to accept arguments • Arguments are values that are passed • Example of arguments: – Direction, distance traveled Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -11

5. 4 Passing Arguments • Passing arguments requires the use of parameters • Parameters are variables that hold an argument that will be passed (or used) into the method • Once created, the argument is required anytime the method is called or used Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -12

5. 4 Passing Arguments Example • Rather than have Exercise. Girl run. In. Place X times, … – a parameter could be created called repetitions – would allow the viewer to enter an amount Exercise. Girl should run in place Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -13

Task #2 • -Practice stepwise refinement by designing the Tutorial 6 -3 on pages 225 -227 called Completing the Workout World. • -Complete tutorial 6 -4 on page 229 -231 which is called passing arguments to a method to see how arguments are used in programming. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -14

5. 4 Object Parameters • Some parameters can be an object since some methods use objects as an argument • turn to face is a method with an object as an argument • move to is another example • When creating an object parameter, identify the new parameter as an object Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -15

5. 5 Using Class-Level Variables as Properties • Properties can be added to an object through the creation of class-level variables • When the object is saved as a new class, the variables are saved with it • Common properties are: • color • opacity • is. Showing Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -16

5. 5 Using Class-Level Variables as Properties • Property is a variable that belongs to an object • Properties hold data about that object and are referred to as class-level variables Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -17

5. 6 Writing Class-Level Functions • Function is a method that returns a value back to the instruction – such as: distance to • Additional data is required by the function to return back a value – for example…Exercise. Girl can run. In. Place forever, but she’ll get tired – a function can be created…but it needs to know how many repetitions it will take before she gets tired AND how many repetitions she has already completed Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -18

Task #3 • -Complete two tutorials to help showcase how items can be added to an object and used within a program. • The first tutorial will be 6 -5 on pages 235 -237 called Adding a Property To An Object And • The second tutorial 6 -6 is on pages 238 -242 called Writing Class Level Functions. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -19

Task #4 • Students will complete tutorial 6 -7 on page 243 – 248 called Writing A Class Level Function. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -20

5. 7 World-Level Methods and Variables • Rather than creating one long method…several smaller world-level methods can be created • Each smaller method performs a specific part of the overall animation • Can also create world-level variables • These variables are available to all methods in the world. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -21

5. 7 World-Level Methods • Class-level methods give unique behaviors to an object • World-level methods are methods created for the object world • Worlds can have many methods • Commonly used to break a large complex algorithm into small manageable pieces • This is the divide and conquer approach Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -22

5. 7 World-Level Methods • Meant to simplify the program • Once a method has been written, it can be implemented many times • Known as code reuse because the code is written once and used many times to perform the task Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -23

5. 7 World-Level Methods Example Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -24

5. 7 World-Level Methods Example Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -25

5. 7 World-Level Variables • Variable that belongs to the world object • World-level variables act as properties for the world • Exist as long as the world is running (playing) • Available to all methods in the world Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -26

5. 8 Using Clipboards Only one clipboard appears in Alice…the brown one is what an empty clipboard looks like and the white version has information stored in it. • Clipboards are locations for storing a copy of something • Use the clipboard in Alice to copy an instruction from one method to another • Drag the instruction tile to the clipboard • Grab the clipboard and drag back to another method Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -27

5. 8 Need More Clipboards? Increase the number of clipboards from Edit Preferences Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -28

5. 9 Tips for Visual Effects and Animation • Billboards – – Graphical images that have been inserted into the world Can insert images that are JPEG, GIF, TIF Known as “Billboard” Boy are you shallow, you’re Images are flat, 2 D only 2 D! with height and width no depth – Can be used as backgrounds or scenery or used to give info to the viewer! Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -29

5. 9 Tips for Visual Effects and Animation • Fog – Alice has a look of “mist” to the world – The entire world becomes less visible Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -30

5. 9 Tips for Visual Effects and Animation • Vehicles – Allows two objects to move together – If done separately… • Write the code to move one object • Write code to move the other and “hope” they would move together • OR – Make one object a vehicle that the other rides on…and only right the code to move the vehicle Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -31

5. 9 Tips for Visual Effects and Animation • Circling Other Objects • Object’s turn method spins the object around • The turn method can also have an object circle around another object • The as. Seen. By argument causes an object to turn around the as. Seen. By’s objects center point • Example: the hawk turns one complete revolution, as. Seen. By the tree, around the tree Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -32

5. 9 Tips for Visual Effects and Animation • Invisible Objects • Sometimes an object needs to move in a circle, but NOT around another object. • In this instance, an object is placed at “the center” of the revolution • The object’s opacity property can be turned to 0% (invisible) or the is. Showing property is set to false Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -33

5. 9 Tips for Visual Effects and Animation • ATTENTION!!! – Once an object has been put into a position or pose, the pose can be restored during an animation • Pose is a property of an object • set. Pose is a method of an object Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -34

5. 9 Tips for Visual Effects and Animation • Programming the Camera – Camera can be programmed just like other objects – All the same primitive methods and functions are available for it: • Point at, Move, Turn, etc. – Camera can also be a vehicle for other objects! Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -35

5. 9 Tips for Visual Effects and Animation • Creating Dummy Objects – Dummy objects are invisible objects that are placed in the world – Camera then moves to the invisible object to get different perspectives Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -36

Task #5 • Design a program that utilizes 5 of the following 7 features that are covered on pages 252 -262. 1)Billboards 2)Fog 3)Moving Objects Together With the Vehicle Property 4)Making Objects Circle Another Object 5) Capture a pose and 6)Programming the camera and 7) Creating Dummy Objects for the camera. Students will design a program that utilizes the above 5 of the 7 items and each time that an item is used a comment should be placed within the program code. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -37

End Of Chapter Skills • - Complete the following skills programs) from pages 265 -270 valued at 50 points each. Students will select 3 of the following programs: • 1, 4, 5, 6, 7 • The students will then complete following challenge programs that will be worth 100 points. Students should select three of the following programs. • 8, 10, 11, 12, 13 • Total points for the skills programs will equal 450. Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -38
- Slides: 38