object oriented concepts

23
1 OBJECT ORIENTED CONCEPTS Chapter 2

Upload: afi

Post on 14-Feb-2016

22 views

Category:

Documents


0 download

DESCRIPTION

OBJECT ORIENTED CONCEPTS. Chapter 2. Introduction. In the real world, there are many individual objects all of the same kind . There may be thousands of cars, all of the same make and model. Each car was built from the same set of blueprints and therefore contains the same components . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: OBJECT ORIENTED CONCEPTS

1

OBJECT ORIENTED CONCEPTSChapter 2

Page 2: OBJECT ORIENTED CONCEPTS

2

Introduction In the real world, there are many

individual objects all of the same kind. There may be thousands of cars, all of the same make and model. Each car was built from the same set of blueprints and therefore contains the same components.

In object-oriented terms, we say that your car is an instance of the class of objects known as cars.

Many objects can be instantiated from a single class.

Therefore a class is an abstract descriptor for a set of instances.

Page 3: OBJECT ORIENTED CONCEPTS

3

Class A class is the blueprint from which

individual objects are created These objects have similar

characteristics (common structure, common behaviour, common relationship and common semantics).

Semantics is a set of rules that specify the meaning of syntactically legal construct in a programming language.

Classes are sometimes referred to as entity classes or model class  

Page 4: OBJECT ORIENTED CONCEPTS

4

Class For example, a Car class may be used to

represent the common state (e.g., make, model, colour, mileage, etc...) and behaviour (e.g., start, stop, changeGear, turn etc...) between all car objects

a 2DShape class may represent common state (e.g., centerPoint, lineWidth) and behaviour (e.g., calculateArea, calculatePerimeter)  for 2D shapes.

Page 6: OBJECT ORIENTED CONCEPTS

6

TASK 1 List out the classes that you think

are involve in the Student Information System?

Page 7: OBJECT ORIENTED CONCEPTS

7

CLASS Class

table/notation is used to model the static structure of a system which is based on data

it contains the name of class, attributes and methods

Class name(singular)

List of attributes

List of methods

Page 8: OBJECT ORIENTED CONCEPTS

8

A class Car represented in class notation.

CarmakemodeldoorsbodyLengthengineSizecolourspeed

stop()turn()accelerate()brake()

Class name

attributes

methods

Class

Page 9: OBJECT ORIENTED CONCEPTS

9

Implementation in JavaThe following codes implements a class car (it is just a blueprint that might be used in an application) :

class car { String make = “ “; String model = “ “; int doors=0; int bodyLength = 0; int engineSize = 0; String colour = “ “; int speed = 0;

void accelerate(int increment) {

speed = speed + increment; }

void brake(int decrement) { speed = speed -

decrement; }

}

Page 10: OBJECT ORIENTED CONCEPTS

10

The following carDemo class will creates 2 car objects and invoke their methods:

class carDemo { public static void main(String[] args) { // Create two different car objects car car1 = new car(); car car2 = new car();

// Invoke methods on those objects car1.accelerate(50); car2.brake(20) ;}

Implementation in Java

Page 11: OBJECT ORIENTED CONCEPTS

11

Objects Software objects are conceptually

similar to real-world objects They too consist of state and related

behavior. An object stores its state in fields

(variables in some programming languages) and exposes its behavior through methods (functions in some programming languages) attributes – they know things behavior (methods) - they do things

Page 12: OBJECT ORIENTED CONCEPTS

12

Objects object of the same type will have

an identical set of attributes Attributes of one object may

contain different data values from those of another object.

Page 13: OBJECT ORIENTED CONCEPTS

13

Objects Object can be considered as a

container for a set of data and the operation that need to be performed on it.

Properties are : It has a unique identity for the lifetime of

the object Its data is in the form of a set of

attributes, each has a value at any given point in time.

A set of methods can be performed on the data

It is an instance of a class

Page 15: OBJECT ORIENTED CONCEPTS

15

ATTRIBUTES Attributes are data items that

define a class They are features/properties that

help describes the system

Page 16: OBJECT ORIENTED CONCEPTS

16

TASK 2 List the attributes necessary for

the class student

Page 17: OBJECT ORIENTED CONCEPTS

17

CarmakemodeldoorsbodyLengthengineSizecolourspeedstop()turn()accelerate()brake()

BJ1132: carmake=‘Ford’model=‘Falcon’doors=4bodyLength=300engineSize=6colour=‘red’speed=0stop()turn()accelerate()brake()

KN1123: carmake=‘Toyota’model=‘Corolla’doors=5bodyLength=200engineSize=4colour=‘white’speed=100stop()turn()accelerate()brake()

A Ca

r cla

ss

Car o

bjec

ts

Class Notation Object Notation/diagram

CLASS & OBJECTS

Page 18: OBJECT ORIENTED CONCEPTS

18

CLASS & OBJECTS There are 4 different types of

methods/behaviour :1. Constructor methods – executed each time an

instance of the class is created. Often used to initialise attributes.

2. Mutator methods – methods which allow other objects to set or change the value(s) of an object attribute(s). This method begins with the word set e.g. setName.

3. Accessor methods – methods which allow other objects to query the value(s) of a n object attribute(s). This method begins with the word get e.g.getName.

4. Other methods – normally used to perform more complex operations e.g. calculation, validation

Page 19: OBJECT ORIENTED CONCEPTS

19

CLASS & OBJECTS Tips :

To find class/objects/attributes, read the problem definition for nouns and noun phrases. (Not all nouns in the problem statement are vital to the solution)

To identify methods, look at the verbs that describe the actions performed by the nouns

Page 20: OBJECT ORIENTED CONCEPTS

20

Example: A program is required to process the

employee pay. The employee data is read from a file. For each employee, compute the employee’s monthly pay and then print a report containing their detail together with their pay. The input file contains the employee number, pay rate, the number of hours worked and the overtime hours worked in a week. Assuming the overtime rate rate is 1.5 times the standard rate.

Analyzed the above problem.

CLASS & OBJECTS

Page 21: OBJECT ORIENTED CONCEPTS

21

EmployeePay

empNumberpayRatehoursWorkedovertimeHrs

Listing the attributes

CLASS & OBJECTS

Page 22: OBJECT ORIENTED CONCEPTS

22

CLASS & OBJECTS Example: A program is required to process the

employee pay. The employee data is read from a file. For each employee, compute the employee’s monthly pay and then print a report containing their detail together with their pay. The input file contains the employee number, pay rate, the number of hours worked and the overtime hours worked in a week. Assuming the overtime rate rate is 1.5 times the standard rate.

Analyzed the above problem.

Page 23: OBJECT ORIENTED CONCEPTS

23

23

EmployeePay

empNumberpayRatehoursWorkedovertimeHrs

readData()printDetail()calcPay()printPay()

Listing the methods

CLASS & OBJECTS