View
215
Download
2
Tags:
Embed Size (px)
Citation preview
Fall 2008 ACS-1805 Ron McFadyen 1
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
Fall 2008 ACS-1805 Ron McFadyen 2
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
Fall 2008 ACS-1805 Ron McFadyen 3
Classes Each class has properties, methods, functions
We can customize … new properties, methods, functions
Fall 2008 ACS-1805 Ron McFadyen 4
Objects Showing the relationship amongst classes and objects:
Teacher
fung-yee ron
Student
april joe janet
Fall 2008 ACS-1805 Ron McFadyen 5
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
Fall 2008 ACS-1805 Ron McFadyen 6
World-level MethodsIf 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
Fall 2008 ACS-1805 Ron McFadyen 7
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.
Fall 2008 ACS-1805 Ron McFadyen 8
World-level Methods (4-1)Original: Code is placed in a new method:
Fall 2008 ACS-1805 Ron McFadyen 9
World-level MethodsOriginal: Revised:
• Original was long, complex
• Revised version more easily understood
Call/invoke/execute the method react
Fall 2008 ACS-1805 Ron McFadyen 10
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”
Fall 2008 ACS-1805 Ron McFadyen 11
Parameters
A method with a parameter:
Some original code:
Fall 2008 ACS-1805 Ron McFadyen 12
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?
Fall 2008 ACS-1805 Ron McFadyen 13
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
Fall 2008 ACS-1805 Ron McFadyen 14
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; distance to move
object
Fall 2008 ACS-1805 Ron McFadyen 15
Example
Put toys away
pickup putInCloset
putdown
We can represent the overall organization of our methods (i.e. who calls who)
Fall 2008 ACS-1805 Ron McFadyen 16
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
Fall 2008 ACS-1805 Ron McFadyen 17
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 …