Upload
arline-palmer
View
221
Download
3
Embed Size (px)
Citation preview
An Object-Oriented Approach to An Object-Oriented Approach to
Programming Logic and DesignProgramming Logic and Design
Chapter 3Using Methods and Parameters
An Object-Oriented Approach to Programming Logic and Design 2
ObjectivesObjectives
• Create methods with and without arguments• Create instance methods in a class• Explore the rationale behind data hiding• Organize classes
An Object-Oriented Approach to Programming Logic and Design 3
Objectives (continued)Objectives (continued)
• Understand the role of the this reference• Begin to understand how to use constructors
An Object-Oriented Approach to Programming Logic and Design 4
Creating Methods With and Without Creating Methods With and Without ArgumentsArguments
• Method – a program module containing statements that carry out a task
• Invoking, or calling, a method causes it to execute
• A method may require that data items (called arguments or parameters) be provided to it
• A method may or may not send back data (called a return value)
An Object-Oriented Approach to Programming Logic and Design 5
Creating Methods With and Without Creating Methods With and Without Arguments (continued)Arguments (continued)
Example: a class with no arguments and no return value
An Object-Oriented Approach to Programming Logic and Design 6
Creating Methods With and Without Creating Methods With and Without Arguments (continued)Arguments (continued)
• Method declaration (header) contains:– Optional access modifiers (public, private, or
protected)
– Return type
– Method name
– Optional list of arguments, separated by commas
An Object-Oriented Approach to Programming Logic and Design 7
Creating Methods With No ArgumentsCreating Methods With No Arguments
• Creating methods with no arguments:– Place the method within the class that will use it,
but not within any other method
An Object-Oriented Approach to Programming Logic and Design 8
Creating Methods With No Arguments Creating Methods With No Arguments (continued)(continued)
Invoking a method with no arguments
An Object-Oriented Approach to Programming Logic and Design 9
Creating Methods With No Arguments Creating Methods With No Arguments (continued)(continued)
Program flow in a method call
An Object-Oriented Approach to Programming Logic and Design 10
Creating Methods With No Arguments Creating Methods With No Arguments (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 11
Creating Methods With No Arguments Creating Methods With No Arguments (continued)(continued)
• Full name of a method:
<class name>.<method name>
Example:
Hello.nameAndAddress()
An Object-Oriented Approach to Programming Logic and Design 12
Creating Methods With One ArgumentCreating Methods With One Argument
• Requires the following in the method header:– The type of the argument
– A local name for the argument
• Argument receives a value that is passed in when the method is invoked
• Any method that does not return a value is declared with type void
An Object-Oriented Approach to Programming Logic and Design 13
Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 14
Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)
• When calling a method with arguments, you can pass – Values
– Variables
– Constants
• Each time a method is called, it is reinitialized to use the new values for arguments that are passed in the call
An Object-Oriented Approach to Programming Logic and Design 15
Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 16
Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 17
Creating Methods With Multiple Creating Methods With Multiple ArgumentsArguments
• Creating methods with multiple arguments:– Argument list is separated by commas
– Each argument’s type is specified with the argument name, even if two or more arguments are the same type
– Argument type is placed before the argument name
An Object-Oriented Approach to Programming Logic and Design 18
Creating Methods With Multiple Creating Methods With Multiple Arguments (continued)Arguments (continued)
Method with two arguments
An Object-Oriented Approach to Programming Logic and Design 19
Creating Methods With Multiple Creating Methods With Multiple ArgumentsArguments (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 20
Creating Methods With Multiple Creating Methods With Multiple Arguments (continued)Arguments (continued)
• Arguments passed to a method must match the method’s declaration in: – Number of arguments
– Type of each argument
– Order of each argument in the list
An Object-Oriented Approach to Programming Logic and Design 21
Creating Methods that Return ValuesCreating Methods that Return Values
• Return type can be any type• If no value is to be returned, the type is void• Return type is placed in the header before the
method name
Example:public static numeric predictRaise(numeric money)
An Object-Oriented Approach to Programming Logic and Design 22
Creating Methods that Return Values Creating Methods that Return Values (continued)(continued)
• Method can only return at most one value• Return value can be assigned to a variable by
using the method call as a variable
Example:
myNewSalary = predictRaise(mySalary)
An Object-Oriented Approach to Programming Logic and Design 23
Creating Methods that Return Values Creating Methods that Return Values (continued)(continued)
• Return value of a method can be used as a variable
Example:print “New salary is”, predictRaise(mySalary)
An Object-Oriented Approach to Programming Logic and Design 24
Creating Instance Methods in a ClassCreating Instance Methods in a Class
• Every object that is an instance of a class is assumed to possess the same methods as the class
• Static methods: methods for which no object needs to exist
• Non-static methods: methods that exist to be used with an object created from a class
An Object-Oriented Approach to Programming Logic and Design 25
Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)
• Mutator methods: methods which set values• Accessor methods: methods which retrieve values
An Object-Oriented Approach to Programming Logic and Design 26
Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 27
Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)
• A class does not create an object; an object must be instantiated from the class
• Once instantiated, a class’s methods can be invoked by name
Syntax:
<object identifier>.<method name>
An Object-Oriented Approach to Programming Logic and Design 28
Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 29
Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 30
Exploring the Rationale Exploring the Rationale Behind Data HidingBehind Data Hiding
• Accessing data only through public methods of a class guarantees that the data will only be manipulated in an approved way
• Methods can be written to ensure that the data conforms to required standards
• Example: ensuring that all phone numbers have area codes
An Object-Oriented Approach to Programming Logic and Design 31
Organizing ClassesOrganizing Classes
• Data fields: – No special order required, but key data items
(used as database look-up values) placed first
– Common practice to declare all data items first, before any methods
• Methods:– Common practice to order them alphabetically
by name, or to put them in get/set pairs
An Object-Oriented Approach to Programming Logic and Design 32
Organizing Classes (continued)Organizing Classes (continued)
An Object-Oriented Approach to Programming Logic and Design 33
Understanding the Role Understanding the Role of the of the thisthis Reference Reference
• Just one copy of a class’s methods are stored in memory, and all objects instantiated from that class use the same stored method
• Each object has its own set of data• The this reference (or pointer variable) allows
the programmer to specify the use of the particular object instance
• The this reference is implicitly used when the object’s code is running
An Object-Oriented Approach to Programming Logic and Design 34
Understanding the Role Understanding the Role of the of the thisthis Reference (continued) Reference (continued)
An Object-Oriented Approach to Programming Logic and Design 35
An Introduction to Using ConstructorsAn Introduction to Using Constructors
• Constructor method establishes the object when it is instantiated
• Default constructor requires no arguments, and is created automatically by the compiler for every class you write
• Constructor class has the same name as the class name in some languages
• Constructors are usually declared as public so that other classes can instantiate objects of this class
An Object-Oriented Approach to Programming Logic and Design 36
An Introduction to Using ConstructorsAn Introduction to Using Constructors(continued)(continued)
• Constructor is a method; it can contain any valid program statements
• Constructor is usually placed as the first method in the class
• Constructor may have arguments that have to be supplied when creating a new object from the class
An Object-Oriented Approach to Programming Logic and Design 37
An Introduction to Using ConstructorsAn Introduction to Using Constructors(continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 38
SummarySummary
• Method is a procedure that contains a series of statements to accomplish a task
• Methods may contain one or more arguments defining data that must be supplied when calling the method
• Method may return a value of a specific type, or may be void
• An instantiated object possesses the methods and attributes of the class from which it was created
An Object-Oriented Approach to Programming Logic and Design 39
Summary (continued)Summary (continued)
• Data hiding using encapsulation guarantees that the data may only be manipulated by the methods of a class
• Most data items are declared as private to support data hiding
• this reference is a pointer to the instantiated object whose code is currently running
• Constructor methods are created automatically for each class, and are executed when the object is created from the class