Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
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.
Road Map Introduction to object oriented programming. Classes
VBA Modules, Functions, Variables, and Constants
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
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
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.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Written by: Dr. JJ Shepherd
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Classes and Objects
Chapter 14: Exception Handling
Exception Handling Chapter 9 Edited by JJ.
CIS16 Application Development and Programming using Visual Basic.net
CIS 199 Final Review.
Presentation transcript:

Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes

Object-Oriented Application Development Using VB.NET 2 Objectives In this chapter, you will: Write a new problem domain class definition Create custom methods Write class variables and methods Write overloaded methods Work with exceptions

Object-Oriented Application Development Using VB.NET 3 Writing a New Problem Domain Class Definition Slip class –Attributes slipId slipWidth slipLength –Custom method LeaseSlip

Object-Oriented Application Development Using VB.NET 4 Writing a New Problem Domain Class Definition Class definition of Slip –Class header Public Class Slip –Attribute definition statements Private slipId As Integer Private slipWidth As Integer Private slipLength As Integer

Object-Oriented Application Development Using VB.NET 5 Writing a New Problem Domain Class Definition Class definition of Slip (Continued) –Parameterized constructor Enables attributes to be automatically populated whenever an instance is created Public Sub New(ByVal aSlipId As Integer, _ ByVal aSlipWidth As Integer, ByVal aSlipLength As Integer) Argument data types must be assignment compatible with parameter data types

Object-Oriented Application Development Using VB.NET 6 Writing a New Problem Domain Class Definition Class definition of Slip (Continued) –Statements to populate the Slip attributes To avoid redundant code, setter methods are invoked instead of directly assigning the values 'invoke setter methods to populate attributes SetSlipId(aSlipId) SetSlipWidth(aSlipWidth) SetSlipLength(aSlipLength)

Object-Oriented Application Development Using VB.NET 7 Writing a New Problem Domain Class Definition Class definition of Slip (Continued) –TellAboutSelf method Invokes the three getters Concatenates the returned values Returns the result Public Function TellAboutSelf() As String Dim info As String info = "Slip: Id = " & GetSlipId() & ", Width = " & GetSlipWidth() _ & ", Length = " & GetSlipLength() Return info End Function

Object-Oriented Application Development Using VB.NET 8 Writing a New Problem Domain Class Definition Class definition of Slip (Continued) –TellAboutSelf method Polymorphic methods –Two methods with the same name, residing in different classes, that behave differently –Example: TellAboutSelf methods for Customer and Slip classes –Setter and getter methods

Object-Oriented Application Development Using VB.NET 9 Creating Custom Methods Custom methods –Process data Accessor methods –Store and retrieve attribute values LeaseSlip method –A custom method –Computes the lease fee for a slip Fees range from $800 to $1,500 depending on the slip’s width

Object-Oriented Application Development Using VB.NET 10 Creating Custom Methods LeaseSlip method –A Function procedure Will return a value: the lease fee amount –Has Public accessibility Can be invoked by any object –Return data type: Single Will compute and return the lease fee, a dollar amount –Method header: Public Function LeaseSlip() As Single

Object-Oriented Application Development Using VB.NET 11 Creating Custom Methods Select statement for computing lease fee: Dim fee As Single Select Case slipWidth Case 10 fee = 800 Case 12 fee = 900 Case 14 fee = 1100 Case 16 fee = 1500 Case Else fee = 0 End Select Return fee

Object-Oriented Application Development Using VB.NET 12 Writing Class Variables and Methods Instance variables and methods –Each instance has a copy Class variables and methods –Shared by all instances of the class –Each instance does not have its own copy –Declared using the keyword Shared

Object-Oriented Application Development Using VB.NET 13 Writing Class Variables and Methods Variable numberOfSlips can be used to track the total number of slips numberOfSlips variable –Initialized to 0 –Private accessibility Will be accessed only by methods within the Slip class Private Shared numberOfSlips As Integer = 0

Object-Oriented Application Development Using VB.NET 14 Writing Class Variables and Methods Code added to the Slip constructor for the numberOfSlips variable 'increment shared attribute numberOfSlips += 1 –Increments numberOfSlips each time a slip instance is created

Object-Oriented Application Development Using VB.NET 15 Writing Class Variables and Methods GetNumberOfSlips() method –Returns the contents of numberOfSlips –Written as a Function procedure Returns a value –Written as a shared method Not associated with any individual instance –Has Public accessibility Will be invoked by other classes

Object-Oriented Application Development Using VB.NET 16 GetNumberOfSlips() method 'shared (class) method Public Shared Function GetNumberOfSlips() As Integer Return numberOfSlips End Function

Object-Oriented Application Development Using VB.NET 17 Writing Overloaded Methods Method signature: Name and parameter list A class definition can contain several methods with the same name, as long as their parameter lists differ –VB.NET identifies a method by its signature, not only by its name Overloaded method: a method that has the same name as another method in the same class, but a different parameter list

Object-Oriented Application Development Using VB.NET 18 Overloading a Constructor Problem –Most slips at Bradshaw Marina are 12 feet wide and 25 feet long, but a few have different widths and lengths –Current Slip constructor requires three arguments: slipId, slipWidth, and slipLength

Object-Oriented Application Development Using VB.NET 19 Overloading a Constructor Solution –Write a second Slip constructor that has only a single parameter for slipId –Include statements to assign the default values of 12 and 25 to slipWidth and slipLength, respectively

Object-Oriented Application Development Using VB.NET 20 Overloading a Constructor Constants can be used for the default width and length values: 'constants Private Const DEFAULT_SLIP_WIDTH As Integer = 12 Private Const DEFAULT_SLIP_LENGTH As Integer = 25

Object-Oriented Application Development Using VB.NET 21 Overloading a Constructor Code for second constructor: '1-parameter constructor 'Overloads keyword not used with constructors Public Sub New(ByVal aSlipId As Integer) 'invoke 3-parameter constructor, passing default values Me.New(aSlipId, DEFAULT_SLIP_WIDTH, DEFAULT_SLIP_LENGTH) End Sub

Object-Oriented Application Development Using VB.NET 22 Overloading a Constructor VB.NET determines which constructor to invoke by the argument list –If the argument consists of three values Original constructor with three parameters is executed –If the argument consists of a single value New constructor with one parameter is invoked

Object-Oriented Application Development Using VB.NET 23 Overloading a Custom Method Problem –Bradshaw Marina permits a discounted lease fee under certain conditions Solution –A second version of the LeaseSlip method that accepts a value for the percentage discount Will overload the original LeaseSlip method

Object-Oriented Application Development Using VB.NET 24 Overloading a Custom Method Two LeaseSlip methods in Slip, each with a different signature –Original LeaseSlip has an empty parameter list –Second LeaseSlip has a parameter variable named aDiscountPercent

Object-Oriented Application Development Using VB.NET 25 Overloading a Custom Method Header for new method –Must contain the keyword Overloads Public Overloads Function LeaseSlip(ByVal aDiscountPercent As Single) _ As Single

Object-Oriented Application Development Using VB.NET 26 Overloading a Custom Method New method: 'overloaded custom method LeaseSlip if discount requested Public Overloads Function LeaseSlip(ByVal aDiscountPercent As Single) _ As Single 'invoke LeaseSlip() to get fee Dim fee As Single = Me.LeaseSlip() 'calculate and return discount fee Dim discountFee As Single = fee * (100 - aDiscountPercent) / 100 Return discountFee End Function

Object-Oriented Application Development Using VB.NET 27 Overloading a Custom Method VB.NET will execute the appropriate method depending on the argument list –If no argument is passed Original LeaseSlip method is invoked –If an argument is coded Overridden method is executed

Object-Oriented Application Development Using VB.NET 28 Working with Exceptions An exception –Used to notify the programmer of errors, problems, and other unusual conditions that may occur while the system is running –An instance of the Exception class or one of its subclasses

Object-Oriented Application Development Using VB.NET 29 Working with Exceptions VB.NET uses five keywords to deal with exceptions –Try Used by the client –Catch Used by the client –Finally Used by the client –End Try Used by the client –Throw Used by the server

Object-Oriented Application Development Using VB.NET 30 Working with Exceptions

Object-Oriented Application Development Using VB.NET 31 Data Validation for slipId slipId value should be within the range of 1 through 50 SetSlipId method –Data validation logic used to verify that slipId values are in the range of 1 through 50 –Create and throw an exception instance if a value is outside the valid range

Object-Oriented Application Development Using VB.NET 32 Data Validation for slipId Constant MAXIMUM_NUMBER_OF_SLIPS –Simplifies future maintenance: should Bradshaw Marina decide to have docks with more than 50 slips Private Const MAXIMUM_NUMBER_OF_SLIPS As Integer = 50

Object-Oriented Application Development Using VB.NET 33 Data Validation for slipId If statement –Determines if aSlipId is within valid range –If a value is outside acceptable range An instance of Exception class is created and thrown

Object-Oriented Application Development Using VB.NET 34 Data Validation for slipId Public Sub SetSlipId(ByVal aSlipId As Integer) 'reject slipId if maximum If aSlipId MAXIMUM_NUMBER_OF_SLIPS Then Throw New Exception("Slip Id not between 1 and " _ & MAXIMUM_NUMBER_OF_SLIPS) Else slipId = aSlipId End If End Sub

Object-Oriented Application Development Using VB.NET 35 Data Validation for slipWidth Code must be added to the SetSlipWidth method to verify that the width parameter is a valid width value: 10, 12, 14, or 16 VALID_SLIP_WIDTHS –An integer array –Stores valid width values Private Shared ReadOnly VALID_SLIP_WIDTHS As Integer() = {10, 12, 14, 16}

Object-Oriented Application Development Using VB.NET 36 Data Validation for slipWidth Validation logic –Will iterate the array, seeking a match between the parameter value received (aSlipWidth) and an array value –If a matching value is found Parameter is valid –If the end of the array is reached without finding a matching value Parameter contains an invalid value An exception is created and thrown

Object-Oriented Application Development Using VB.NET 37 Data Validation for slipWidth Validation logic: Dim validWidth As Boolean = False 'search for a valid width Dim i As Integer For i = 0 To VALID_SLIP_WIDTHS.Length - 1 If aSlipWidth = VALID_SLIP_WIDTHS(i) Then validWidth = True Next i

Object-Oriented Application Development Using VB.NET 38 Data Validation for slipWidth If statement to test validWidth –If true Width attribute is populated with parameter value –If not true An instance of Exception is created and thrown 'if a valid width found, set value If validWidth Then slipWidth = aSlipWidth Else 'else throw exception Throw New Exception("Invalid Slip Width") End If

Object-Oriented Application Development Using VB.NET 39 Catching Exceptions If a method that might throw an exception is invoked, the invoking code must be prepared to catch the exception –.NET CLR will terminate processing if an exception is thrown and not caught Try block structure –Keyword Try –Code containing invoking statement or statements –Keyword End Try

Object-Oriented Application Development Using VB.NET 40 Catching Exceptions Catch block –Receives and deals with an exception –Must specify a parameter variable to receive a reference to the exception instance Dim aSlip As Slip Try 'force an exception with invalid slipID (150) aSlip = New Slip(150, 10, 25) Console.WriteLine(aSlip.TellAboutSelf()) Catch theException As Exception Console.WriteLine("An exception was caught: " & _ theException.ToString()) End Try

Object-Oriented Application Development Using VB.NET 41 Catching Exceptions Finally block –Used to add statements that are to be executed whether or not an exception was caught Finally Console.WriteLine("Finally block always executes")

Object-Oriented Application Development Using VB.NET 42 Summary Custom methods: methods which process data Class variables and methods are associated with the class instead of individual instances A new instance is given a copy of all instance variables and access to instance methods A new instance does not get a copy of class variables and methods A method signature consists of the method name and its parameter list VB.NET identifies a method by its signature

Object-Oriented Application Development Using VB.NET 43 Summary An overloaded method has the same name as another method in the same class, but a different signature Exceptions notify the programmer of errors, problems, and other unusual conditions that may occur while the system is running Keywords dealing with exceptions: Try, Catch, Finally, End Try, and Throw CLR terminates processing if an exception is thrown and not caught