51
CS708 Fall 2004 • Professor Douglas Moody – MW – 2:15-3:55 pm – Friday – 6:00-9:20 pm – Email – [email protected] – Web Site: websupport1.citytech.cuny.edu • Click on Technology and Design • Select My page • Prerequisite: VB.NET CS608

CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – [email protected] –Web Site: websupport1.citytech.cuny.edu

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

CS708 Fall 2004• Professor Douglas Moody

– MW – 2:15-3:55 pm– Friday – 6:00-9:20 pm– Email – [email protected]– Web Site: websupport1.citytech.cuny.edu

• Click on Technology and Design• Select My page

• Prerequisite: VB.NET CS608

Page 2: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

CS708 Fall 2004

• Two Texts– CS608 Text (Object-Oriented Application

Development Using MicroSoft Visual Basic.NET (Thomson)

– Advanced Programming Using Visual Basic .NET, Bradley-Millspaugh

• Attendance : Mandatory , maximum of 10% absence

Page 3: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

CS708 Fall 2004• Grading

– 3 Tests (30%)– Final (30%)– Programs (25%) (Homework Assignments)– Research Project (15%)

• Programs / Homeworks must done in order to succeed in the course

• Plan on 1 and ½ hours of non class time for the course

Page 4: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

CS708 Fall 2004 Structure

• Implement advanced class structures within a case study

• Learn and add data management techniques (persistence) to the case study

• Learn and add web services to the case study

Page 5: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Chapter 6(Thomson Book)

Writing a Problem Domain Class

Definition

Page 6: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Objectives

We will review:

• Parts of an object

– Properties, Attributes

– Methods

• Constructor methods

Page 7: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

VB .NET Naming Conventions • Class names

– Start with a capital letter– Examples: Customer, Boat

• Attribute names– Begin with a lowercase character– Subsequent words are capitalized– Examples: address, phoneNo

• Method names– Begin with an uppercase character– Subsequent words are capitalized– Examples: GetPhoneNo, SetAddress, ComputeLease

Page 8: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Developing a PD Class Definition

• Customer class– Represents customers– Has attributes for customer’s

• Name• Address• Telephone number

Page 9: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Developing a PD Class Definition

Page 10: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Class Definition Structure

• Structure of a class definition– Class header– Attribute definitions– Method code

• Class header– Line of code that identifies the class and

some of its characteristics

Page 11: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Class Definition Structure

• Class header for the Customer definition:Public Class Customer

• Keyword Public indicates that the class has public accessibility

• Keyword Class indicates that this line of code is a class header

• Customer establishes the class name

Page 12: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Defining Attributes

• Attributes: defined by declaring variables for each attribute

• An attribute definition– Written in the same way as a variable is declared,

except:• Keyword Private is used instead of Dim

• Customer attributes are defined as follows:'attributesPrivate name As StringPrivate address As String

Private phoneNo As String

Page 13: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Defining Attributes• When defining attributes, a variable’s accessibility can

be:– Public: allows any class to access the variable directly– Private: prohibits direct access; variable is accessible only

within the class where it is defined– Protected: allows subclasses to have direct access– Friend: permits classes within the same assembly to have

access• Assembly: a collection of one or more projects deployed as an

application

• Accessor methods can be invoked by other classes to access attribute values

Page 14: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing Methods and Properties

• In OO systems, objects interact by one object sending a message to another object to invoke a method– Client object: object sending the message

• Sends a message invoking a server method• Can send values in the form of arguments

– Server object: object receiving the message • Performs the requested task• May return a value to the client

Page 15: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing Methods and Properties

Page 16: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing Methods and Properties

• Methods are written using procedures• VB .NET has two types of procedures: Sub

procedures and Function procedures– A Sub procedure does not return a value– A Function procedure returns a value

• A Sub procedure definition– A procedure header followed by one or more

statementsaccessibility Sub procedurename(parameter list)

method statements

End Sub

Page 17: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing Methods and Properties

• A Function procedure definition– A procedure header followed by one or more

statementsaccessibility Function procedurename (parameter list) As datatype

method statements

End Function

Page 18: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Defining Attributes

• Accessor methods– Often called standard methods– Typically not shown on class diagrams

• Custom methods– Perform other functions– Shown on class diagrams

Page 19: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Defining Attributes

• Two types of accessor methods– Get accessor methods or getters

• Retrieve, or get, attribute values• Named with the prefix “Get” followed by the

attribute name

– Set accessor methods or setters• Change, or set, attribute values• Named with the prefix “Set” followed by the

attribute name

Page 20: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Defining Attributes

• A property– Can be used to set and get attribute values– Similar to a method, but appears as an

attribute to a client– Begins with a header indicating a property

definition– Ends with End Property

Page 21: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Testing a PD Class

• A tester class simulates the way a client might send messages

• For example: TesterOne class can invoke methods in the Customer class definition– TesterOne is the client and Customer is the

server

• TesterOne class – Startup object for the project– Main method begins execution when it is loaded

Page 22: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating an Instance

• In TesterOne– An instance of the Customer class is created– A variable with data type Customer references

this instance

• Creating an instance of a class:Dim firstCustomer As Customer = New Customer() ' create instance

• The variable firstCustomer points to the newly created Customer instance

Page 23: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating an Instance

Page 24: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating an Instance

• Attributes of the Customer instance initially have no values

• Instance attributes can be populated by– Setter methods– Properties

• Example of using properties– CustomerName property can be used to populate

the name attribute ' use property to populate name

firstCustomer.CustomerName = "Eleanor"

Page 25: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating an Instance

• Example of using setter methods' invoke set accessors to populate attributes

firstCustomer.SetName("Eleanor")

firstCustomer.SetAddress("Atlanta")

firstCustomer.SetPhoneNo("123-4567")

Page 26: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating an Instance

Page 27: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating an Instance

• Code to retrieve attribute values and display them:

' define variables to contain attribute values retrieved

Dim customerName, customerAddress, customerPhoneNo As String

customerName = firstCustomer.GetName()

customerAddress = firstCustomer.GetAddress()

customerPhoneNo = firstCustomer.GetPhoneNo()

' display the retrieved attribute values

Console.WriteLine("The name is " + customerName)

Console.WriteLine("The address is " + customerAddress)

Console.WriteLine("The phone is " + customerPhoneNo)

Page 28: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating Multiple Instances

• A tester class can create more than one instance of a class

• For example: TesterTwo will create three instances using the Customer class definition

• To create three instances, three reference variables are needed:

Dim firstCustomer, secondCustomer, thirdCustomer As Customer

Page 29: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating Multiple Instances

• Code to create three instances of the Customer class:

firstCustomer = New Customer()

secondCustomer = New Customer()

thirdCustomer = New Customer()

Page 30: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating Multiple Instances

• Code to invoke setters to populate the attributes:

firstCustomer.SetName("Eleanor") ' populate first instancefirstCustomer.SetAddress("Atlanta")firstCustomer.SetPhoneNo("123-4567")

secondCustomer.SetName("Mike") ' populate second instance secondCustomer.SetAddress("Boston")secondCustomer.SetPhoneNo("467-1234")

 thirdCustomer.SetName("JoAnn") ' populate third instancethirdCustomer.SetAddress("St. Louis")thirdCustomer.SetPhoneNo("765-4321")

Page 31: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Creating Multiple Instances

• Each instance has– Its own identity– Its own attribute values– The ability to respond to messages

• Statements to retrieve and display each customer’s name:

' display names of all three customers

Console.WriteLine(firstCustomer.GetName())

Console.WriteLine(secondCustomer.GetName())

Console.WriteLine(thirdCustomer.GetName())

Page 32: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Constructor Method

• Constructor– A method that is automatically invoked whenever an

instance of a class is created using the keyword New– Named New– Written as a Sub procedure; cannot return a value

• Default constructor– Created by VB .NET if the programmer does not write

a constructor– Does not do anything– Consists of only a header and an End Sub statement

Page 33: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Constructor Method

• Parameterized constructor– Created by the programmer– Can contain a parameter list to receive

arguments that are used to populate the instance attributes

Page 34: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Constructor Method

• Parameterized constructor for Customer:

'constructor (3 parameters)

Public Sub New(ByVal aName As String, ByVal

anAddress As String, ByVal aPhoneNo As String)

SetName(aName)

SetAddress(anAddress)

SetPhoneNo(aPhoneNo)

End Sub

• This constructor invokes setter methods to populate attributes

Page 35: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Constructor Method

• Alternative design: constructor assigns values directly to attribute variables instead of invoking setter methods

Public Sub New(ByVal aName As String, ByVal

anAddress As String, ByVal aPhoneNo As String)

name = aName

address = anAddress

phoneNo = aPhoneNo

End Sub

Page 36: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a TellAboutSelf Method

• Good design: changes in one class should be insulated from outside classes to reduce maintenance requirements

• To accomplish this, a TellAboutSelf method can be used– Can be invoked to retrieve all of the attribute values for

an instance– Places all the values in a String instance– Returns the String instance to the invoking client– Should have public accessibility– Should be written as a Function procedure

Page 37: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a TellAboutSelf Method

• TellAboutSelf method for Customer'TellAboutSelf method

Public Function TellAboutSelf() As String

Dim info As String

info = "Name = " & GetName() & _

", Address = " & GetAddress() & _

", Phone No = " & GetPhoneNo()

Return info

End Function

Page 38: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Tester Class as a Form

• In previous examples – Tester classes were written as modules– A console application was used to run them

• Another approach to writing tester classes: making them GUIs

• Form– A visible GUI object– Can have push buttons and other GUI objects

Page 39: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Tester Class as a Form

Page 40: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Writing a Tester Class as a Form

• When a button is clicked on a form, an event is created

• Event procedure or event handler– A Sub procedure– Executes when an event occurs

Page 41: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Introducing the Bradshaw Marina Case Study

• Tasks of development team– Analyze business and identify system functions– Begin object-oriented analysis to identify the

required use cases and scenarios, creating use case diagrams

– Identify required problem domain classes and create class diagram

– Develop sequence diagrams to model object interactions

Page 42: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Exploring the Background of Bradshaw Marina

• Bradshaw Marina– A privately owned corporation that rents boat

slips and provides boat services on Clinton Lake

– Wants an automated system to track customers, slips they lease, and boats in the slips

Page 43: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Diagram of the Business Terms

Page 44: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Exploring the Background of Bradshaw Marina

• System– Maintain basic information for customers,

slips, and boats, and perform day-to-day business tasks

– Later we will add a persistent database component and a web interface

Page 45: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Identifying Bradshaw Use Cases and Scenarios

Page 46: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Some Early Class Identification

Page 47: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Developing a PD Class Definition

• Bradshaw Marina system PD classes include– Customer– Boat– Slip– Dock

• Class definition – Code that represents a class– Contains attributes and methods of the object

Page 48: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Identifying Bradshaw Problem Domain Classes

• To explore problem domain classes, the development team would– Meet with Bradshaw Marina to ask about

things that are involved in the work of the marina

• For example – customers, boats, leases, slips, and docks

– Begin an initial class diagram that includes these potential classes

Page 49: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Identifying Bradshaw Problem Domain Classes

• Development team would further develop the class diagram by– Showing generalization/specialization

hierarchies – Adding specific information about each class– Identifying and modeling the association

relationship among classes

Page 50: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Basic Case Study Classes

Page 51: CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm –Email – dmoody@citytech.cuny.edu –Web Site: websupport1.citytech.cuny.edu

Overall Case Study Diagram