Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming April 13, 2007. Problem Description “ …Wombats are allowed to have different directions and number of eaten leaves, move,

Similar presentations


Presentation on theme: "Object Oriented Programming April 13, 2007. Problem Description “ …Wombats are allowed to have different directions and number of eaten leaves, move,"— Presentation transcript:

1 Object Oriented Programming April 13, 2007

2 Problem Description “ …Wombats are allowed to have different directions and number of eaten leaves, move, turn, find leaves and eat leaves”

3 Mapping the world to software Objects in the problem domain are mapped to objects in software 011101 10011 11101 0110100 11010 010101 1110101 10101

4 Object Oriented Data and operations are grouped together WombatWorld

5 Data Encapsulation class Wombat { private int direction; private int leavesEaten; public Wombat() {… } public move() { … }

6 Advantages of Encapsulation Protection Consistency Allows change

7 Objects and Classes Classes reflect concepts, objects reflect instances that embody those concepts. Dana Jane Emily Sarah woman class object Not Fred

8 Objects and Classes cont’d Class BankAccount Balance InterestYTD Owner Account_number Balance:500 InterestYTD: 10 Owner: Mark Account_number: 12345 Balance 10,000 InterestYTD: 25 Owner: Martha Account_number: 76543 Operations MakeDesposit Transfer WithDraw GetBalance

9 Objects as instances of Classes The world conceptually consists of objects Many objects can be said to be of the same type or class My bank account, your bank account, Bill Gates’ bank account … We call the object type a class

10 Instantiation An Object is instantiated from a Class BankAccount myAccount; myAccount = new BankAccount();

11 Objects and Classes Class Visible in source code The code is not duplicated Object Own copy of data Active in running program Occupies memory Has the set of operations given in the class

12 Classification Mammal RodentPrimateCat Reptile Animal SquirrelRabbitMouse

13 Classification WombatRock Actor

14 Inheritance A class which is a subtype of a more general class is said to be inherited from it. The sub-class inherits the base class’ data members and member functions

15 Inheritance cont’d A sub-class has all data members of its base-class plus its own A sub-class has all member functions of its base class (with changes) plus its own Look at the wombatWorld -- what Actor functions or methods are used in the Wombat class?

16 Polymorphism Polymorphism comes from the Greek language and means ”many shapes”. Overloading One interface with multiple implementations or methods. Each method must have a distinct signature. Overriding When a subclass has a method with the same signature (number, type and order of parameters) as a method in one of its superclasses.

17 Overloading Example One interface with multiple implementations or methods. Each method must have a distinct signature. Example: fill() fill(gray) fill(gray, alpha) fill(value1, value2, value3) fill(value1, value2, value3, alpha) fill(color) fill(color, alpha) fill(hex) fill(hex, alpha)

18 Polymorphism Parameters gray int or float: number specifying value between white and black alpha int or float: opacity of the fill value1 int or float: red or hue value value2 int or float: green or saturation value value3 int or float: blue or brightness value color color: any value of the color datatype hex int: color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)

19 Overriding : Example Look at the wombat class and the actor class: each has an act() method. A wombat instance of the Wombat class will use the wombat act(). (Check out the Actor class’s act() method. What does it do?)

20 What is a good class ? A class abstracts objects A class should be non-trivial in the context of the program (has data structures and operations different from other classes)

21 Class Design Use “is a” or “has a” to determine whether something should be a subclass or be a data item. Ex: A dog has a name. Not: A dog is a name. A dog is a mammal. Not: A dog has a mammal. (unless its in predator-mode which isn’t a step in oo design).

22 Summary What is Object Oriented Programming? Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships

23 Importance of Documenation The Greeps assignment will require you to read the documentation. As practice for the Greeps assignment, in class today create the car game found at http://www.greenfoot.org/greenroom/images/c /c2/Car_Game_Tutorial.zip http://www.greenfoot.org/greenroom/images/c /c2/Car_Game_Tutorial.zip The line image isn’t included -- use whatever you want! What method has replaced stopSimulation() in Greenfoot? What method can you use to play a sound?

24 Things to Note getX() and getY() are methods of the base class Actor but are used in the Car class. World class methods used in Car class include removeObject(), getHeight() and addObject(). They are called by getWorld().method-call Example: getWorld().removeObject(this)


Download ppt "Object Oriented Programming April 13, 2007. Problem Description “ …Wombats are allowed to have different directions and number of eaten leaves, move,"

Similar presentations


Ads by Google