object-oriented application development using vb.net 1 creating a string array code to create a...

41
Object-Oriented Application Development Using VB .NET 1 Creating a String Array Code to create a String array: ' declare a String array with 4 elements Dim stringArray(3) As String Elements of stringArray are reference variables of data type String

Post on 20-Dec-2015

226 views

Category:

Documents


3 download

TRANSCRIPT

Object-Oriented Application Development Using VB .NET

1

Creating a String Array

• Code to create a String array:

' declare a String array with 4 elements

Dim stringArray(3) As String

• Elements of stringArray are reference variables of data type String

Object-Oriented Application Development Using VB .NET

2

Creating a String Array

• Code to create four String instances and populate the array elements

stringArray(0) = "Hello"

stringArray(1) = "World"

stringArray(2) = "Wide"

stringArray(3) = "Web"

• A specific element can be accessed using the index of that element

• String methods can be invoked for String instances referenced by array elements

Object-Oriented Application Development Using VB .NET

3

Creating a String Array

Object-Oriented Application Development Using VB .NET

4

Using the ArrayList Class

• Major limitation of arrays: fixed size• ArrayList class

– Member of System.Collections namespace

– Creates dynamically resizable array – the number of elements can change during runtime

Object-Oriented Application Development Using VB .NET

5

Using the ArrayList Class

• Code to create an instance of ArrayList class:

' create an ArrayList instance with 3 elements

Dim anArrayList As ArrayList = New ArrayList(3)

Object-Oriented Application Development Using VB .NET

6

Using the ArrayList Class

• Code to create four instances of String class:

' create String instances

Dim s1 As String = New String("Hello")

Dim s2 As String = New String("World")

Dim s3 As String = New String("Wide")

Dim s4 As String = New String("Web")

Object-Oriented Application Development Using VB .NET

7

Using the ArrayList Class

• Add method is used to populate the elements• When a fourth element is added, the number of

elements increases automatically

anArrayList.Add(s1)

anArrayList.Add(s2)

anArrayList.Add(s3)

anArrayList.Add(s4)

Object-Oriented Application Development Using VB .NET

8

Identifying Association Relationships on Bradshaw Marina’s Class Diagram

• Association relationships– Show how instances of classes are associated, or

connected, to each other

– Indicate that the system requires information about these associations

– Can be used to navigate from instance to instance following the association

Object-Oriented Application Development Using VB .NET

9

Identifying Association Relationships on Bradshaw Marina’s Class Diagram

• In a class diagram– An association relationship

• Appears as a line connecting classes

– Multiplicity of the association• Indicated by numbers written at both ends of the line

Object-Oriented Application Development Using VB .NET

10

Identifying Association Relationships on Bradshaw Marina’s Class Diagram

• Association relationships in the Bradshaw class diagram:– A customer owns a boat– A boat is owned by a customer– A boat is assigned to a slip– A slip contains a boat– A dock contains many slips– A slip is attached to a dock– A slip is leased to a customer (Lease is an association

class)– A customer leases a slip (Lease is an association class)

Object-Oriented Application Development Using VB .NET

11

Identifying Association Relationships on Bradshaw Marina’s Class Diagram

Object-Oriented Application Development Using VB .NET

12

Identifying Association Relationships on Bradshaw Marina’s Class Diagram

• Lease class– An example of an association class

• Exists because of the relationship between a slip and a customer

– In the class diagram• Shown as a class connected by a dashed line to an

association between Slip and Customer

Object-Oriented Application Development Using VB .NET

13

Associating VB .NET Classes in a One-to-One Relationship

• Association relationship between Customer and Boat– Has two “directions”

• A customer owns a boat• A boat is owned by a customer

– Each direction of the relationship• Must be defined separately by the system developer• Must be handled separately with VB .NET

Object-Oriented Application Development Using VB .NET

14

Associating VB .NET Classes in a One-to-One Relationship

• To implement an association relationship in VB .NET– Use a reference variable as an attribute of a class

• A reference variable points to an actual instance

Object-Oriented Application Development Using VB .NET

15

Associating VB .NET Classes in a One-to-One Relationship

Object-Oriented Application Development Using VB .NET

16

Associating VB .NET Classes in a One-to-One Relationship

• A Customer reference variable– Points to a Customer instance

– If added as an attribute in the Boat class, each Boat instance can• Point to a Customer instance• Invoke the customer’s methods

Object-Oriented Application Development Using VB .NET

17

Associating VB .NET Classes in a One-to-One Relationship

• A Boat reference variable– Points to a Boat instance

– If added as an attribute in the Customer class, each Customer instance can• Point to a Boat instance• Invoke the boat’s methods

Object-Oriented Application Development Using VB .NET

18

Modifying the Customer Class

• Modifying the Customer class implements one direction of the association relationship– From Customer to Boat

• theBoat attribute– A Boat reference variable

– Added to the Customer class

– Used to implement a one-to-one association relationship with the Boat class

Object-Oriented Application Development Using VB .NET

19

Modifying the Customer Class

• Accessor methods– SetBoat

• Accepts a Boat reference as a parameter• Assigns the reference to the attribute

– GetBoat• Returns the Boat reference

Object-Oriented Application Development Using VB .NET

20

Modifying the Customer Class

• Constructor– Sets the Boat reference variable to Nothing using

the SetBoat method

– Once Customer instance is created• Boat reference variable can be set to an actual Boat

reference

Object-Oriented Application Development Using VB .NET

21

Modifying the Boat Class

• Modifying the Boat class implements the other direction of the association relationship– A boat is owned by a customer

• theCustomer attribute– A Customer reference variable

– Added to the Boat class

– Used to implement a one-to-one association relationship with the Customer class

Object-Oriented Application Development Using VB .NET

22

Modifying the Boat Class

• Accessor methods– GetCustomer

• Returns the Customer reference

– SetCustomer• Accepts a Customer reference as a parameter• Assigns the reference to the attribute

Object-Oriented Application Development Using VB .NET

23

Adding Functionality to the Boat Class

• Functionality of classes that have association relationships can be increased

• AssignBoatToCustomer custom method– Added in Boat

– Establishes the association between Boat and Customer in both directions in one step

Object-Oriented Application Development Using VB .NET

24

Adding Functionality to the Boat Class

• Mandatory association between Boat and Customer– Possible because

• Bradshaw Marina does not want to keep information about a boat if its owner is not a customer

– Done by• Adding a Customer reference parameter to the Boat

constructor

Object-Oriented Application Development Using VB .NET

25

Adding Functionality to the Boat Class

• Modified Boat TellAboutSelf method– Returns information about

• Boat• Customer

– Possible because• A boat must be associated with a customer

Object-Oriented Application Development Using VB .NET

26

Associating Docks and Slips: A One-to-Many Association Relationship

• Association relationship between Slip and Dock– Two directions of the relationship

• Association relationship between Slip and Dock– One-to-one

» A slip is attached to a dock

• Association relationship between Dock and Slip– One-to-many

» A dock contains many slips

Object-Oriented Application Development Using VB .NET

27

Associating Docks and Slips: A One-to-Many Association Relationship

• To implement – A one-to-one association relationship between Slip

and Dock• Use the technique used to implement the Customer

and Boat associations

– A one-to-many association relationship between Dock and Slip• Requires that a dock instance have reference

variables for more than one slip– Use an ArrayList in the Dock class to hold many Slip

reference variables

Object-Oriented Application Development Using VB .NET

28

Associating Docks and Slips: A One-to-Many Association Relationship

• ArrayList class – Can be used to instantiate a container that can

hold many reference variables

– Has methods which can be used to• Add Slip references• Retrieve Slip references

Object-Oriented Application Development Using VB .NET

29

Introducing the Dock Class

• Dock class – Four previous attributes

• An ID number• A location• Two Boolean variables indicating

– Whether the dock has electricity

– Whether the dock has water

– Fifth attribute• slips

– An ArrayList

– Implements the one-to-many association relationship

Object-Oriented Application Development Using VB .NET

30

Introducing the Dock Class

• Dock class constructor– Sets values for the four attributes

– Instantiates the new ArrayList

– Assigns the new ArrayList to the slips attribute

• GetSlips– A getter method

– Returns the slips reference variable

Object-Oriented Application Development Using VB .NET

31

Introducing the Dock Class

• AddSlip custom method– Used to add a slip to the dock

– Invoked by the Slip AddSlipToDock method

– Contains the slips.Add(aSlip) statement• Used to add a Slip reference to the ArrayList

referenced by the variable slips

Object-Oriented Application Development Using VB .NET

32

Associating the Slip Class with Dock

• Modifications to the Slip class to implement the mandatory one-to-one association relationship– theDock attribute

• A Dock reference variable

– Accessor methods for the Dock reference attribute

Object-Oriented Application Development Using VB .NET

33

Associating the Slip Class with Dock

• Modifications to the Slip class (Continued)– Modified constructor

• Expects a Dock reference parameter– Result: When a slip is instantiated, it must be

associated with a dock

• Invokes the slip’s AddSlipToDock method– AddSlipToDock method establishes the association in

both directions by» Invoking the dock’s AddSlip method using the Me

keyword

Object-Oriented Application Development Using VB .NET

34

Associating the Slip Class with Dock

• Modifications to the Slip class (Continued)– A TellAboutSelf method

• Returns information about– The slip

– The slip’s dock

Object-Oriented Application Development Using VB .NET

35

Adding the Boat and Customer Classes to the Slip Example

• Association relationship between Slip and Boat– Modifications to the Slip class

• A Boat reference attribute• Accessor methods

– GetBoat

– SetBoat

– Modifications to the Boat class• A Slip reference attribute• Accessor methods• AssignBoatToSlip method

Object-Oriented Application Development Using VB .NET

36

Adding the Boat and Customer Classes to the Slip Example

• To include the Customer class in this example– Boat class does not need to be modified

• It is already designed to associate with a customer

• Given a Customer reference– Can navigate to find

• The customer’s boat• The boat’s slip• The slip’s dock

Object-Oriented Application Development Using VB .NET

37

Adding the Boat and Customer Classes to the Slip Example

• Given a Dock reference– Can navigate to

• Each slip• The slip’s boat• The customer who owns the boat

Object-Oriented Application Development Using VB .NET

38

Creating and Using an Association Class – Lease

• Lease class– An association class

• A Lease is like an association between a customer and a slip, but with attributes for– Start date

– End date

– Amount of lease, and so on

– Subclasses• AnnualLease• DailyLease

Object-Oriented Application Development Using VB .NET

39

Creating and Using an Association Class – Lease

• Modifications to the Lease superclass– A Slip reference attribute

– A Customer reference attribute

– Accessor methods

• AnnualLease class– A subclass of Lease

– Inherits the association between• Lease and Slip• Lease and Customer

Object-Oriented Application Development Using VB .NET

40

Creating and Using an Association Class – Lease

• No modifications needed for the Boat class– Reason: Boat is not directly associated with Lease

• Modifications to the Customer class– A Lease reference attribute

– Accessor methods

Object-Oriented Application Development Using VB .NET

41

Creating and Using an Association Class – Lease

• Modifications to the Slip class– A Lease reference attribute

– Accessor methods

– LeaseAnnualSlip method• Creates an AnnualLease instance