object-oriented programming object-oriented programming (oop) allows you to create your program...

53
Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects. Your programs properties and behaviors are modeled based upon real objects like cars, books, houses, etc.

Upload: buck-jeffrey-doyle

Post on 14-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Object-Oriented Programming

Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects. Your programs properties and behaviors

are modeled based upon real objects like cars, books, houses, etc.

Page 2: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Objects

An object is a thing, either tangible or intangible. When you write an object-oriented program

it consists of many objects. An object is composed of properties

(attributes, data) and behaviors (operations) .

Page 3: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Objects

Our textbook uses a rectangle with rounded corners to represent objects. (I will use an oval).

<Object Name>

Page 4: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Car Objects

Clint's Porsche

Jessica's BMW

John's Lamborghini

Page 5: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Classes

Classes are the definitions (or blue prints) used to create objects. To make a car the manufacturer must first

have a design from which to build the first car. Then once all the problems are worked out, the design is used to build all the cars of that model.

Page 6: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Classes and Objects

An object is an instance of a class. If we have a class definition called Car, then

we can think of Clint's Porsche, Jessaca's BMW, and John's Lamborghini as each being an instance (object) of the class Car. They are each a type of car.

Page 7: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Car Class and Objects

Clint's Porsche

Jessica's BMW

John's Lamborghini

Car

Car

Car

Page 8: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Classes and Objects

An object is an instance of exactly one class. John's Lamborghini can not be an instance

of a car class and an instance of a plane class at the same time. It is only an instance of one class.

An instance of a class (object) belongs to that particular class. A Lamborghini is a car. So John's

Lamborghini belongs to the class Car.

Page 9: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Classes

Once a class is defined you can create as many instances of the class (objects from the class) as you would like. Once a blue print is completed for the 2003

Porsche 911, Prosche will use an assembly line to build as many instances of the 2003 Prosche 911 as they wish.

Page 10: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

ClassesOur text represents classes using the following graphic.

<Class Name>

Page 11: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Car Class and Objects

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

Car

Page 12: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Defining a Class Lets look at our car class

Properties are variables which describe the essential characteristics of an object. For a car they are: color, model, make, how many doors, transmission type, direction of movement, etc.

Behaviors are methods that describe how the object behaves and how the behaviors may be modified. For a car they are: braking, changing gears, opening doors, painting the car, moving forwards or backwards, etc.

Page 13: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Defining a Class

Properties or

Instance Variables

Behavior orMethod 1

Behavior orMethod 3

BehaviororMethod 4

BehaviororMethod 2

Page 14: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Parameter Values

The class definition will include parameter definitions (properties) that represent data about a particular object. For instance the fact that Jessica's car has 4 gallons of

gas in it while Clint's car has 10 gallons of gas.

The amount of gas in each car will change without affecting the amount of gas in the other cars.

These are called instance variables.

All instances (objects) of a class will have a set of instance variables that are specific to the individual object.

Page 15: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Parameter Instance Values

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevel

GasLevel = 10 GasLevel = 4 GasLevel = 9

Page 16: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Parameter Values

The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects). In the case of a the car class we will define

a maximum gas level that represents the amount of gas the gas tanks can hold. This will be the same for each individual car.

These types of parameters are called class variables.

Page 17: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Class Parameter Values

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevelMaxGasLevel = 18

GasLevel = 10 GasLevel = 4 GasLevel = 9

Page 18: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Parameter Values

Class variables may also be used to keep track of things like how many instances of a class exist. For our car example, lets create a counter

the records how many cars we have built.

Page 19: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Class Parameter Values

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevelMaxGasLevel = 18NumCars = 3

GasLevel = 10 GasLevel = 4 GasLevel = 9

Page 20: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Constant Parameters

It is possible that you will have a parameter whose value should not change while your program is running. Such a parameter is called a constant. The MaxGasLevel parameter that we

defined for the Car class is a constant. The amount of gas the gas tank can hold will not change once the car has been built.

Page 21: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Messages

Objects communicate much like humans do, they pass messages back and forth to one another. Each message has three components:

The object to whom the message is addressed. The name of the method to perform in that object. Any parameters (variables) needed by that method.

Page 22: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Messages

When we "send a message" directly to another person our message will include: An indication of who the message is

intended for. What task we want the person to do. What the information is that they need to

complete the task.

Page 23: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Messages In object-oriented programming we must

explicitly state to what object we are sending a message (the object name), what task (method) we want the object to complete, and any required information (parameters). For instance, we will define a task to turn on the

car's hazards in the Car class. If we want to tell Jessica to turn on her BMW's hazards we would say something like. Jessica's BMW.turnOnHazards()

Page 24: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Messages

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevelMaxGasLevel = 18NumCars = 3turnOnHazards()

GasLevel = 10turnOnHazards()

GasLevel = 4turnOnHazards()

GasLevel = 9turnOnHazards()

Page 25: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Messages and Methods

In order to process a message an object needs to have a method defined for the requested task. For instance, we have to define a method to

turn on the car's hazards. A method is a small well defined piece of

code that completes a specific task.

Page 26: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Instance Methods Each class can have methods that are

specific to each object. This means that when the method is executed,

it only affects the instance variables of the object. If Jessica's BMW has 4 gallons of gas and she puts 6

more gallons of gas in her car, she now has 10 gallons. The amount of gas in Clint's Porsche and John's Lamborghini does not change because Jessica put gas in her BMW.

Methods that only affect the object's parameters are called instance methods.

Page 27: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Methods

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevelMaxGasLevel = 18NumCars = 3turnOnHazards()addGas(amount)

GasLevel = 10turnOnHazards()addGas(amount)

GasLevel = 10turnOnHazards()addGas(amount)

GasLevel = 9turnOnHazards()addGas(amount)

Page 28: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Methods

It is also possible that you want information about an object, in this case you would define a method that sends (returns) a message back containing the information. We need to know how much gas is in our

cars, so we will create a new method that returns the value of GasLevel for our car.

Page 29: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Methods

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevelMaxGasLevel = 18NumCars = 3turnOnHazards()addGas(amount)getGasLevel():GasLevel

GasLevel = 10turnOnHazards()addGas(amount)getGasLevel():GasLevel

GasLevel = 4turnOnHazards()addGas(amount)getGasLevel():GasLevel

GasLevel = 9turnOnHazards()addGas(amount)getGasLevel():GasLevel

Page 30: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Class Methods

Class methods are used to get or manipulate information about all objects created from the class. Typically, class methods are changing class

variables. Every time we build a new car we need to add

one more to the number of cars we have built. Also, we may want to know how many cars total

we have built.

Page 31: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Methods

Clint's Porsche Jessica's BMW John's Lamborghini

Car Car Car

CarGasLevelMaxGasLevel = 18NumCars = 3

turnOnHazards()addGas(amount)getGasLevel():GasLevelincrementNumCars()howManyCars()

GasLevel = 10

turnOnHazards()addGas(amount)getGasLevel():GasLevel

GasLevel = 4

turnOnHazards()addGas(amount)getGasLevel():GasLevel

GasLevel = 9

turnOnHazards()addGas(amount)getGasLevel():GasLevel

Page 32: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Object-Oriented Programming

When writing object-oriented programs, first one must define the classes (like car).

Then while the program in running the instances of the classes (or objects) (like Jessica's BMW, Clint's Porsche, and John's Lamborghini) are created.

Page 33: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Object-Oriented Programming

Object-oriented programming allows the programmer to hide the implementation details from the other objects and the users. In other words the implementation is

transparent to the other objects or the user. Examples: Word, Excel, Web pages.

Page 34: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Object-Oriented Programming

Benefits of Object-oriented programming: An object can be written and maintained

separately from the rest of the program, modularity.

An object has a public face that it uses to communicate with other objects but the other objects can not directly access the instance variables, Information Hiding.

Page 35: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Inheritance All classes in Java are organized into a

class hierarchy. The highest level classes are very general

and the lower level classes are more specific.

The lower level classes are based upon the higher level classes and inherit the higher level class instance variables and methods. They also contain their own instance variables and methods beyond the higher level class definition.

Page 36: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Inheritance The higher level classes are called

Superclasses and the new or lower level classes are called the subclasses. A subclass can always become a

superclass

Inheritance allows you to define certain behaviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.

Page 37: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Inheritance

Our Car class is very general. Let's define a new class called BMW that

contains the parameters: model, color, engine size.

Page 38: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

InheritanceCar

GasLevelMaxGasLevel = 18NumCars = 3

turnOnHazards()addGas(amount)getGasLevel():GasLevelincrementNumCars()howManyCars()

BMWModelColorEngineSize

GasLevel

turnOnHazards()addGas(amount)getGasLevel():GasLevel

Page 39: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Inheritance

Now let's define two new classes. One for the Z3 and another for the 3 Series Sedan. What would be some of the differences

between the two classes?

Page 40: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

InheritanceCar

GasLevelMaxGasLevel = 18NumCars = 3

turnOnHazards()addGas(amount)getGasLevel():GasLevelincrementNumCars()howManyCars()

BMWModelColorEngineSize

GasLevel

turnOnHazards()addGas(amount)getGasLevel():GasLevel

Z3ModelColorEngineSize

GasLevel

turnOnHazards()addGas(amount)getGasLevel():GasLevel

3 Series SedanModelColorEngineSizeNumDoors

GasLevel

turnOnHazards()addGas(amount)getGasLevel():GasLevel

Page 41: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Primitive Data Types

Primitive data types are basic data such as numbers, characters, and Boolean values.

Page 42: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Primitive Data Types

Java has four basic data types: integer numbers

10, 6785943, -8

real numbers5.564, 8976.5687

booleantrue, false

characters (Unicode) a, &, z, ?

Page 43: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Primitive Data Types

The Java primitive data types each have a set size (bits) and value range. This size and range are the same on every

type of computer (PC, Mac, Sun workstation) that runs Java programs. This is not true with other programming languages.

Page 44: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Integer Values

Integer data types: byte uses 8 bits and represents the

numbers -128 to 127 short uses 16 bits and represents the

numbers -32,768 to 32,768 int uses 32 bits and represents the

numbers -2,147,483,648 to 2,147,483,647usually the type you will use

Page 45: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Integer Values

Integer data types: long uses 64 bits and represents the

numbers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807When typing long integer constants into a program the number must have an L at the end of the number.

99999999999L

Page 46: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Real Values Real or floating point numbers are

represented in scientific notation inside a computer. The number is divided up into two parts the mantissa and the exponent. The mantissa contains a number between -1.0

and 1.0 The exponent contains the power of 2 required to

scale the number to its actual value. Value = mantissa * 2exponent

Page 47: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Real Values Real numbers are characterized by their

precision and range. Precision represents the number of

significant digits that is represented in the number.Precision depends upon the number of bits in the mantissa.

Range represents the difference between the largest and smallest numbers that can be represented.Range depends upon the number of bits in the exponent.

Page 48: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Real Values Real data types:

float uses 32 bits and represents the numbers -3.40292347E+38 to 3.40292347E+38The mantissa is 24 bits and the exponent is 8 bits.The float type permits 6 to 7 decimal digits of precision.

Page 49: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Real Values Real data types:

double uses 64 bits and represents the numbers -1.79769313486231570E+308 to 1.79769313486231570E+308. The mantissa is 53 bits and the exponent is 11 bitsThe double type permits 15 to 16 decimal digits of precision.The double data type is more precise than float.

Page 50: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Real Values

Round off Error When a double is stored to a float, part of

the number is lost because a double is much larger than a float. The float can not represent the same exact number. In this situation the seven most significant digits are preserved and the rest of the information is lost.It is important to know which real data type is required for your calculation.

Page 51: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Character Values

Character Values: may represent both printable and non-

printable characterssome printable characters are: a, %some non-printable characters are: newline - \n, tab - \t, carriage return - \r

What is unique about these characters?

Page 52: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Character Values

Character data types: char uses 16 bits and represents the

characters \u0000 to \uFFFF Unicode character set which represents almost every modern language.

Page 53: Object-Oriented Programming Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your programs properties

Boolean Values

Boolean data types boolean uses 1 bit and represents the

values true and false.