Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Pemrograman VisualMinggu …3… Page 1 MINGGU Ke Tiga Pemrograman Visual Pokok Bahasan: Class, Objects, Methods and Instance Variable Tujuan Instruksional.
The Web Warrior Guide to Web Design Technologies
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
VBA Modules, Functions, Variables, and Constants
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.
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 13: Advanced GUI and Graphics
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
VB .NET Programming Fundamentals
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm – – –Web Site: websupport1.citytech.cuny.edu.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
A First Program Using C#
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Microsoft Visual Basic 2005: Reloaded Second Edition
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
An Object-Oriented Approach to Programming Logic and Design
Tutorial 11 Using and Writing Visual Basic for Applications Code
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 8: Writing Graphical User Interfaces
Chapter 6 Sub Procedures
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Chapter 12 - Designing Multiwindow Applications1 Chapter 12 Designing Multiwindow Applications 12.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Applications Development
Dr. Azeddine Chikh IS444: Modern tools for applications development.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 5 Introduction to Defining Classes
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Object-Oriented Programming: Inheritance and Polymorphism.
Object-Oriented Application Development Using VB.NET 1 Chapter 11 Using Multiple Forms with Problem Domain Classes.
 ASP.NET provides an event based programming model that simplifies web programming  All GUI applications are incomplete without enabling actions  These.
Object-Oriented Application Development Using VB.NET 1 Chapter 13 Introduction to Data Access Classes and Persistence.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Object-Oriented Application Development Using VB.NET 1 Chapter 15 Assembling a Three-Tier Windows Application.
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
CIS16 Application Development and Programming using Visual Basic.net
Tonga Institute of Higher Education
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Object Oriented Programming in java
Presentation transcript:

Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition

Object-Oriented Application Development Using VB.NET 2 Objectives In this chapter, you will: Learn VB.NET naming conventions Develop a problem domain (PD) class definition Define attributes Write methods and properties Test a PD class Create an instance Write a constructor method Write a TellAboutSelf method Write a Tester class as a Form

Object-Oriented Application Development Using VB.NET 3 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

Object-Oriented Application Development Using VB.NET 4 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

Object-Oriented Application Development Using VB.NET 5 Developing a PD Class Definition Customer class –Represents marina’s customers –Has attributes for customer’s Name Address Telephone number

Object-Oriented Application Development Using VB.NET 6 Developing a PD Class Definition

Object-Oriented Application Development Using VB.NET 7 Class Definition Structure Structure of a class definition –Class header –Attribute definitions –Method code Public Class … … … … End Class

Object-Oriented Application Development Using VB.NET 8 Class Definition Structure Class header –Identifies the class and some of its characteristics 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 represents the class name

Object-Oriented Application Development Using VB.NET 9 Defining Attributes Attributes: defined by declaring variable 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: 'attributes Private name As String Private address As String Private phoneNo As String

Object-Oriented Application Development Using VB.NET 10 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

Object-Oriented Application Development Using VB.NET 11 Writing Methods and Properties In OO systems, objects interact by one object sending a message to another object to invoke a method. The interaction can be viewed as client-server model : - –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

Object-Oriented Application Development Using VB.NET 12 Writing Methods and Properties

Object-Oriented Application Development Using VB.NET 13 Writing 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 statements Sub ( ) … … … … … … … End Sub Note: Arguments are passed into parameters.

Object-Oriented Application Development Using VB.NET 14 Writing Methods and Properties A Function procedure definition –A procedure header followed by one or more statements Function ( ) As method statements Return End Function

Object-Oriented Application Development Using VB.NET 15 Methods Accessor methods –Often called standard methods –Typically not shown on class diagrams Custom methods –Perform other functions –Shown on class diagrams

Object-Oriented Application Development Using VB.NET 16 Methods 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 Public Function GetAttributeName ( ) As AttributeDatatype Return AttributeName End Function –Set accessor methods or setters Change, or set, attribute values Named with the prefix “Set” followed by the attribute name Public Sub SetAttributeName ( ByVal paramterName As AttributeDatatype) AttributeName = paramterName End Sub

Object-Oriented Application Development Using VB.NET 17 Methods Example of Get accessor method for name attribute. Public Function GetName ( ) As String Return name End Function Example of Set accessor method for name attribute. Public Sub SetName ( ByVal customerName As String) name = customerName End Sub

Object-Oriented Application Development Using VB.NET 18 Defining Properties A property –Can be used to set and get attribute values –Similar to a method, but appears as an attribute to a client. –Example: text of a Button Class. –Begins with a header indicating a property definition –General Format Public Property ( ) As Get Return End Get Set (ByVal As ) = End Set End Property

Object-Oriented Application Development Using VB.NET 19 Defining Properties Example of A property that Set/Get name Attribute Public Property CustomerName ( ) As String Get Return name End Get Set (ByVal aName As String) name = aName End Set End Property

Object-Oriented Application Development Using VB.NET 20 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

Object-Oriented Application Development Using VB.NET 21 Creating an Instance Creating an instance of a class: ‘ Create Ref Variable of Customer Data Type Dim firstCustomer As Customer ' create instance of Customer & assigns it to Ref Variable firstCustomer = New Customer( ) The variable firstCustomer points to the newly created Customer instance The above instructions can be rewritten as follow: Dim firstCustomer As Customer = New Customer( )

Object-Oriented Application Development Using VB.NET 22 Creating an Instance

Object-Oriented Application Development Using VB.NET 23 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"

Object-Oriented Application Development Using VB.NET 24 Creating an Instance Example of using setter methods ' invoke set accessors to populate attributes firstCustomer.SetName("Eleanor") firstCustomer.SetAddress("Atlanta") firstCustomer.SetPhoneNo(" ")

Object-Oriented Application Development Using VB.NET 25 Creating an Instance

Object-Oriented Application Development Using VB.NET 26 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)

Object-Oriented Application Development Using VB.NET 27 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 Code to create three instances of the Customer class: firstCustomer = New Customer() secondCustomer = New Customer() thirdCustomer = New Customer()

Object-Oriented Application Development Using VB.NET 28 Creating Multiple Instances Code to invoke setters to populate the attributes: firstCustomer.SetName("Eleanor") ' populate first instance firstCustomer.SetAddress("Atlanta") firstCustomer.SetPhoneNo(" ") secondCustomer.SetName("Mike") ' populate second instance secondCustomer.SetAddress("Boston") secondCustomer.SetPhoneNo(" ") thirdCustomer.SetName("JoAnn") ' populate third instance thirdCustomer.SetAddress("St. Louis") thirdCustomer.SetPhoneNo(" ")

Object-Oriented Application Development Using VB.NET 29 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())

Object-Oriented Application Development Using VB.NET 30 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. Why? 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

Object-Oriented Application Development Using VB.NET 31 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 during the instantiation process.

Object-Oriented Application Development Using VB.NET 32 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

Object-Oriented Application Development Using VB.NET 33 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 When the setters have data validation statements, the constructor must invoke them to populate attributes. Example: –Dim aCust as Customer –aCust = New Customer(“Ali”, “Jedda”, “ ”)

Object-Oriented Application Development Using VB.NET 34 Writing a TellAboutSelf Method Good design: changes in one class (adding new attributes, and etc) should be insulated from outside classes (Clients) to reduce maintenance requirements especially when using getter methods. 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

Object-Oriented Application Development Using VB.NET 35 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

Object-Oriented Application Development Using VB.NET 36 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

Object-Oriented Application Development Using VB.NET 37 Writing a Tester Class as a Form

Object-Oriented Application Development Using VB.NET 38 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 Event handler of create button will create & initialize instance of Customer Class. Event handler of display button will display the information of current instance by invoking the TellAboutSelf method. Event handler of close button will remove the form from memory by invoking dispose method.

Object-Oriented Application Development Using VB.NET 39 Lab 4: Chapter 6 The Arraylist object you will create should be a dynamic list of Student Class.