30
Introduction to Object Oriented Design Lecture 11

Introduction to Object Oriented Design Lecture 11

  • View
    220

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Introduction to Object Oriented Design Lecture 11

Introduction to Object Oriented Design

Lecture 11

Page 2: 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.

Page 3: Introduction to Object Oriented Design Lecture 11

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.

Page 4: Introduction to Object Oriented Design Lecture 11

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.

Page 5: Introduction to Object Oriented Design Lecture 11

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.

Page 6: Introduction to Object Oriented Design Lecture 11

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

Page 7: Introduction to Object Oriented Design Lecture 11

A car classCar

Factory

Model

Doors

Bodylength

Colour

Speed

Accelerate()

Stop()

Brake()

Turn(direction)

Page 8: Introduction to Object Oriented Design Lecture 11

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)

Page 9: Introduction to Object Oriented Design Lecture 11

Attributes

• : Properties or characteristics of particular object.

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

Page 10: Introduction to Object Oriented Design Lecture 11

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

Page 11: Introduction to Object Oriented Design Lecture 11

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

Page 12: Introduction to Object Oriented Design Lecture 11

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.

Page 13: Introduction to Object Oriented Design Lecture 11

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

Page 14: Introduction to Object Oriented Design Lecture 11

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.

Page 15: Introduction to Object Oriented Design Lecture 11

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.

Page 16: Introduction to Object Oriented Design Lecture 11

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

Page 17: Introduction to Object Oriented Design Lecture 11

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

Page 18: Introduction to Object Oriented Design Lecture 11

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

Page 19: Introduction to Object Oriented Design Lecture 11

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

Page 20: Introduction to Object Oriented Design Lecture 11

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

Page 21: Introduction to Object Oriented Design Lecture 11

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

Page 22: Introduction to Object Oriented Design Lecture 11

Mutator setExam()

setExam(result)

validateMark(result, validInput)

IF validInput THEN

examMark = result

ENDIF

END

Page 23: Introduction to Object Oriented Design Lecture 11

printFinalMark()

printFinalMark()

calculateFinalMark(finalMark)

Print studentNumber, finalMark

END

Page 24: Introduction to Object Oriented Design Lecture 11

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

Page 25: Introduction to Object Oriented Design Lecture 11

calculateFinalMark(finalMark)

finalMark = (asstOne + asstTwo + asstThree) * 0.133

finalMark = finalMark + (examMark * 0.6)

END

Page 26: Introduction to Object Oriented Design Lecture 11

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

Page 27: Introduction to Object Oriented Design Lecture 11

Constructor

Student(inStudentNumber)

set studentNumber to inStudentNumber

set asstOne to 0

set asstTwo to 0

set asstThree to 0

set examMark to 0

END

Page 28: Introduction to Object Oriented Design Lecture 11

• 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)

Page 29: Introduction to Object Oriented Design Lecture 11

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

Page 30: Introduction to Object Oriented Design Lecture 11

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.