Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon Page 1 05 – Database Design: Sub-forms.
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Visual BASIC 2005.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Modular Design in ASP.
Mark Dixon Page 1 06 – Expression Builder. Mark Dixon Page 2 Session Aims & Objectives Aims –To use expressions to perform more complex calculations in.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Coursework 2 Debrief: Numbers Game.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Visual BASIC 6.0.
Mark Dixon 1 9 – Case Study. Mark Dixon 2 Session Aims and Objectives Aims –To give an overview of the development of a web-page from initial idea to.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon 1 Soft051 Examination Sample Questions.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
18 – Databases: Structured Query Language
23 – Object Oriented Programming in ASP
17 – Persistent data storage: relational databases and ADO
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations

Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce 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: –create a project with several associated objects

Mark Dixon, SoCCE SOFT 131Page 3 Example: Face Class Option Explicit Public x As Single Public y As Single Public Sub Draw (picDest As PictureBox) picDest.Cls picDest.Circle (x, y), 500 picDest.Circle (x - 150, y - 100), 100 picDest.Circle (x + 150, y - 100), 100 picDest.Line (x - 200, y + 300)-(x + 200, y + 300) picDest.Line (x, y - 50)-(x, y + 150) End Sub Public Sub MoveUp () y = y End Sub Properties Methods Class

Mark Dixon, SoCCE SOFT 131Page 4 Example: Face Form Option Explicit Dim tmpFace As Face Private Sub Form_Load () Set tmpFace = New Face tmpFace.x = 2000 tmpFace.y = 2000 Me.Show tmpFace.Draw Me.picMain End Sub Private Sub btnUp_Click() tmpFace.MoveUp tmpFace.Draw Me.picMain End Sub Private Sub Form_Unload (Cancel As Integer) Set tmpFace = Nothing End Sub Face

Mark Dixon, SoCCE SOFT 131Page 5 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

Mark Dixon, SoCCE SOFT 131Page 6 Example: Bar (Analysis) Scenario 1: small project, limited automation –Nouns: drinks, order, cost –Verbs: describe, calculate cost 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.

Mark Dixon, SoCCE SOFT 131Page 7 Example: Bar (Design v1) Object Classes, properties, and methods –Class diagram: Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double Drink mType: Long mQty: Long

Mark Dixon, SoCCE SOFT 131Page 8 Example: Bar (Implementation) Form Class Bar

Mark Dixon, SoCCE SOFT 131Page 9 Example: Bar (Drink module) Drink (class module): Option Explicit Public mType As Long Public mQty As Long Drink mType: Long mQty: Long

Mark Dixon, SoCCE SOFT 131Page 10 Example: Bar (Order module) Option Explicit Const First = 0 Const Last = 9 Private mDrinks(First To Last) As Drink Private mListBox As ListBox Public Sub Add(tmpType As Long, tmpQty As Long) Dim d As Long ' Find free slot. For d = First To Last If mDrinks(d) Is Nothing Then Exit For End If Next ' Create object and store data. Set mDrinks(d) = New Drink mDrinks(d).mType = tmpType mDrinks(d).mQty = tmpQty End Sub Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double

Mark Dixon, SoCCE SOFT 131Page 11 Example: Bar (Order module) method (procedure) to display order's drinks in a list box Public Sub Display(lstOrder As ListBox) Dim d As Long Dim tmpStr As String lstOrder.Clear For d = First To Last If Not (mDrinks(d) Is Nothing) Then tmpStr = mDrinks(d).mQty & " " tmpStr = tmpStr & mListBox.List(mDrinks(d).mType) lstOrder.AddItem tmpStr lstOrder.ItemData(lstOrder.NewIndex) = d End If Next End Sub Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double

Mark Dixon, SoCCE SOFT 131Page 12 Example: Bar (Order module) method (procedure) to remove drink from order Public Sub Remove(d As Long) Set mDrinks(d) = Nothing End Sub Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double

Mark Dixon, SoCCE SOFT 131Page 13 Example: Bar (Order module) Method to populate Drinks List Public Sub DrinksListInit(tmpList As ListBox) Set mListBox = tmpList mListBox.Clear mListBox.AddItem "Coke", 0 mListBox.ItemData(0) = 115 mListBox.AddItem "Lemonade", 1 mListBox.ItemData(1) = 110 mListBox.AddItem "Beer", 2 mListBox.ItemData(2) = 180 mListBox.AddItem "Cider", 3 … mListBox.AddItem "Whisky", 6 mListBox.ItemData(6) = 95 mListBox.AddItem "Rum", 7 mListBox.ItemData(7) = 105 End Sub

Mark Dixon, SoCCE SOFT 131Page 14 Example: Bar (frmMain module) Option Explicit Private curOrder As Order Private Sub Form_Load () Set curOrder = New Order curOrder.DrinksListInit lstDrinks End Sub Private Sub btnAdd_Click () curOrder.Add lstDrinks.ListIndex, txtQty.Text curOrder.Display lstOrder End Sub Private Sub btnRemove_Click () curOrder.Remove lstOrder.ItemData(lstOrder.ListIndex) curOrder.Display lstOrder End Sub Private Sub btnCost_Click () lblCost.Caption = "£" & curOrder.Cost End Sub

Mark Dixon, SoCCE SOFT 131Page 15 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.

Mark Dixon, SoCCE SOFT 131Page 16 Tutorial Exercise: Music Task 1: Implement a music track program that stores Track title, and duration, and artist name. Use object-oriented techniques.