1 / 51

Classes, Objects, and World-level Methods

Classes, Objects, and World-level Methods. Alice. Larger Programs. As you become more skilled in writing programs, you will find that programs quickly increase to many, many lines of code. Games and other "real world" software applications can have thousands, even millions of lines of code. .

tal
Télécharger la présentation

Classes, Objects, and World-level Methods

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. Classes, Objects, and World-level Methods Alice

  2. Larger Programs • As you become more skilled in writing programs, you will find that programs quickly increase to many, many lines of code. • Games and other "real world" software applications can have thousands, even millions of lines of code.

  3. Classes, Objects, & Methods • Object-oriented programming uses classes, objects, and methods as basic programming components. • These components help to • organize a large program into small modules • design and think about an intricate program • find and remove errors (bugs)

  4. In our programs,we have been using… • Classes • In Alice, classes are predefined as 3D models • Objects • An object is an instance of a class. Class: Frog (Uppercase name) Objects: frog, frog1, frog2, frog3 (lowercase names)

  5. Objects • An "object" is • any thing that can be identified as unique from other things • How is an object unique? • has a name • has properties: • width, height, color, location • can perform actions (methods): • associated actions it can perform • tasks it can carry out

  6. Object Parts • Objects may be composed of parts

  7. 3 Dimensions, 6 Directions • A 3D object has • 3 dimensions • height, width, depth • 6 degrees of freedom (directions of movement)

  8. Center of an object • At the center of mass • Where it stands on the ground • Where it is held

  9. peter Person paul mary spike Dogs scottie fluffy Class • Objects are categorized into classes • Each object is an instance of the class. • All objects in a class have similar propertiesand generally can perform the same tasks.

  10. We have also used… • built-in (predefined) methods • Examples: move, turn to face, say • World.my first method • Example: In the FirstEncounter world, we wrote program code where a robot was surprised by an alien. All the program code was written in this one method, see next slide…

  11. Potential Problem • The program code just seemed to grow and grow. • If we continue to write programs this way the programs will become longer and more difficult to read and think about.

  12. Solution • A solution is to organize the instructions into smaller methods. • A possible storyboard Do in order surprise – spiderRobot and alienOnWheels surprise each other investigate – spiderRobot gets a closer look at alienOnWheels react – alienOnWheels hides and spiderRobot sends message

  13. Solution • A solution is to organize the instructions into smaller methods. first encounter surprise investigate react

  14. Top-Down Design: Modular Design • The next step is to break down each major task into simpler steps. • Example: surprise Do in order alien moves up alien says "Slithy toves?" robot's head turns around

  15. Top-Down Design: Modular Design surprise Do in order alien moves up alien says "Slithy toves?" robot's head turns around investigate Do in order robot turns to look at alien Do together robot moves forware toward the alien robot’s legs walk react Do in order alien moves down robot turns to look at the camera robot’s head turns red (signaling danger) robot says “Houston, we have a problem!”

  16. Stepwise Refinement • The process of breaking a problem down into large tasks and then breaking each task down into simpler steps is called stepwise refinement. • Once the storyboard is completed, we write a method for each task.

  17. Demo: Starting a new method • First, to associate the new method with the World • select the World tile in the Object Tree • select the methods tab in the details area • click on the "create new method" button

  18. Demo • Ch04Lec1FirstEncounter • Concepts illustrated in this example world: • surprise is a world-level method because it is defined as a method for World and has instructions that involve more than one object (spiderRobot, alienOnWheels) • The surprise method is executed by calling (invoking) the method .

  19. Why? • Why do we want to write our own methods? • saves time -- we can call the method again and again without reconstructing code • reduces code size – we call the method rather than writing the instructions again and again • allows us to "think at a higher level" • can think surprise instead of “The alien moves up and says ‘Slithy toves?’ and then the robot's head turns around. " • the technical term for "think at a higher level" is "abstraction"

  20. Parameters Alice

  21. A beetle band • Our task is to create an animation for a bug band as an advertisement for their next concert.

  22. Storyboards • Each bug band member will perform a solo. Do together Do in order georgeBeetle move up georgeBeetle move down play sound Do together Do in order ringoBeetle move up ringoBeetle move down play sound Do together Do in order paulBeetle move up paulBeetle move down play sound Do together Do in order lennonBeetle move up lennonBeetle move down play sound

  23. Demo • Ch04Lec2BeetleBand-v1 • Concepts illustrated • To play a sound, a sound file (MP3 or WAV) must first be imported into Alice. (Alice is not a sound editor.) • This code is only for georgeBeetle. • Three more methods (one for each band member) will be needed!

  24. A Better Solution • Four versions of very similar code seems a bit tedious. The only things that change are the beetle and the music that plays. • A better solution is to write a more flexible method.

  25. Parameters • Built-in methods provide flexibility by providing parameters such as distance and direction. • Parameters allow you to pass in values (arguments). • Example Parameters: distance, direction Arguments: 0.5 meters, 0.5 seconds

  26. Kinds of Parameters • Alice provides several kinds of parameters that can be used in your own methods.

  27. The storyboard solo Parameters: bandMember, music Do together Do in order bandMembermove up bandMember move down playmusic • In this example, we can write just one method and • use parameters to specify: • which band member is to perform and • which music should be played.

  28. Demo • Ch04Lec2BeetleBand-v2 • Concepts illustrated • Enter name and select the type of each parameter • bandMember is an Object parameter • music is a Sound parameter • A parameter acts as a placeholder in the instruction • Arguments are passed to the parameter in the call to the method

  29. A Number parameter Add a Number parameter to specify the height the bandMember jumps up and down. Note that the call to the method must now include an argument for the height.

  30. Class-level Methods and Inheritance Alice

  31. Class-level Methods • Some actions are naturally associated with a specific class of objects. • Examples • A person walking • A wheel rolling • We can write our own methods to define an action for a specific class of objects -- a class-level method, instead of a world-level method we defined previously.

  32. An example (building technique) • How can we create a skate method for ice skater objects? • We need to: • Tell Alice to associate the new method (the one we are about to write) with an ice skater, and • Write a new method to animate the ice skater skating.

  33. Demo: The solution • First, to associate the animation with the ice skater • select the iceSkater tile in the Object Tree • select the methods tab in the details panel • click on the create new method button

  34. Storyboard for skate Skate: Do together move skater forward 2 meters Do in order slide on left leg slide on right leg • The slide actions each require several motion instructions, so we will break down these two actions into smaller steps

  35. Stepwise Refinement Refinement of slideLeft Do in order Lift right leg and turn upper body forward Lower right leg and return body upright Skate: Do together 1) move forward 2 meters 2) Do in order slideLeft slideRight Refinement of slideRight Do in order Lift left leg and turn upper body forward Lower left leg and return body upright

  36. Demo • Ch04Lec3Skater • Concepts illustrated in this example world • A method defined for a specific type of object defines an action for that object. • A method can call other methods. In this example, the skate method calls slideRight and slideLeft.

  37. Reuse • Writing methods to make an ice skater perform a skating motion is an intricate task. • We would like to have the iceSkater skate in other worlds without having to write the methods again. • The idea of being able to use previously written program code in another program is known as reuse.

  38. A new class 1) Rename iceSkater as cleverSkater. 2) Save out as a new class. Alice saves the new class as CleverSkater.a2c

  39. Inheritance • The CleverSkater class • inherits all the properties and methods from the original IceSkater class, and also • has the newly defined methods (skate, slideLeft, slideRight) • In other programming languages, the concept of creating a new class based on a previously defined class is called inheritance.

  40. Importing CleverSkater • An instance of the CleverSkater class can be added to a new world – use File|Import.

  41. Guidelines • To avoid potential misuse of class-level methods, follow these guidelines (encapsulation): • Avoid references to other objects • Avoid calls to world-level methods • Play a sound only if the sound has been imported and saved out as part of the new class • If these guidelines are not followed and an instance of the new class is added to another world, Alice will open an Error dialog box to tell you something is wrong.

  42. Bad Example What if there is no penguin in the new world where a cleverSkater object is imported?

  43. Problem • Suppose you really want to write a class-level method where another object is involved? • For example, a method to make the skater skate around another object-- in this scene, the penguin.

  44. Parameter • A solution is to write a class-level method with an object parameter that allows you to pass in the specific object. cleverSkater.skateAround Parameter: whichObject Do in order Do together cleverSkater turn to face whichObject cleverSkater lift right leg cleverSkater move to whichObject cleverSkater turn around whichObject

  45. Translation to Code • Most of the skateAround storyboard design is straightforward and easy to code. • One step, however, requires some thought: cleverSkater move to whichObject -- what distance should the cleverSkater move?

  46. Calling a built-in function • The instruction to move the skater to whichObject (penguin, in this example) would look like this: • Unfortunately, the skater will collide with the penguin because the distance between two objects is measured center-to-center.

  47. Expression • To avoid a collision, use a math operator to create an expression that adjusts the distance. • Math operators in Alice: addition + subtraction  multiplication * division / • Example:

  48. Demo • Ch04Lec3SkateAround • Concepts illustrated: • A parameter acts as a placeholder for the object that will be passed in • A call to the distance to function returns a number value • A math expression can be created as part of an instruction

  49. Assignment • Read Chapter 4 Section1 • World-level Methods • How to create them • How to call them • When it is appropriate to use them

More Related