1 / 30

Introduction to Object Oriented Design

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.

hinto
Download Presentation

Introduction to Object Oriented Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Object Oriented Design Lecture 11

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

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

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

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

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

  7. A car class

  8. Car objects

  9. Attributes • : Properties or characteristics of particular object. • Object of the same class will have the same attributes.

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

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

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

  13. Steps in creating an object oriented solution • Identify the classes, together with their attributes, responsibilities and operations • Determine the relationship between the object of those classes • Design the algorithms for the operations, using structured design • Develop a test or driver algorithm

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

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

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

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

  18. Class table

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

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

  21. Mutator setAsst() setAsst(asstNum, result) validateMark(result, validInput) IF validInput THEN CASE OF asstNum 1: asstOne = result 2: asstTwo = result 3: asstThree = result OTHERWISE Report invalid assignment number error ENDCASE ENDIF END

  22. Mutator setExam() setExam(result) validateMark(result, validInput) IF validInput THEN examMark = result ENDIF END

  23. printFinalMark() printFinalMark() calculateFinalMark(finalMark) Print studentNumber, finalMark END

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

  25. calculateFinalMark(finalMark) finalMark = (asstOne + asstTwo + asstThree) * 0.133 finalMark = finalMark + (examMark * 0.6) END

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

  27. Constructor Student(inStudentNumber) set studentNumber to inStudentNumber set asstOne to 0 set asstTwo to 0 set asstThree to 0 set examMark to 0 END

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

  29. Finally, triall the student class: testStudent() Set studentNumber to 111555 Create sudent1 as new Student(studentNumber) set asstNum to 2 set result to 80 student1.setAsst(asstNum, result) Set studentNumber to 222000 Create sudent2 as new Student(studentNumber) set asstNum to 1 set result to 95 student2.setAsst(asstNum, result) student1.printFinalMark() student2.printFinalMark() END

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

More Related