1 / 23

24 – Datatypes and Object Association

24 – Datatypes and Object Association. Session Aims & Objectives. Aims To introduce the idea of datatypes some of the more subtle aspects of object oriented design (such as object associations) Objectives, by end of this week’s sessions, you should be able to:

reed-gamble
Download Presentation

24 – Datatypes and Object Association

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. 24 – Datatypes and Object Association

  2. Session Aims & Objectives • Aims • To introduce • the idea of datatypes • some of the more subtle aspects of object oriented design (such as object associations) • Objectives,by end of this week’s sessions, you should be able to: • use datatypes to make you programs more memory efficient and faster • create a project with several associated objects

  3. Data Types • Variant – all types of data • slow, memory hungry • Boolean – true or false • Integer – whole numbers (-32768 to 32768) • Long – whole numbers (large) • Single – decimal numbers • Double – decimal numbers (more precise) • String – text • Object – object instances

  4. Data Type Selection • Number of e.g. 4 Integer/LongRooms • Height e.g. 1.87m Single/Double • Surname e.g. Smith String • Car Reg e.g. XY55 ABC String

  5. Using data types • Variable declaration Dim x As Long • Parameters Sub Thing(boo As String, y As Long) • Functions Function IsTall() As Boolean

  6. client-side vs. server-side code • client-side code – only variant • server-side code – all datatypes <script language="vbscript"> Dim x Dim y Dim s Dim b x = 23 y = 18.5 s = "Hello there" b = false </script> <script language="vbscript" runat="server"> Dim x As Long Dim y As Double Dim s As String Dim b As Boolean x = 23 y = 18.5 s = "Hello there" b = false </script>

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

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

  9. Object Concepts - Implementation • Properties – implemented as • data structures (variables, arrays, and types). • 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

  10. Class Diagrams • Used to describe structure of object classes: Class Name Module Code: string Title: string Class Attributes/Properties GetTitle(): string SetTitle(t: string) Count(): integer Class Operations/Methods

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

  12. Implementing Class Diagrams Module Code: String Title: String GetTitle(): string SetTitle(t: string) Count(): integer Class Module Public Code As String Public Title As String Public Function GetTitle() As string Public Sub SetTitle(t As String) Public Function Count() As Integer End Class

  13. Public and Private • Control access to properties and methodsClass a Public x As Single Private y As SingleEnd ClassDim b As New a b.x = 5 b.y = 10  this works (x is public) this will fail (y is private)

  14. Example: Counter (html) <html> <head><title>Counter page</title></head> <body> <%Main()%> <form action="Main.aspx" method="post"> <input name="btnReset" type="submit" value="Reset" /> <input name="btnUp" type="submit" value="Up" /> <input name="btnDown" type="submit" value="Down" /> </form> <p><%=msg%></p> </body> </html>

  15. Example: Counter (code) <script language="vbscript" runat="server" src="Counter.vbs"></script> <script language="vbscript" runat="server"> Dim c As Object Dim msg As String Sub Main() If Session("c") Is Nothing Then Session("c") = New Counter Else c = Session("c") If Request.Form("btnReset") <> "" Then c.Reset() ElseIf Request.Form("btnUp") <> "" Then c.Up() ElseIf Request.Form("btnDown") <> "" Then c.Down() End If msg = c.GetCount() End If End Sub </script> Counter.vbs Class Counter Private mCount As Long Public Function GetCount() As Long GetCount = mCount End Function Public Sub Reset() mCount = 0 End Sub Public Sub Up() mCount = mCount + 1 End Sub Public Sub Down() mCount = mCount - 1 End Sub End Class

  16. Object Associations • In practice projects will be made of • many object classes • that interact with each other (are associated) • There are several types of association • One of the most often used is the ‘part of’ association, • where one object class forms part of another object class • A common example of this occurs where it is necessary to store multiple instances of a class

  17. Example: Bar (Analysis) 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. • Scenario 1: small project, limited automation • Nouns: drinks, order, cost • Verbs: describe, calculate cost

  18. Example: Bar (Design v1) Order Drink mDrinks(): Drink mType: String mQty: Long Add(string, long) Remove(long) Display (): String Cost(): double • Object Classes, properties, and methods • Class diagram:

  19. Example: Bar (Drink module) Drink mType: String mQty: Long • Drink (class module): Class Drink Public mType As String Public mQty As Long End Class

  20. Example: Bar (Order module) Order mDrinks(): Drink Add(string, long) Remove(long) Display (): String Cost(): double Class Order Private mDrinks(9) As Drink Public Sub Add(tmpType As String, tmpQty As Long) Dim d As Long ' Find free slot. For d = 0 To 9 If mDrinks(d) Is Nothing Then Exit For End If Next ' Create object and store data. mDrinks(d) = New Drink mDrinks(d).mType = tmpType mDrinks(d).mQty = tmpQty End Sub End Class

  21. Example: Bar (Order module) Order mDrinks(): Drink Add(string, long) Remove(long) Display (): String Cost(): double • method (procedure) to display order's drinks in a list box Public Function Display() As String Dim d As Long Dim tmpStr As String tmpStr = "" For d = 0 To 9 If Not (mDrinks(d) Is Nothing) Then tmpStr = tmpStr & mDrinks(d).mQty & " " tmpStr = tmpStr & mDrinks(d).mType End If Next Display = tmpStr End Function

  22. Example: Bar (Order module) Order mDrinks(): Drink Add(string, long) Remove(long) Display (ListBox) Cost(): double • method (procedure) to remove drink from order Public Sub Remove(d As Long) mDrinks(d) = Nothing End Sub

  23. Tutorial Exercise: Bar • Task 1: Get the Bar example from the lecture working. • Task 2: Modify your code – add code to calculate the cost of the order. This object method is not in the lecture notes – you need to create it (not necessarily on your own – discuss it with others, feel free to ask me for help). • Task 3: What happens if the user tries to add more than 10 drinks? Modify your code to cope with this (you decide how it should respond). • Task 4: What happens if the user tries to add a drink when none is selected (in the drinks list box)? Modify your code to cope with this. • Task 5: What happens if the user tries to remove a drink when none is selected (in the order list box)? Modify your code to cope with this. • Task 6: Modify your code – so that the cost is continuously calculated and there is no need for the cost button.

More Related