+ All Categories
Home > Documents > An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

Date post: 04-Jan-2016
Category:
Upload: arline-palmer
View: 217 times
Download: 3 times
Share this document with a friend
39
An Object-Oriented An Object-Oriented Approach to Approach to Programming Logic and Programming Logic and Design Design Chapter 3 Using Methods and Parameters
Transcript
Page 1: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to An Object-Oriented Approach to

Programming Logic and DesignProgramming Logic and Design

Chapter 3Using Methods and Parameters

Page 2: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using 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

Page 3: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 4: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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)

Page 5: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 6: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 7: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 8: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 9: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 10: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 10

Creating Methods With No Arguments Creating Methods With No Arguments (continued)(continued)

Page 11: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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()

Page 12: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 13: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 13

Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)

Page 14: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 15: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 15

Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)

Page 16: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 16

Creating Methods With One Argument Creating Methods With One Argument (continued)(continued)

Page 17: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 18: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 19: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 19

Creating Methods With Multiple Creating Methods With Multiple ArgumentsArguments (continued)(continued)

Page 20: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 21: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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)

Page 22: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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)

Page 23: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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)

Page 24: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 25: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 26: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 26

Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)

Page 27: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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>

Page 28: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 28

Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)

Page 29: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 29

Creating Instance Methods in a Class Creating Instance Methods in a Class (continued)(continued)

Page 30: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 31: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 32: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 32

Organizing Classes (continued)Organizing Classes (continued)

Page 33: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 34: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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)

Page 35: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 36: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 37: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

An Object-Oriented Approach to Programming Logic and Design 37

An Introduction to Using ConstructorsAn Introduction to Using Constructors(continued)(continued)

Page 38: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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

Page 39: An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

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


Recommended