Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

8 – Objects as building blocks Visual Basic: An Object Oriented Approach.
CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally.
CSM18 Visual Basic Section 2 Department of Computing UniS 1 CSM18 Further Constructs in VB A statement is the fundamental syntactical element of a program.
Object Oriented Programming. Object Oriented Data and operations are grouped together Account Withdraw Deposit Transfer Interface: Set of available operations.
Intermediate Code Generation
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Visual Basic: An Object Oriented Approach 2 – Designing Software Systems.
Lecture 8 Electronic Commerce Modelling Techniques
Visual Basic: An Object Oriented Approach 6: Object Modelling.
Sequence Diagrams. Introduction A Sequence diagram depicts the sequence of actions that occur in a system. The invocation of methods in each object, and.
ATM User Interface Design. Requirements A bank customer is able to access his or her account using an automatic teller machine. To be able to use an ATM.
Interaction Diagrams Activity Diagram State Machine Diagram
8 – Objects as building blocks Visual Basic: An Object Oriented Approach.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Visual Basic: An Object Oriented Approach 3 – Making Objects Work.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Visual Basic: An Object Oriented Approach 4: Simple Programming in VB.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
{ How to Use An ATM A simple tutorial to teach how to use ATM Machines.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Writing Classes (Chapter 4)
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
1 Object-Oriented Analysis Use Case Driven. 2 The outline method for OOA 1.Identify object classes within the problem domain 2.Define the behaviour of.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Data structures Abstract data types Java classes for Data structures and ADTs.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Information Systems Engineering Interaction Diagrams: Sequence Diagram Collbortion Diagram.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Data Structures and Algorithms Dr. Tehseen Zia Assistant Professor Dept. Computer Science and IT University of Sargodha Lecture 1.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Data Structure and Algorithms
Lecture Outline Monday 23 rd February (Week 4) 3 – 3:10pm Review of Requirements Eng. and use cases 3:10 – 3:40pm Exercise on Use Case 3:40-4:40 Class.
Stacks Chapter 3 Objectives Upon completion you will be able to
Inf 43: Introduction to Software Engineering May 7, 2016.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Sorts, CompareTo Method and Strings
Slides by Donald W. Smith
COMP 53 – Week Fourteen Trees.
Copy Constructor / Destructors Stacks and Queues
Data Structure and Algorithms
Data Structures Interview / VIVA Questions and Answers
draw a sequence diagram
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lesson Objectives Aims
CS285 Introduction - Visual Basic
Introduction to Data Structures
Pointers & Dynamic Data Structures
Please use speaker notes for additional information!
Queues Jyh-Shing Roger Jang (張智星)
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Introduction to Data Structure
Appendix D: Network Model
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled as an object in the program Hence Object Oriented Programming

Dr Terry Hinton 28/10/03 CSM18 VB OOP2 Object Oriented Programming Classes – templates for objects The “cookie cutter” idea Class Definition Objects

Dr Terry Hinton 28/10/03 CSM18 VB OOP3 Object Oriented Programming Classes – templates for objects Encapsulation – of Data and Code Attributes of an object - variables Methods – operations defined for a class of objects Messages – interactions between objects via Methods to send or receive data

Dr Terry Hinton 28/10/03 CSM18 VB OOP4 Object Models Real programs may involve large numbers of objects Will interact in various ways Will form logical structures Key is in building an object model that provides a ‘world view’ of the system One to one correspondence between object in the real world and object in the model

Dr Terry Hinton 28/10/03 CSM18 VB OOP5 Use-Case Diagrams Used to obtain a high level (user-level) picture of the system Can be used in discussions with clients Shows main interactions between system and users/external environment ATM - Bank Machine Accept Card Verify PIN Print Statement Withdraw Cash Deposit Cash User (Actor)

Dr Terry Hinton 28/10/03 CSM18 VB OOP6 Class Model Diagrams Class Name Attributes- Variables define state of object Methods- Program code defines processes or behaviour of object methods interact with methods of other objects

Dr Terry Hinton 28/10/03 CSM18 VB OOP7 Class Model based on Use-Case Diagram Having identified classes, need to define… Interactions between them Structure Class Interfaces - public view of object ATMObj ValidateUser PrintStatement MakeDeposit MakeWithdrawal AccountObj Balance PIN Owner Deposit Withdraw Statement ATMAccount Account.Deposit(50)

Dr Terry Hinton 28/10/03 CSM18 VB OOP8 Coding a Class Need to… Determine storage needs - attributes or variables Determine Methods and interaction between them Consider mechanisms for getting data into and out of an object Define Methods & Functions to do this Data - Private Methods & Functions - Public

Dr Terry Hinton 28/10/03 CSM18 VB OOP9 Example – Bank/ATM Assume two classes only ATM Class models Automatic Teller Machine Account class models individual account in Bank Start with Account class ATM accesses Account Account receives deposit, agree a withdrawal, present state of balance, agree PIN number

Dr Terry Hinton 28/10/03 CSM18 VB OOP10 Account Class Private Balance As Currency ‘ very simple example Private PIN As Integer Private Owner As String Public Function GetBalance() As Currency… Public Function GetPIN() As Integer… Public Sub AssignPIN(newValue As Integer)… Public Function GetOwner() As String… Public Sub AssignOwner(newValue As String)… Public Sub Deposit(amount As Currency)… Public Sub Withdraw(amount As Currency)… Public Function Statement() As String… ‘returns text about account Private Balance As Currency ‘ very simple example Private PIN As Integer Private Owner As String Public Function GetBalance() As Currency… Public Function GetPIN() As Integer… Public Sub AssignPIN(newValue As Integer)… Public Function GetOwner() As String… Public Sub AssignOwner(newValue As String)… Public Sub Deposit(amount As Currency)… Public Sub Withdraw(amount As Currency)… Public Function Statement() As String… ‘returns text about account Methods are Subroutines & Functions

Dr Terry Hinton 28/10/03 CSM18 VB OOP11 Account – Methods Public Function GetBalance() As Currency GetBalance = Balance End Function Public Funtion GetPIN() As Integer GetPin = PIN End Function Public Sub AssignPIN(newValue As Integer) PIN = newValue End Sub Public Sub Deposit(amount As Currency) Balance = Balance + amount End Sub Public Function GetBalance() As Currency GetBalance = Balance End Function Public Funtion GetPIN() As Integer GetPin = PIN End Function Public Sub AssignPIN(newValue As Integer) PIN = newValue End Sub Public Sub Deposit(amount As Currency) Balance = Balance + amount End Sub Read-only Read-only property Read Write Method definition

Dr Terry Hinton 28/10/03 CSM18 VB OOP12 Class - Object - Instances Define a Class of Objects but need an actual example of an object Instance of a Class - Object Instance Each Object has the same set of attributes Each Object has its own values of these attributes Values of Attributes set the state of the Object Only one copy of an Object Instance but have any number of Place Holders for a reference or pointer to an Object - more later

Dr Terry Hinton 28/10/03 CSM18 VB OOP13 Testing Account Set Account = New AccountObj Account.AssignOwner ( “John Smith”) Account.AssignPIN (1234) Account.Deposit (500.00) Account.Statement Statement for: John Smith Balance = £ Account.Withdraw (50) Account.Statement Statement for: John Smith Balance = £ ‘ etc… Set Account = New AccountObj Account.AssignOwner ( “John Smith”) Account.AssignPIN (1234) Account.Deposit (500.00) Account.Statement Statement for: John Smith Balance = £ Account.Withdraw (50) Account.Statement Statement for: John Smith Balance = £ ‘ etc… In VB can use the Immediate Window to test a class Create an object Execute methods Print attribute values (Using Print) Can also copy, the sequence of test statements, paste into Notepad or an editor, and save for re-testing if changes are made.

Dr Terry Hinton 28/10/03 CSM18 VB OOP14 Continue Development Create next class ATM User-Interactions with ATM should translate into ATM Methods and interactions with Account Methods

Dr Terry Hinton 28/10/03 CSM18 VB OOP15 Example ATM Method Public Function ValidateUser(Account As AccountObj) As Boolean Dim userPIN As Integer userPIN = InputBox(“Enter PIN”) If TestAccount.PIN = userPIN Then ValidateUser = True Else ValidateUser = False End If End Function Public Function ValidateUser(Account As AccountObj) As Boolean Dim userPIN As Integer userPIN = InputBox(“Enter PIN”) If TestAccount.PIN = userPIN Then ValidateUser = True Else ValidateUser = False End If End Function This method takes as its parameter an Object of type AccountObj. Returns a Boolean.

Dr Terry Hinton 28/10/03 CSM18 VB OOP16 Testing an ATM Method Set Account = New AccountObj Account.AssignOwner (“John Smith”) Account.AssignPIN = 1234 Set ATM = New ATMObj Print ATM.ValidateUser (Account) ‘ VB creates an InputBox() here to enter PIN number ‘ ATM returns True or False depending on input to it. ‘ Could now have ATM making deposits and ‘ withdrawals etc. e.g… ATM.MakeDeposit (100.00) ATM.PrintStatement (Account) Statement for: John Smith Balance = £ Set Account = New AccountObj Account.AssignOwner (“John Smith”) Account.AssignPIN = 1234 Set ATM = New ATMObj Print ATM.ValidateUser (Account) ‘ VB creates an InputBox() here to enter PIN number ‘ ATM returns True or False depending on input to it. ‘ Could now have ATM making deposits and ‘ withdrawals etc. e.g… ATM.MakeDeposit (100.00) ATM.PrintStatement (Account) Statement for: John Smith Balance = £100.00

Dr Terry Hinton 28/10/03 CSM18 VB OOP17 References A Reference is a variable that acts as an alias for an object or pointer to an object References are used for interacting with objects avoids copying objects An object with no references to it is destroyed automatically - orphan When an object instance is created it is given a name and allocated space for its attributes Reference to that object can be made without copying the Object.

Dr Terry Hinton 28/10/03 CSM18 VB OOP18 References and Objects Dim Account As AccountObj, TempAccount As AccountObj Set Account = New AccountObj Set TempAccount = Account Dim Account As AccountObj, TempAccount As AccountObj Set Account = New AccountObj Set TempAccount = Account Copies reference for Account to TempAccount Object: Account Creates two references of type AccountObj Creates object instance and sets aside space for variables Only one instance of Account

Dr Terry Hinton 28/10/03 CSM18 VB OOP19 Collections When we create a set of object instances they all have the same name!! To avoid losing track of Objects we need a Collection which is a list of object references Therefore no need to give each object instance a different name Acts as an unbounded array – no need to indicate how many elements to accommodate

Dr Terry Hinton 28/10/03 CSM18 VB OOP20 Collection Methods Four main methods Add – adds an object reference to the collection Remove – removes a reference Count – returns number of references in the collection Item – allows access to individual object references Indexing Objects can be retrieved by number Objects can be retrieved by value of an attribute Objects can be added with a Key (string), which acts as a textual index

Dr Terry Hinton 28/10/03 CSM18 VB OOP21 A Collection Set Queue = New CollectionObj Set Account = New AccountObj Account.AssignOwner (“Fred”) Queue.Add (Account) Set Account = New AccountObj Account.AssignOwner (“Mary”) Queue.Add (Account) Print Queue.Count 2 Print Queue(1).Owner Fred Print Queue(2).Owner Mary Set Queue = New CollectionObj Set Account = New AccountObj Account.AssignOwner (“Fred”) Queue.Add (Account) Set Account = New AccountObj Account.AssignOwner (“Mary”) Queue.Add (Account) Print Queue.Count 2 Print Queue(1).Owner Fred Print Queue(2).Owner Mary

Dr Terry Hinton 28/10/03 CSM18 VB OOP22 Collections and For Each… Collections and objects introduce a new style of For..Next loop For Each Account In Queue Print Account.Owner Next For Each Account In Queue Print Account.Owner Next Fred Mary Fred Mary

Dr Terry Hinton 28/10/03 CSM18 VB OOP23 Building Structures with Objects Class CarObj Private ID As Integer, mileage As Single Private Passenger As PassengerObj Public Sub AcceptPass(Pass As PassengerObj) Public Sub Journey CurrentName = Car.GetPass.GetName

Dr Terry Hinton 28/10/03 CSM18 VB OOP24 Structures in programs Ways of grouping objects using Arrays, Collections Lists Simple lists of objects Stacks (Last-in, First out) Queues (Last-in, Last-out) Trees Hierarchical structures of objects - node contains reference to next node Graphs Arbitrarily interconnected objects

Dr Terry Hinton 28/10/03 CSM18 VB OOP25 Logical and Physical Structure Interconnect objects using references Forms of interconnection involve Logical structure The model of interconnections we wish to create Physical structure code The actual mechanisms we use to create it

Dr Terry Hinton 28/10/03 CSM18 VB OOP26 Alternative Collections Ordered Collection Items added in a specific order Can use binary search to insert and retrieve items Efficiency in searching (and therefore retrieval

Dr Terry Hinton 28/10/03 CSM18 VB OOP27 Queues and Stacks Collections with limitations on insertions/retrievals Item 5Item 4Item 3Item 2Item 1 Front of Queue (items leave here) Rear of Queue (items join here)

Dr Terry Hinton 28/10/03 CSM18 VB OOP28 Alternatives to Collections Tree Structures Each item can contain references to several other items Efficient for storage and retrieval Recursive Me My FatherMy Mother My father’s father My father’s Mother My mother’s mother father

Dr Terry Hinton 28/10/03 CSM18 VB OOP29 Graphs Arbitrary interconnections Complex, and can be inefficient to code Require use of specialist algorithms to search and traverse Node A B E F D C