introduction to object oriented design lecture 11

Post on 22-Dec-2015

222 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to Object Oriented Design

Lecture 11

Procedural Design

• Program design so far: Procedural approach concentrate on what program does, data will be considered when the program need input, output or calculation.

• Procedural design : involves indentifying and organising the processes in the problem solution

• Backward: – Limitations in the development of large systems or

networked and multi-user systems. (even if they are structured and modular)

– If programmer work in a team, the same blocks code are duplicated in different parts of the system.

Object Oriented Design

• We interact with the problem in the same way that we interact with our world we treat it as a set of separate objects that perform actions and relate to each other.

Encapsulation and information hiding

• Object are said encapsulate their data and the process for the data.

• Internal process and data of the object work independently from the system.

• In OOD principle of information hiding to make object as robust and independent as possible.

Objects

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

• An object has properties:– Identity– Attributes– Methods or operations

• Example: car identity: licence plate, attributes: model, number of doors, body lenght, colour, speed, etc. Operation: accelerate, stop, brake, run, etc.

Classes and Objects

• Class: pattern of an object, which define: the basic relations, attributes, operations available to the object of the class.

• The process of creating objects from classes is called: instantiation

• Object is called an instant of its class.

• Example: class: Flower, instant: rose, orchid, jasmine, etc

A car classCar

Factory

Model

Doors

Bodylength

Colour

Speed

Accelerate()

Stop()

Brake()

Turn(direction)

Car objectsRVJ635:Car

Factory = `Toyota`

Model = `Inova`

Doors = 5

Bodylength = 300

Colour = ´Black`

Speed = 80

Accelerate()

Stop()

Brake()

Turn(direction)

SVU478:Car

Factory = `Ford`

Model = `Falcon`

Doors = 4

Bodylength = 200

Colour = ´red`

Speed = 60

Accelerate()

Stop()

Brake()

Turn(direction)

Attributes

• : Properties or characteristics of particular object.

• Object of the same class will have the same attributes.

Constructors

• The process of instantiating an object from the class is performed when constructor is called.

• Constructor assign initial values to the new object‘s attributes

• Constructor usually have the same name as their class

• Pseudocode:Create object-name as new Class-name()

Accessors and mutators

• The value in the attributes hidden from external objects. For safety, only special public operations, known as accessors and mutators can acces it.

• Accessors : pass attribute values to external object keyword: get, example: getName()

• Mutators: enable external object to change the value in attributes keyword: set, example: setPayRate()

Visibility

• Public : operate services required by other object

• Private : operate services required by internal actions in an object and cannot be accessed directly from outside of the object.

Steps in creating an object oriented solution

1. Identify the classes, together with their attributes, responsibilities and operations

2. Determine the relationship between the object of those classes

3. Design the algorithms for the operations, using structured design

4. Develop a test or driver algorithm

Example 11.1 Print student results

• Design a class to manage student results in a subject. A unique student number identifies each student. During the course of the subject, each student completes three assignments (representing 40% of the final mark but each scored out of 100) and an examination(also scored out of 100). The final mark is calculated by multiplying the sum of assignments by 0.133 and the examination by 0.6 and adding the two products together. The class will allow a user to update an assignment mark or an examination mark, and to print the final mark along with the student number for each student.

Step 1: Identify the classes, together with their

attributes, responsibilities and operations • Underline the nouns and nouns phrases to identify the object and

their attribute:

Design a class to manage student results in a subject. A unique student number identifies each student. During the course of the subject, each student completes three assignments (representing 40% of the final mark but each scored out of 100) and an examination(also scored out of 100). The final mark is calculated by multiplying the sum of assignments by 0.133 and the examination by 0.6 and adding the two products together. The class will allow a user to update an assignment mark or an examination mark, and to print the final mark along with the student number for each student.

• We need: student object, with attributes: unique student number, three assignment and an examination. No need to make final mark as attribute because it can be derived from the other attributes.

Underline the verb and verb phrases to identify the responsibilities and the operations that the object needs to perform:

• Design a class to manage student results in a subject. A unique student number identifies each student. During the course of the subject, each student completes three assignments (representing 40% of the final mark but each scored out of 100) and an examination (also scored out of 100). The final mark is calculated by multiplying the sum of assignments by 0.133 and the examination by 0.6 and adding the two products together. The class will allow a user to update an assignment mark or an examination mark, and to print the final mark along with the student number for each student.

There are three public or visible operations: update an assignment mark or update an examination mark, and to print the final mark and one private operation: calculate final mark

• Two mutator will be needed: one to update the assignment, called setAsst(), and another to update the exam mark, called setExam()

• The input mark must be validated before calculation, so another private operation to validate the mark is required.

Class table

Class Attributes Responsibilities Operations

Student studentNumber

asstOne

asstTwo

asstThree

examMark

Update assignment mark

Update exam mark

Print final mark

Calculate final mark

+setAsst(asstNum, result)

+setExam(result)

+validateMark()

+calculateFinalMark()

+printFinalMark()

Step 2: Determine the relationship between the object of those classes

• There is only one object in the solution, so we can move to the next step

Step 3: Design the algorithms for the operations, using structured design

• Public operations– An algorithm is required for each operation in

the object table– The mutator, setAsst() requires two

parameters be passed to it: the assignment number and the result of the assignment

– The result, passed as parameter, will be validated by the private operation, validateMark(), before updating mark to the assignment

Mutator setAsst()

setAsst(asstNum, result)validateMark(result, validInput)IF validInput THEN

CASE OF asstNum1: asstOne = result2: asstTwo = result3: asstThree = result

OTHERWISEReport invalid assignment number error

ENDCASEENDIF

END

Mutator setExam()

setExam(result)

validateMark(result, validInput)

IF validInput THEN

examMark = result

ENDIF

END

printFinalMark()

printFinalMark()

calculateFinalMark(finalMark)

Print studentNumber, finalMark

END

Private Operation

validateMark(result, validInput)

Set validInput to true

IF (result < 0 OR result > 100) THEN

Set validInput to false

Report invalid result error

ENDIF

END

calculateFinalMark(finalMark)

finalMark = (asstOne + asstTwo + asstThree) * 0.133

finalMark = finalMark + (examMark * 0.6)

END

Step 4: Develop a test or driver algorithm

• Rather than develop a mainline algorithm, we will write a simple Test class, calles testStudent, to triall the Student class.

• A constructor for Student class, named Student, also needed to create student object and initialise the studentNumber attribute

Constructor

Student(inStudentNumber)

set studentNumber to inStudentNumber

set asstOne to 0

set asstTwo to 0

set asstThree to 0

set examMark to 0

END

• The value of studentNumber is passed as a parameter to the constructor, which will create a Student object with pseudocode:

Create student as new Student(studentNumber)

Finally, triall the student class:testStudent()

Set studentNumber to 111555Create sudent1 as new Student(studentNumber)set asstNum to 2set result to 80student1.setAsst(asstNum, result)Set studentNumber to 222000Create sudent2 as new Student(studentNumber)set asstNum to 1set result to 95student2.setAsst(asstNum, result)student1.printFinalMark()student2.printFinalMark()

END

EXERCISE

• Write an object oriented design for a program that will prompt for and receive the diameter of a circle, and calculate and display the radius and the area of that circle.– Design the class table– Write an algorithm for each operation– Write an algorithm for a circle Test class.

top related