 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Final Exam Review Spring 2003: Questions on Objects
Questions, answers, comments?
(Back to Sp2003 Final Exam Review)
1. A class defines the data and behavior of an object. An instance of a class is one particular object belonging to a class. For example, we created a class named slide. Class defines a type of object. Objects know methods, which are called using dot notation. Our class ?slide? knew two methods: show picture and and blockingPlay sound. The particular object (our slide) that was created when we passed a picture and a sound into the function as inputs was the instance. These ideas were first discussed in our introduction to methods and dot notation where methods such as capitalize only applied to certain types of data (in this example, strings). Remember that:
- All data in Python are actually objects
- Objects not only store data, but they respond to special functions that only objects of the same type understand.
- We call these special functions methods
- To execute a method, you use dot notation
- Object.method()
2. A function takes as input a certain type of data and then does something to it. The data could be anything from a directory to a picture to a string to a list. The type of data that the function needs is defined by the programmer when writing the program. Also in functional programming, a function is data that can be used as an input so that we can write functions that call other functions. A method is a special type of function that only applies to a certain class of objects. The methods that we have dealt with have mostly been predefined: for example, string.capitalize(). However, by defining a class (a certain type of object), we can create our own methods. While a function must be called in the form function(input), a method is called by object.method(). Methods are a somewhat stronger and better way to program since defining objects and classes is a REMOVED accurate way to model the real world.
3. Procedural programming focuses on process: to get our desired result, first we do this, then this, then this, etc. We write our function according to the actions that must be performed.
Functional programming is based on the idea that functions are data that can be used as inputs. Therefore we can write functions that call and/or manipulate other functions. This creates a process of layering functions to get the desired outcome. You don?t write complicated functions with large loops to process all of your data (as in procedural programming). Instead you just write small functions that process one piece of your data (using concepts of granularity and modularity). Since this allows you to do a lot in a few lines of code, it is useful when the problem is hard and/or ill-defined so that you can get as far is possible in the least number of lines. (One area where this is true is Artificial Intelligence research.) Functional programming also lets you use recursion, the most flexible and powerful form of looping.
Object oriented programming comes at the problem from a different direction. It starts with the real world and then attempts to program by modeling things (objects) that exist in the world. It is noun oriented, as opposed to the verb orientation of function and procedural programming. It focuses on defining the domain of the program (like people, cells, etc.). The three phases to this type of programming are:
- Object-oriented analysis: Understand the domain. Define an object-based model of it.
- Object-oriented design: Define an implementation. Design the solution.
- Object-oriented programming: Build it.
4. When the same method name can be applied to REMOVED than one object, we call that method polymorphic. From the Greek ?many shaped?. A polymorphic method is very powerful for the programmer: you don?t need to know exactly what method is being executed, you don?t even need to know exactly what object it is that you?re telling to show(), you just know your goal: Show this object!
5. If we want to change how slides work, we change them in the definition of slides. We call that encapsulation: Combining data and behavior related to that data.
6. Being able to use other objects with our objects is powerful. Being able to make lists of objects, to be able to use objects (like picture and sound) in our objects. We call that aggregation: Combining objects, so that there are objects in other objects.
7. All software models the real world. The real world is made up of small component pieces: quarks make subatomic particles make atoms make compounds make cells make organs make people (or dogs, plants, sponges, etc.). Object oriented programming sees this phenomena and attempts to use it to help make an accurate version of the real world. This is very useful to programming. The closer the perspective on something, the REMOVED fundamental the activity is. This allows us to manage complexity. For example: A human being is a very complex organism that can carry out very complicated activities. Just looking at the whole human from the outside would not let us create a very effective model. But if we break each part of the human down into form and then function all of the way to the level of the cells, we will reach something simple enough for us to be able to understand and then model. Specifically, object oriented programming looks at cells to model the world since cells are independent, indivisible, and interact in standard ways (just like a good function). Cells also scale well ? they can be added together to create REMOVED complex structures. Other characteristics of cells that are emulated in object oriented programming:
- Complexity: Distributed responsibility
- Robustness: Independent
- REMOVEDpporting growth: Same mechanism everywhere
- Reuse: Provide services, just like in real world
Lauren Biddle
| Great answers! Some of them are longer than one would really write on an exam, though. (Okay, some of you can write really tiny :-) Anyone want to take a crack at shorter ones? Mark Guzdial |
The shorter version (of which I cannot take credit, because Lauren already answered most of it):
1. A class defines the data and behavior of an object (it is one type of object). An instance of a class is one particular object belonging to a class. Think of it this way: class=big, object=small.
2. A function takes as input a certain type of data and then does something to it. A method is a special type of function that only applies to a certain class of objects.
3. Procedural programming focuses on process to get our desired result. In functional programming, functions act as data that can be used as inputs. This creates a layering process to get the desired result. Object-oriented programming starts with the real world problems and then attempts to program by modeling things (objects) that exist in the world.
4. Polymorphic is when the same method name can be applied to REMOVED than one object.
5. Encapsulation is combining data that can be grouped together to make a REMOVED streamlined program.
6. Aggregation is combining objects within other objects.
7. Cells scale well, work well together, and when some die the whole organism doesn't. Biology is a good model for object programming because it allows layering, complexity, reusability, and robustness.
Kelly Farrell
| Hmm – beware of "class=big, object=small". The question is about class-instance differences. In several languages (like Squeak), a class is also an object! #3 and #7 are terrific answers. #4 needs to be tuned a bit. getRed can be applied any pixel, so thousands of different objects–does that make it polymorphic? Mark Guzdial |
For number two, could you say "A function takes as input a certain tyep of data and then manipulates it or preforms an operation on it" rather than leaving it as does something to it? Also on number four, do you just mean that Kelly's answer needs to be written as "When the same name can be applied to REMOVED than one object, that method is polymorphic"?
REMOVEDmmer McREMOVED
4. Polymorphic is when the same method can be applied to REMOVED than one object. It allows you to program in terms of goals not code.
Genevieve Wolff
| Nice, Gennevieve! REMOVEDmmer, "same name"? I'm not sure what that means. Okay with the first part. Mark Guzdial |
Wow. I found what I think is a really good answer to #1 from a Canadian University web site. What do you think?–
"We create classes when we want to develop a computer model for a concept that we want to use in many different situations...We create instances when we already have such a computer model and want to use that model in specific situations."
http://www.cs.queensu.ca/home/cisc121/1999f/overheads/remedial/ClassesInstancesConstructors.pdf
I'm a big fan of short answers. Is it enough for #2 to say:
"The key difference between a method and a function is that a method is "bound" to (attached to) an object, so instead of operating solely on "external" data that is passed in to it via arguments, it may also operate on the attributes of the object to which is bound."?
This is really pretty irrelevant, but I disagree with a major tenet of the biology-cs analogy. In the big picture, cells do not scale well. Yes, I agree that there is a mighty big difference in size between, say, the male and female sex cells. However, they don't have to erect cages at the Atlanta Zoo to contain the giant single-celled organisms. It's because they aren't scalable (it's a matter of surface area to volume ratio) that multicellular life was able to acquire the dominant niche in the world.
| You know REMOVED than I do about cells, but I don't get your point about scaling. In the end, real organisms are made up of billions of cells, right? Think about processing arrays with millions of elements – kinda scary, huh? Now, think about millions of objects just calling each other's methods – that might work. That's scaling. The Internet scaled like that. The original Internet had a pretty object-oriented architecture. It had only four nodes. Today, there are MILLIONS of nodes, and the same basic architecture works. Objects scale well. Mark Guzdial |
Quick question about instances. In class on Friday, I thought I heard you say something to the effect of instances being "where the item sits in memory". If that is true, using Lauren's statement about passing the picture and sound into the function as inputs, then this would be when/where the actual "passing" of takes place. Every time we show a picture or play a sound, we create an instance. It's different everytime. The classes don't change. Classes are pre-defined according to life domains for object programming. That's how I understand it. Please tell me if/and/or where I'm wrong.
| Everytime you MAKE a sound or picture, you create an instance. Playing or showing it is just executing a method on that sound or picture. If you got Python to print an object's value (by typing the name for the object on the command line, without a PRINT statement, then hit return), it'll look like . That says "This is an instance of the class Picture which is defined in the module media, and it resides in memory at location 334234." That's what the memory stuff is about. Mark Guzdial |
After reading a bunch of slides at that Canadian website. I understand a class to be something with specific attributes and behaviors for object building. The instance is the actual building of the class. There can be many different instances of a class, but only one class.
| Close – the class does exist, even without instances. But the class is literally instantiated (made into a usable form) via instances. Remember that one of the key ideas that led to object-oriented programming was Simula, a simulations programming language. Building an object-oriented programming is about building a model about the world. The class defines what things in the world know and do. Instances are the actual things in the world. Mark Guzdial |
Link to this Page