Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes, Objects, and World-level Methods

Similar presentations


Presentation on theme: "Classes, Objects, and World-level Methods"— 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) One object acts on another by calling the other objects methods. Another way to think of this is that one object is "sending a message" to the other object. For example, a "Driver" object might send a message to a "Car" object to turn right. Named messages correspond to named methods and functions in a class definition.

5 Class vs. Object Class: Object: 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 A class is like a blueprint for creating a specific kind of object. Even better, it is like a factory for creating a certain type of objects. Objects are specific instances of classes that have been created for use in a particular program. The first object created in a program from a class has the same name as the Class, which can be a little confusing. But if you create multiple objects from the same class, they will automatically receive unique names, like Ambulance2, etc.

6 Class vs. Object class Person beth derek maria pete … objects
Don't confuse classes with the groupings Alice uses to display related objects. Since there is no "Person" class in Alice, a better example might be something like using the "RandomGirl" class to create instances named Beth, Maria, Susan, etc. objects

7 More Specifics height weight eye color talk walk twiddle thumbs Person
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) Using lower case names for objects isn't consistent across all classes supplied with Alice, but it is a nice way of distinguishing between the Class that generates an object and the objects it generates.

9 We have also used… Built-in (predefined) methods World.my first method
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: Don’t worry about details at this level
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


Download ppt "Classes, Objects, and World-level Methods"

Similar presentations


Ads by Google