Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where are we? Alice is a “toy” language Fundamentals are exactly like “real”

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where are we? Alice is a “toy” language Fundamentals are exactly like “real”"— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where are we? Alice is a “toy” language Fundamentals are exactly like “real” languages Concepts of parameters, reuse, design are classic First half of the semester, we talked about problems scientists and particularly computer scientists solve Second half we look at the fundamental tools used in solving those problems 5-1

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Computer Science – is it for you? Some of you have a real talent for this. Computer Science degrees earn $51K starting and over $100K after a few years. There are jobs now and only getting more! Projected to increase by 38 percent over the next ten years, which is much faster than the average for all occupations. This occupation will generate about 324,000 new jobs, over the next decade, one of the largest employment increases of any occupation. Try CS 1400 next semester 5-2

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

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-4 Writing Custom Class-Level Methods If a class does not provide a method, a custom method can be written. Have it YOUR way. A VERY IMPORTANT CONCEPT For example, we could have a method for our bopper which goes to an item and bops! Write the pseudocode Primitive methods are available for various classes - Move, Turn, Roll, Say, Think, etc. Some classes have customized methods written just for them --flap, hop, walk, jump 5.1

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Suppose I want to create rollable wheelies – where as they move forward the wheels move. How would I get make it so my other programs could use rollable wheelies? What is a billboard? Give an instance where you would want to use one. What is a pose? Why would you want to use one? 5-5

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

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Try one Takes any object and makes it jump What is input? What is the point of producing methods? 5-7

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Who “owns” the method? You can also have ‘world’ methods – that belong to the world. The method should be part of the most ‘logical’ group. “I have rights to everything I am using.” If you make ‘disappear’ a part of the world, when the magician is moved to another world, he/she won’t know how to make objects disappear. Reuse – means creating to be used again! 5-8

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-9 Saving an Object to a New Class Custom methods are for only a single instance of the class. If you “copy” the instance, the new instance will also have the methods. If you create a new instance or add to one of two instances, they don’t share methods. If methods will be needed in other worlds, need to create a new class. Saving as a new class, adds the new method to that object permanently. For example, if you get a chicken to walk, you might want to allow the class to be used by others (and yourself) Programming becomes very creative. The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures... Frederic Brooks, Jr 5.2

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-10 Saving an Object to a New Class Right-click and Rename the object in the object tree 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!

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-11 Inheritance New methods were added and a new class was created. The new class still contains 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.2

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-12 Stepwise Refinement Better to start out with general idea and then develop the detail 5.3 Refined to have more detail added to it Algorithm becomes several methods

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-13 Stepwise Refine Example Note the reuse 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 rightLegUp(+1) method Loop 10 times: Do together: call rightLegUp(-1) method call the leftLegUp (+1) method End Do together Do together call leftLegUp(-1) method call rightLegUp (+1) method End Do together End Loop call rightLegUp(-1)_ method 5.3

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

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

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

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Functions return a value where methods don’t. For the following identify inputs and possibly outputs: Solo for beetle Wheely moving around and turning near edge Checking if wheelies have crashed Making an object levitate Making all objects disappear 5-17

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using the class parameters It “real” languages, you are able to specify the type of the parameter. That allows you to use methods that all objects don’t necessarily have. Not being able to do that is limiting 5-18

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

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

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

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

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

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

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

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

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

28 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-28 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 –Very flakey – doesn’t work well with parameters 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.

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

30 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-30 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 instructions to the viewer! 5.9 Boy are you shallow, you’re only 2D!

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

32 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-32 Tips for Visual Effects and Animation 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 objec –setPose is a method of an object 5.9

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

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


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where are we? Alice is a “toy” language Fundamentals are exactly like “real”"

Similar presentations


Ads by Google