1 / 14

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 your programs quickly begin to increase to many, many lines of code.

monifa
Download Presentation

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 your programs quickly begin to increase to many, many lines of code. • Games and other "real world" software applications can have thousands, even millions of lines of code. • In this session, we begin to look at organizing large programs into small manageable pieces.

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

  4. In your programs,you have been using… • Classes • In Alice, classes are predefined as 3D models • Objects • An object is an instance of a class. Class: Fishy (Uppercase name) Objects: fishy, fishy1, fishy2, fishy3 (lowercase names)

  5. In your programs,you have also used… • built-in (predefined) methods • Examples: move, turn to face, say • World.my first method • Example: In the snowpeople world (Chapter 2), we wrote program code where the snowman tried to get the attention of a snowwoman. As a reminder, see next slide…

  6. Modifying the program • To make the snowpeople animation more realistic, we might want the snowman to be a little less shy. In our original program, the snowman tries to catch the snowwoman's attention only once. Perhaps he could try to get her attention more than once. • To make this modification, additional lines would be added to the code. • This would make the program code longer and more difficult to read and think about.

  7. A Solution • A solution to the problem is to • define our own method • name the new method catchAttention • Then, we can drag-and-drop the catchAttention method into the edit box just like the built-in methods

  8. Demo: The solution • 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

  9. Demo • A demonstration of writing the catchAttention method

  10. World-level method • catchAttention is a world-level method because it • is defined as a method for World • has instructions that involve more than one object (snowman, snowwoman, camera)

  11. Using the catchAttention method The catchAttention method is executed by calling (invoking) the method from my first method

  12. 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 “catchAttention" instead of “turn head to face the camera, then say ‘Ahem’ while moving eyes up and down" • the technical term for "think at a higher level" is "abstraction"

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

More Related