Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties.

Similar presentations


Presentation on theme: "Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties."— Presentation transcript:

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

2 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).

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

4 Car Objects Clint's Porsche Jessica's BMW John's Lamborghini

5 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.

6 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.

7 Car Class and Objects Clint's Porsche Jessica's BMW John's Lamborghini Car

8 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.

9 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.

10 Classes Our text represents classes using the following graphic.

11 Car Class and Objects Clint's Porsche Jessica's BMW John's Lamborghini Car

12 Defining a Class  Let’s 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.

13 Defining a Class Properties or Instance Variables Behavior or Method 1 Behavior or Method 3 Behavior or Method 4 Behavior or Method 2

14 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.

15 Parameter Instance Values Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel GasLevel = 10 GasLevel = 4 GasLevel = 9

16 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.

17 Class Parameter Values Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 GasLevel = 10 GasLevel = 4 GasLevel = 9

18 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.

19 Class Parameter Values Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 GasLevel = 10 GasLevel = 4 GasLevel = 9

20 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.

21 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.

22 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.

23 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()

24 Messages Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() GasLevel = 10 turnOnHazards() GasLevel = 4 turnOnHazards() GasLevel = 9 turnOnHazards()

25 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.

26 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.

27 Methods Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) GasLevel = 10 turnOnHazards() addGas(amount) GasLevel = 10 turnOnHazards() addGas(amount) GasLevel = 9 turnOnHazards() addGas(amount)

28 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.

29 Methods Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 10 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 4 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 9 turnOnHazards() addGas(amount) getGasLevel():GasLevel

30 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.

31 Methods Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() GasLevel = 10 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 4 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 9 turnOnHazards() addGas(amount) getGasLevel():GasLevel

32 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.

33 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.

34 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.

35 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.

36 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.

37 Inheritance •Our Car class is very general. •Let's define a new class called BMW that contains the parameters: model, color, engine size.

38 Inheritance Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() BMW Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel

39 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?

40 Inheritance Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() BMW Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel Z3 Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel 3 Series Sedan Model Color EngineSize NumDoors GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel

41 Primitive Data Types • Primitive data types are basic data such as numbers, characters, and Boolean values.

42 Primitive Data Types • Java has four basic data types: – integer numbers •10, 6785943, -8 – real numbers •5.564, 8976.5687 – boolean •true, false – characters (Unicode)  ‘a’, ‘&’, ‘z’, ‘?’

43 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.

44 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,647 •usually the type you will use

45 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,807  When typing long integer constants into a program the number must have an ‘L’ at the end of the number. – 99999999999L

46 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 * 2 exponent

47 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.

48 Real Values • Real data types: – float uses 32 bits and represents the numbers -3.40292347E+38 to 3.40292347E+38 •The mantissa is 24 bits and the exponent is 8 bits. •The float type permits 6 to 7 decimal digits of precision.

49 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 bits •The double type permits 15 to 16 decimal digits of precision. •The double data type is more precise than float.

50 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.

51 Character Values • Character Values: – may represent both printable and non- printable characters  some printable characters are: ‘a’, ‘%’  some non-printable characters are: newline - ‘\n’, tab - ‘\t’, carriage return - ‘\r’ – What is unique about these characters?

52 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.

53 Boolean Values • Boolean data types – boolean uses 1 bit and represents the values true and false.


Download ppt "Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties."

Similar presentations


Ads by Google