150 likes | 163 Views
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.
E N D
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
Can’t see all the code even for our example from last class!
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)
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
Class vs. Object class Person beth derek maria pete … objects
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
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)
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?
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
Chicken Baseball • Large tasks: • chicken swings • chicken runs • chicken slides • chicken throws • chicken catches • Don’t worry about details at this level • abstraction
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
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
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