Presentation is loading. Please wait.

Presentation is loading. Please wait.

APCS-A: Intro Alice: Program Design and Implementation September 12, 2005.

Similar presentations


Presentation on theme: "APCS-A: Intro Alice: Program Design and Implementation September 12, 2005."— Presentation transcript:

1 APCS-A: Intro Alice: Program Design and Implementation September 12, 2005

2 Alice Exploration Demo of Alice programs Discussion of what we discovered

3 Alice Design Process Read the scenario  A description of the problem or task Design (Plan ahead)  Storyboard (visual or textual)  Evaluate & revise - think objectively about what might be changed Implement (Write the program)  Dragging and dropping the components Test (See if it works)  Does the program act how you expect it to?

4 Methods A method is something an object does  In Alice, it is a set of instructions that defines how to perform a specific task Alice automatically creates World.my first method that happens when the world starts (when you click play) An argument is information a method needs to execute an action  The move method takes several arguments

5 Control Statements Sequential programming  Do in order Simultaneous programming  Do together Sets up a block of code - everything inside that block happens as specified by the block

6 Nesting A control statements may be placed inside another  Example: We can put a small block of “do together” code inside a bigger block of “do in order” code

7 Bugs Errors in computer science are called bugs. In Alice, your “bugs” will be of two types:  Visual bugs: you expected one thing to happen, but actually programmed your method to do the wrong thing  Alice bugs: The programmers who wrote Alice didn’t find all the problems, so sometimes Alice will tell you it is quitting due to an unexpected bug. It is not your fault. Finding bugs in a program and fixing them is called debugging

8 Properties A property can also be called a definition (or sometimes a variable). It is something the object knows about itself.

9 Comments All good programs have comments which describe in regular English what the program is doing (in general, at a given step, before a particularly hard to understand point in code, etc) Comments are ignored by the computer, they are written for people looking at your code

10 Homework Mr. Forgetful is a referee for the STA football game which starts in an hour. His uniform, rule book, flag and whistle are all locked in his gym locker, but unfortunately he has forgotten the combination to the lock. He even bought an easy lock to begin with! The lock he owns has three dials, with numbers 0-2. Mr. Forgetful has asked you to help him out with his problem. He needs a surefire way to figure out the combination to his lock.  A) Describe to Mr. Forgetful the steps he should take to systematically find a way to get his lock open. Take into account the fact that he’s very stressed out - so you need to specifically tell him each individual step -- how to set the numbers, etc. (i.e. Don’t just say “try combination 011”)  B) How many combinations will he potentially have to try to get his lock open?  C) (Extra credit) If it takes him 1 second to set each number, will he make the football game?

11 APCS-A: Intro Alice: Control Structures September 13, 2005

12 Checkpoint Homework

13 Functions In Alice, functions are used to ask questions about an object’s properties  (most programming languages do not distinguish between methods and functions) Alice provides many built-in functions, in these basic categories:  Proximity  Size  Spatial relation  Point of view  Other

14 Answers to functions Each of Alice’s built-in functions returns an answer The most common type of answers are:  Number  Boolean value (true or false)  String  Object

15 Combining Functions We can use these functions in many ways. If we wanted to move a penguin from an arbitrary place in the world to her igloo, how would we do so? Move Method Functions:  Distance to  Object’s width Math manipulation of the above

16 Conditionals Sometimes what we want our program to do depends on certain conditions Is it raining? YesNo Take an Umbrella Bring a swimsuit Does x = 0? YesNo Say “Can’t divide by zero” Return y/x

17 Conditional Control Structure Use the if/else statement by dragging it into the editor Needs a function that will evaluate to true or false, if the answer is true, only the “IF” part happens, if the answer is false, the “Else” part happens --> and “do nothing” is an acceptable action

18 Relational Operators Located in World’s functions in the math category Use these to compare values to each other  == is equal to  != is not equal to  > is greater than  < is less than  >= is greater than or equal to  <= is less than or equal to

19 Boolean Logic Also located in World’s functions Not a --> not Both a and b --> and Either a or b, or both --> or

20 Other World Functions Advanced Mathematical operators  Min, max, sin, cos, powers, logs, etc Ask user  For a string, number, or yes/no Random number generation Time questions Etc Need to make sure you use things in the proper places, which the editor helps you figure out

21 Repetitions A control structure that easily lets you repeat an action a given number of times  Must enter a whole number of times Under more options, you can choose to count (increment) by 1 or more Discussion: Why would you ever want to increment by more than 1?

22 Try It! (Exercise 7 from Alice book) Create a world with a snowman and a stool, with the two object kind of far apart. Use a loop to make the snowman move to the stool, one meter at a time. Use a distance to function to determine the number of times the loop repeats. (The distance to function might return a fractional distance such as 3.75 meters. The loop statement truncates the fractional number to the integer 3 and will repeat 3 times.) We recommend that you test your solution by moving the snowman and the stool to different locations on the screen and running the program with each change, so you can tell whether it works no matter where the objects are located

23 APCS-A: Intro Alice: Classes, Objects, Methods, and Parameters September 14, 2005

24 Classes A class defines a kind of object  In Alice, classes are predefined as 3d models Think of the class as a blueprint that tells Alice how to create & display an object of that class Instantiation is the process of creating the object instance from the class

25 Objects An object is one particular instance of a general class  Frank is an instance of the Person class The person class would specify that all person objects would have hairColor and eyeColor  The Frank instance would have values for hairColor and eyeColor (like black and brown)

26 Classes vs Objects The difference between classes and objects is an important distinction in OO-programming Do you feel comfortable with the distinction? Identify the following as class or object:  Dog  Spike the dog  Car  Ms.K’s Saturn  Canoe

27 Methods Something an object does  A set of instructions Primitive methods (in Alice)  These are the pre-defined methods built-in to the system like move, turn, say, etc But as you saw in the ice-skating program, other methods can be defined What belongs in a method?  Everything… but think about making a new method whenever you can logically define something as a separate task

28 Abstraction Methods can keep a program from getting too long by allowing the programmer to divide a task into smaller parts When we have a method that does a series of steps that we can think of as one unit, we have abstracted away part of the details of the program  Instead of thinking of “Move” as the individual steps - we think of it as one action We don’t have to worry about each individual action that goes into making something move Having separate methods also makes the program more readable (and more organized) And Ms. K likes things organized :)

29 Parameters The pieces of information that a method or function needs in order to carry out the action  Also known as arguments Parameters are a form of object-object (or method-method) communication Think of the parameter as a basket (with a name) that received information for the method to use

30 More about methods A method that defines a behavior for a single object is a class-level method A method that defines behaviors for several objects is a world-level method Methods can call as many other methods as it wants A method needs to be invoked (called) before it is executed

31 Making new methods Each method tab has a button that allows you to “create new method”  You will be prompted to enter a name  Then an new editor pane opens up where you can begin your code You can define as many different methods as you want  And then World.myFirstMethod could just call them in order World.StartObjectsMoving World.SurpriseEvent World.RunAway  Or you can start to do even more exciting things…

32 Dance Recital You have been tasked to show off the dance routine for a group of 15 ballerinas. The first step of the choreography is to have each dancer move forward, “jump”, and move back  How would you do this? (Let’s sketch ideas on the board)

33 Parameterized methods When you have a set of objects that all need to do the same thing, you don’t want to have to repeat the same code over and over  You can take in the object as a parameter, and have the set of repeated actions in the method body When you define a new method, you have the option to “create new parameter”  A dialog box asks you to name your parameter (a placeholder name) and for the type of parameter (number, boolean, object, or other)  Then you can drag the parameter name from the method header into the editor to call methods Only methods that are common to all objects are visible

34 Multiple Parameters What if in our dance recital, we want the 1st dancer to move forward 1 meter, the 2nd 2 meters, and so on?  We need a way to specify the distance for each dancer object in our “dance” method We can create a second parameter, call it d, with type number, and use that in the move instruction for each object

35 Choreographer You’re the choreographer! You will make a dance routine for 10 ballerinas (or ice-skaters, penguins, or whatever). Using a parameterized method like we have discussed today, make up one routine that all the objects do (in order or in parallel). You will call your parameterized method from a new method that you create called “CurtainUp”. Replace World.MyFirstMethod with CurtainUp as the method that is called when the world starts.

36 APCS-A Alice: Classes, Objects, Methods, and Parameters Revisited September 15, 2005

37 Yesterday… We talked about classes, objects, methods and parameters and we practiced making some new world level methods (with the choreographer exercise)  I think we need some more work on that, but first lets look at one or two more things (since we won’t have a lecture tomorrow)

38 Creating Class-Level Methods Basically the same as the world-level methods But when you create a class-level method, you can save an object with new methods as a new kind of object  IceSkater becomes FancySkater with new methods  The new class inherits all the properties and methods of the original class (anything IceSkater did, FancySkater can do)

39 Skate Method To make a skate method that looks realistic, you want the skater to slide on each leg as she moves forward  To Slide realistically, you may want to lean the upper body as you lift a leg Already, we have split our task up:  Skate Method - has steps of move & slide  Slide Method - has steps of lift leg and turn upper body

40 Spin Method To make a spin method, we need to know how many times we want the skater to spin  Also, in order to spin, we may want the skater to move her arms up and change the leg position (prepare to spin) before the spinning happens, and after spinning, we want to return the skater back to her normal position (finish the spin) (The exact steps the book suggests for realistic looking movements are a little funny and would probably have to be found otherwise by trial & error)  Things like turning arms backwards to left them up and preparing leg by turning it left and backward for.2 and.25 revolutions, respectively

41 Creating new classes Once you have added new methods to a class, you can save it as a new class, a new 3d model that can be imported  First, right-click rename the iceSkater in the world hierarchy -> to FancySkater  Then, right-click to select save object which writes a new file with extension.a2c This class can then by imported (File>Import) and used in new worlds Note: make sure that you do NOT refer to other objects in your new class methods  (We will talk about exceptions to this Monday)

42 Schedule Rest of today and class time tomorrow (for those of you here) : we will be working with Alice on parameterized class & world level methods  After you finish the dance routine, you can make up your own animation or see Ms. K for a project idea from the book Become comfortable with using methods & parameters  You will show your new animation in class Monday Abstract Article (Due Monday) Read an article about computer science that seems interesting to you and write a 1/2-1 page single- spaced paper in which you give a brief summary of what you read, what you thought was cool about it and what (if any) implications you think it may have for computer science or for society in general  Please take a magazine with you, but bring it back next week!


Download ppt "APCS-A: Intro Alice: Program Design and Implementation September 12, 2005."

Similar presentations


Ads by Google