Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.

Similar presentations


Presentation on theme: "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition."— Presentation transcript:

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

2 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-2 5.1

3 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Custom Class-Level Methods 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 camelCase!) 5-3 5.1

4 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-4 5.2

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

6 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 5-6

7 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-7 5.2

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

9 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 Do together Lower the left leg Raise the right leg End Do together End Loop Lower the right leg Modified Algorithm: Call the rightLegUp method Loop 10 times: Do together: call the rightLegDown method call the leftLegUp method End Do together Do together call the lefttLegDown method call the rightLegUp method End Do together End Loop call the rightLegDown method 5-9 5.3

10 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Stepwise Refinement The algorithm is not necessarily incorrect, it just needs more detail 5-10 5.3 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

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

12 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-12 5.4

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

14 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 5-14

15 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-15 5.4

16 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 isShowing 5-16 5.5

17 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-17 5.5

18 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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…ExerciseGirl can runInPlace 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 5-18 5.6

19 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 5-19

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

21 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 5-21 5.7

22 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-22 5.7

23 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-23 5.7

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

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

26 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-26 5.7

27 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using Clipboards 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 5-27 5.8 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.

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

29 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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” –Images are flat, 2D with height and width no depth –Can be used as backgrounds or scenery or used to give info to the viewer! 5-29 5.9 Boy are you shallow, you’re only 2D!

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

31 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Tips for Visual Effects and Animation 5-31 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 5.9

32 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 asSeenBy argument causes an object to turn around the asSeenBy’s objects center point Example: the hawk turns one complete revolution, asSeenBy the tree, around the tree 5-32 5.9

33 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 isShowing property is set to false 5-33 5.9

34 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 setPose is a method of an object 5-34 5.9

35 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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! 5-35 5.9

36 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 5-36 5.9

37 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 5-37

38 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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. 5-38


Download ppt "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition."

Similar presentations


Ads by Google