1 / 15

Classes, Objects, and World-level Methods

Classes, Objects, and World-level Methods. Alice. Larger Programs. Complex programs require many lines of code (LOC) Games, movies: 1000s or millions of LOC How to handle such large programs? Break up into smaller, manageable pieces.

ahancock
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 • Complex programs require many lines of code (LOC) • Games, movies: 1000s or millions of LOC • How to handle such large programs? • Break up into smaller, manageable pieces

  3. Can’t see all the code even for our example from last class!

  4. Classes, Objects, & Methods • Object-oriented programming (OOP): • program consists of objects acting on each other • each object is an “actor” with its own role • OOP helps to • organize programs into small modules • ease the design process • make code reusable • find and remove errors (bugs)

  5. Class vs. Object • Class: • a conceptual framework for an entity • describes properties, methods, functions • cannot be used directly in execution • Object: • a realization of a class in a program • instance of • used directly during execution

  6. Class vs. Object class Person beth derek maria pete … objects

  7. More Specifics properties methods height weight eye color talk walk twiddle thumbs Person 6’ 3” 285 lbs. dreamy blue 6’ 0” 175 lbs. exotic green derek prof. L

  8. 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)

  9. We have also used… • Built-in (predefined) methods • Examples: move, turn to face, say • World.my first method • provided automatically by Alice • started when you press the “run” button • we have written all code in this method • What about writing other methods?

  10. Solution to One Long Method • Organize instructions into separate smaller methods • Stepwise refinement: • break a problem into large tasks • then break each task into simpler steps

  11. Chicken Baseball

  12. Chicken Baseball • Large tasks: • chicken swings • chicken runs • chicken slides • chicken throws • chicken catches • Don’t worry about details at this level • abstraction

  13. Chicken Runs • This will be its own method • Consider details: • turn to face a base • in a loop, do together: • move 0.5 meters forward • turn legs forward/backward

  14. Why? • Why write our own methods? • saves time: call the method again and again • reduces code size: no need to write the instructions again and again • allows us to "think at a higher level" • can think chicken run instead of “The chicken turns to face the base, then loop doing… ” • easier to find bugs

  15. Creating 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

More Related