St. Ambrose CSCI 275 - Writing a Problem Domain Class Definition

Unformatted text preview:

Chapter 6Things to knowReplayCasting Numeric ConstantsPlay Book IINamingVB .NET Naming ConventionsClass Definition StructureDefining ClassDefining AttributesWriting Methods and PropertiesSlide 12Slide 13MethodsCreating an InstancePopulating the Instance AttributesUsing WithCreating Multiple InstancesWriting a Constructor MethodWriting a Constructor MethodSlide 21Writing a TellAboutSelf MethodScope / LifetimeChapter 7Things to KnowCreating Custom MethodsCreating Custom MethodsSlide 28Writing Class Variables and MethodsWriting Class Variables and MethodsGetNumberOfSlips() methodWriting Overloaded MethodsOverloading a ConstructorSlide 34Overloading a Custom MethodSlide 36OverLoading / OverRiding / PolymorphismWorking with ExceptionsWorking with ExceptionsSlide 40Data Validation for slipIdData Validation for slipWidthCatching ExceptionsSlide 441Chapter 6Writing a Problem Domain Class Definition2Things to know•Naming conventions – per handout•Developing a problem domain (PD) class•Defining class attributes•Writing accessor methods and properties•Constructors•Testing a class definitions–Instantiating•Overloading methods•Accessibility of variables and methods•Variable Scope / Lifetime•With / End With•Structure/ End Structure3Replay•Loop structures–Use the FOR loop when it can be determined the number of times the loop must execute–Use the DO when the number of times a loop must execute is indeterminate •Ex… reading a file–Use the FOR EACH loop for accessing all the elements / items of an Array or ArrayList•Using Debug–Watch Variables4Casting Numeric Constants•D = Decimal 800D•R = Double 800R•I = Integer 80I•L = Long 80L•S = Short 80S•F = Single 80F5Play Book II•Naming Convention–Prefix should identify the Object Type–ie.. alstFlightInfo is better than fltArrayList•Comments in every procedure–Code should have no unused / empty procedure•Happens occasionally with Windows Apps•You can use #Region #End Region–show / hide sections of code6Naming•Logical as well as physical Module names should be meaningful–Representative of the task they perform7VB .NET Naming Conventions •Class names–Start with a capital letter–Examples: Customer, Boat•Attribute names–Begin with a lowercase prefix consistent with the type object – Use camel casing–Subsequent words are capitalized–Examples: address, phoneNo•Method names–Begin with an uppercase character–Subsequent words are capitalized–Examples: GetPhoneNo, SetAddress, ComputeLease8Class Definition Structure •Structure of a class definition–Class header–Object attributes–Class Attributes (shared)–Constants–Constructor Methods–Accessor Methods–Custom Methods•Class header–Line of code that identifies the class and some of its characteristics9Defining Class•Class header for the Customer definition:Public Class Customer•Attributes: –defined by declaring variables for each attribute–Encapsulation / data hiding requires accessibility to be Private•Keyword Private is used instead of Dim •Private name As String•Methods10Defining Attributes•When defining Attributes / Methods 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 values11Writing Methods and Properties12Writing Methods and Properties•Methods are written using procedures•VB .NET has two types of 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 statementsEnd Sub13Writing Methods and Properties•A Function procedure definition–A procedure header followed by one or more statementsaccessibility Function procedurename (parameter list) As datatypemethod statementsEnd Function14Methods•Accessor methods–Often called standard methods–Typically not shown on class diagrams•Get accessor methods or getters (always a function)•Set accessor methods or setters–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•Has imbedded GET/SET methods•Ends with End Property•Custom methods–Perform other functions–Shown on class diagrams15Creating an Instance •Define a variable with data type of the class•Instantiate the object Dim firstCustomer As Customer firstCustomer = New Customer() ' create instance Or Dim firstCustomer As Customer = New Customer() ' create instance•The variable firstCustomer points to the newly created Customer instance16Populating the Instance Attributes•Example of using properties–CustomerName property can be used to populate the name attribute ' use property to populate namefirstCustomer.CustomerName = "Eleanor" •Example of using setter methods' invoke set accessors to populate attributesfirstCustomer.SetName("Eleanor")firstCustomer.SetAddress("Atlanta")firstCustomer.SetPhoneNo("123-4567")17Using With•Code to retrieve attribute values and display them: ' define variables to contain attribute values retrievedDim customerName, customerAddress, customerPhoneNo As StringWith firstCustomercustomerName = .GetName()customerAddress = .GetAddress()customerPhoneNo =.GetPhoneNo() End With18Creating Multiple Instances•Code to invoke setters to populate the attributes:With firstCustomer.SetName("Eleanor") ' populate first instance.SetAddress("Atlanta").SetPhoneNo("123-4567")End WithWith secondCustomer.SetName("Mike") ' populate second instance .SetAddress("Boston").SetPhoneNo("467-1234") End WithWith thirdCustomer.SetName("JoAnn") ' populate third instance.SetAddress("St. Louis").SetPhoneNo("765-4321") End With19Writing 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–Does not do anything–Consists of only a header and an End Sub


View Full Document

St. Ambrose CSCI 275 - Writing a Problem Domain Class Definition

Download Writing a Problem Domain Class Definition
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Writing a Problem Domain Class Definition and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Writing a Problem Domain Class Definition 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?