Download presentation
Presentation is loading. Please wait.
1
Fall 2008ACS-1805 Ron McFadyen1 Programming Concepts Chapter 4 introduces more advanced OO programming techniques. Construction of a programs usually requires: Classes Objects Methods World-level Class-level Parameters Inheritance
2
Fall 2008ACS-1805 Ron McFadyen2 Classes An OO program is organized around the concept of class. For Alice programming, all classes are pre-defined for us. We choose the classes from the gallery. (We need other skills to build classes) If interested, you would explore products: Maya, Max Studio, … These are used to construct 3D models Building classes is outside the scope of this course Classes: comprise properties, methods, functions
3
Fall 2008ACS-1805 Ron McFadyen3 Classes Each class has properties, methods, functions We can customize … new properties, methods, functions
4
Fall 2008ACS-1805 Ron McFadyen4 Objects Showing the relationship amongst classes and objects: Teacher fung-yeeron Student apriljoejanet
5
Fall 2008ACS-1805 Ron McFadyen5 Methods Methods define what an object (from the class) can do Move, Turn, Roll, etc We can’t see the code for these. (The Alice creators don’t want us to change them and they are referred to as primitive methods) We can customize an object by giving it some new methods These you can edit (change) OO programmers typically use many many methods where each is defined to some fairly simple thing Methods give us a way of organizing the complexity of our creations Methods become even more useful when they incorporate parameters
6
Fall 2008ACS-1805 Ron McFadyen6 World-level Methods If we have a complex method, we can break it up into smaller simpler tasks. Main idea: take a group of related instructions in one method, place them in a separate method, replace the original lines with a call to the new method Principle being applied is abstraction – we are now thinking of the group of instructions as a single instruction
7
Fall 2008ACS-1805 Ron McFadyen7 Stepwise refinement When creating an algorithm we can design a solution in terms of solutions to smaller problems. This approach is one where we divide and conquer we break a task down into a number of sub-tasks, each of which is described by an algorithm that is smaller and simpler than that for the entire process. Each sub-task may require further sub-division until we have divided up the problem into elementary pieces, each of which can be tackled in a simple and straightforward way.
8
Fall 2008ACS-1805 Ron McFadyen8 World-level Methods (4-1) Original: Code is placed in a new method:
9
Fall 2008ACS-1805 Ron McFadyen9 World-level Methods Original:Revised: Original was long, complex Revised version more easily understood Call/invoke/execute the method react
10
Fall 2008ACS-1805 Ron McFadyen10 Parameters (4-2) A method is more useful if it can work for different objects A robot walks by turning a leg part backward and then forward We can place these two “turns” into a method with a parameter … where the parameter specifies the part that “turns”
11
Fall 2008ACS-1805 Ron McFadyen11 Parameters A method with a parameter: Some original code:
12
Fall 2008ACS-1805 Ron McFadyen12 Parameters Revised: Original: An argument supplies a value to be substituted for a method’s parameter … at run time What different kinds of parameters does Alice allow?
13
Fall 2008ACS-1805 Ron McFadyen13 Consider Project 4, Cleanup Robot, page 117 We are told to use 3 methods … to make it easier to understand a solution … to make it easier to develop the solution Put Toys Away For each toy: pickup- robot goes and picks up the toy putInCloset- robot takes the toy to the closet Example solution is given in terms of solutions to 2 smaller problems
14
Fall 2008ACS-1805 Ron McFadyen14 Example pickup- robot goes and picks up the toy Robot turns to face the toy Robot moves toward the toy Robot picks up toy putInCloset- robot takes the toy to the closet Robot turns to face the closet Closet door opens Robot moves into the closet putdown- robot places toy in closet Robot exits from the closet Closet door closes putdown- robot places toy in closet Robot lowers his arms Robot releases the toy Parameters object; distance to move object
15
Fall 2008ACS-1805 Ron McFadyen15 Example Put toys away pickupputInCloset putdown We can represent the overall organization of our methods (i.e. who calls who)
16
Fall 2008ACS-1805 Ron McFadyen16 Class-level methods & 3D Models (4-3) Consider that we have developed new methods (perhaps some new properties and functions too) In-class example … We can save our object as a new 3D model … right-click … save object as … note file type is a2c Now we, or someone else, could import objects that are created according to that 3D model
17
Fall 2008ACS-1805 Ron McFadyen17 Inheritance (4-3) The new model, Clever Skater, exhibits a feature of OO programming called inheritance … Clever Skater has methods that were originally part of Ice Skater, along with the new methods we designed We say Clever Skater inherited those original methods from the Ice Skater model. Inheritance is a way for us to develop programs or models and share them (i.e. sharing code) with others. Others may use these models exactly as we create them, or they may extend them by changing methods or adding new ones. This is similar to inheritance in languages like Java … but not exactly …
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.