1 / 23

12 – Object Oriented Analysis, Design, and Programming

12 – Object Oriented Analysis, Design, and Programming. Session Aims & Objectives. Aims To introduce the fundamental ideas of object orientation Objectives, by end of this week’s sessions, you should be able to: create a class definition, which includes properties, and methods

Download Presentation

12 – Object Oriented Analysis, Design, and Programming

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. 12 – Object Oriented Analysis, Design, and Programming

  2. Session Aims & Objectives • Aims • To introduce the fundamental ideas of object orientation • Objectives,by end of this week’s sessions, you should be able to: • create a class definition, which includes • properties, and • methods • create an object instance, for the class • use the object instance, by • assigning values to its properties, and • calling its methods

  3. Example: Ball Bounce v1 <html> <head><title>Ball Bounce</title></head> <body style="margin: 0;"> <img id=imgBall src=Ball.gif style="position: absolute;" /> </body> </html> <script language=vbscript> Option Explicit Dim x Dim y Dim xInc Dim yInc Sub window_onLoad() window.setinterval "Main", 20 xInc = 5 yInc = 3 End Sub Sub Main() x = imgBall.style.pixelLeft + xInc If x <= 0 Or x >= document.body.clientWidth - imgBall.width Then xInc = -xInc Else imgBall.style.pixelLeft = x End If y = imgBall.style.pixelTop + yInc If y <= 0 Or y >= document.body.clientHeight - imgBall.height Then yInc = -yInc Else imgBall.style.pixelTop = y End If End Sub </script>

  4. Structured Paradigm • Program made up of • data structures, and • routines (procedures and functions) that process the data within those structures. • Each routine should perform a single, clearly identifiable operation. • Each routine should be self-contained • Go to statements replaced by structures • Abstract data type = structure + procedures

  5. Example: Ball Bounce v2 Option Explicit Dim x Dim y Dim xInc Dim yInc Sub Init(tmpXInc, tmpYInc) xInc = tmpXInc yInc = tmpYInc End Sub Sub Move(img) x = img.style.pixelLeft + xInc If x <= 0 Or x >= document.body.clientWidth - img.width Then xInc = -xInc Else img.style.pixelLeft = x End If y = img.style.pixelTop + yInc If y <= 0 Or y >= document.body.clientHeight - img.height Then yInc = -yInc Else img.style.pixelTop = y End If End Sub Sprit.vbs BallBounce.htm <html> <head><title>Ball Bounce</title></head> <body style="margin: 0;"> <img id=imgBall src=Ball.gif style="position: absolute;" /> </body> </html> <script language=vbscript src=Sprite.vbs></script> <script language=vbscript> Option Explicit Sub window_onLoad() window.setinterval "Main", 20 Init 5, 3 End Sub Sub Main() Move imgBall End Sub </script>

  6. Object-Oriented Paradigm • A program is made up of a number of objects that communicate with each other by passing messages • Each object contains • attributes/properties that represent its state, and • operations/methods that represent its behaviour • Objects often mirror the real world • Customers • Students • Patients

  7. Classes and Instances • Object Classes • general descriptions of types of objects,e.g. student, product, customer, lecturer, and room. • Object Instances • specific items of a given class, e.g. • each of you could be an instance of the student class • Room 214 could be an instance of the room class • I could be an instance of the lecturer class • Bolt could be an instance of the part class

  8. Object Concepts - Implementation • Properties – implemented as • data structures (variables, and arrays) • Methods – implemented as either • a procedure (to perform some processing), or • a function (to return a value) • Object oriented paradigm builds on (rather than replaces) the structured paradigm

  9. Properties: Animals • Class: Animal • Properties: Name, Species, Gender • Instances: myPet, yourPet

  10. Example: Ball Bounce v3 Option Explicit Class Sprite Dim x Dim y Dim xInc Dim yInc End Class Sub Init(spr, tmpXInc, tmpYInc) spr.xInc = tmpXInc spr.yInc = tmpYInc End Sub Sub Move(spr, img) spr.x = img.style.pixelLeft + spr.xInc If spr.x <= 0 Or spr.x >= document.body.clientWidth - img.width Then spr.xInc = -spr.xInc Else img.style.pixelLeft = spr.x End If spr.y = img.style.pixelTop + spr.yInc If spr.y <= 0 Or spr.y >= document.body.clientHeight - img.height Then spr.yInc = -spr.yInc Else img.style.pixelTop = spr.y End If End Sub Sprit.vbs Class Definition BallBounce.htm <html> … </html> <script language=vbscript src=Sprite.vbs></script> <script language=vbscript> Option Explicit Dim ball Sub window_onLoad() window.setinterval "Main", 20 Set ball = New Sprite Init ball, 5, 3 End Sub Sub Main() Move ball, imgBall End Sub </script>

  11. Methods: Students • Method: Clear

  12. Example: Ball Bounce v4 Option Explicit Class Sprite Dim x Dim y Dim xInc Dim yInc Sub Init(tmpXInc, tmpYInc) xInc = tmpXInc yInc = tmpYInc End Sub Sub Move(img) x = img.style.pixelLeft + xInc If x <= 0 Or x >= document.body.clientWidth - img.width Then xInc = -xInc Else img.style.pixelLeft = x End If y = img.style.pixelTop + yInc If y <= 0 Or y >= document.body.clientHeight - img.height Then yInc = -yInc Else img.style.pixelTop = y End If End Sub End Class Sprit.vbs BallBounce.htm <html> … </html> <script language=vbscript src=Sprite.vbs></script> <script language=vbscript> Option Explicit Dim ball Sub window_onLoad() window.setinterval "Main", 20 Set ball = New Sprite ball.Init 5, 3 End Sub Sub Main() ball.Move imgBall End Sub </script>

  13. Example: Ball Bounce v5 • Multiple instances:

  14. Example: Ball Bounce v5 <html> <head><title>Ball Bounce</title></head> <body style="margin: 0;"> <img id=imgBall src=Ball.gif style="position: absolute;" /> <img id=imgFace src=BallChar2.GIF style="position: absolute;" /> </body> </html> <script language=vbscript src=Sprite.vbs></script> <script language=vbscript> Option Explicit Dim ball Dim face Sub window_onLoad() window.setinterval "Main", 20 Set ball = New Sprite ball.Init 5, 3 Set face = New Sprite face.Init 1, 12 End Sub Sub Main() ball.Move imgBall face.Move imgFace End Sub </script> • Now have easy way of: • creating & using multiple sprites • each with own identity (separate characteristics) • only a few (4) lines of code

  15. Questions: OOP Class House Dim number Dim road Dim district Sub ChangeDist(newDist) district = newDist End Sub End Class Dim h Set h = New House • Name a • class • property • method • instance

  16. Benefits of OOP in code • Procedures and Functions are part of object • encapsulation • RelatedData and Operationstogether • Private keyword – restrict access to data • Clearer code • Reduces chance of accidental interference • Less prone to error

  17. Object Oriented Analysis • Look for nouns in text, either • object classes, or • object properties • Look for verbs in text, • object methods The students' Union bar needs a computer system for recording the purchase of drinks. Typically, a student will stagger to the bar and describe their order, consisting of one or (usually) more drinks. The bar staff will then prepare the drinks and calculate the cost of the order.

  18. Identify all nouns and verbs The students' Union bar needs a computer system for recording the purchase of drinks. Typically, a student will stagger to the bar and describe their order, consisting of one or (usually) more drinks. The bar staff will then prepare the drinks and calculate the cost of the order. • Nouns: student's Union bar, computer system, drinks, student, bar, order, bar staff, cost. • Verbs: recording the purchase, stagger, describe, prepare drinks, calculate cost

  19. Identify relevant nouns and verbs • What is relevant? • depends on project scope, duration, budget • Scenario 1: small project, limited automation • Nouns: drinks, order, cost • Verbs: describe, calculate cost • Scenario 2: large project, high automation • Nouns: student's Union bar, drinks, student, bar, order, bar staff, cost. • Verbs: recording the purchase, describe, prepare drinks, calculate cost

  20. Scenario 1: detail • Nouns: drinks, order, cost • Verbs: describe, calculate cost

  21. Tutorial Exercise: Ball Bounce • Learning Objective: To create and use your own class. • Task 1: Get the Ball Bounce examples (1, 2, and 5) from the lecture working. • Task 2: Add a hit method to the sprite class, which detects the collision with another sprite. • Task 3: Modify your page to count the number of hits between the two sprites. • Task 4: Modify your page to make sprites bounce off each other. • Task 5: Add another sprite.

  22. Tutorial Exercise: Bar • Task 1: Continue the analysis of the bar example in the lecture, and implement a simple object oriented bar drinks calculation program.

  23. Tutorial Exercise: Interceptor • Learning Objective: To create and use your own classes. • Task 1: Re-write your Interceptor example (from last week) to use object oriented concepts (classes, properties, methods, and instances)

More Related