Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters."— Presentation transcript:

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

2 An Object-Oriented Approach to Programming Logic and Design 2 Objectives Create methods with and without arguments Create instance methods in a class Explore the rationale behind data hiding Organize classes

3 An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Understand the role of the this reference Begin to understand how to use constructors

4 An Object-Oriented Approach to Programming Logic and Design 4 Creating Methods With and Without Arguments 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)

5 An Object-Oriented Approach to Programming Logic and Design 5 Creating Methods With and Without Arguments (continued) Example: a class with no arguments and no return value

6 An Object-Oriented Approach to Programming Logic and Design 6 Creating Methods With and Without Arguments (continued) Method declaration (header) contains: –Optional access modifiers (public, private, or protected) –Return type –Method name –Optional list of arguments, separated by commas

7 An Object-Oriented Approach to Programming Logic and Design 7 Creating 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

8 An Object-Oriented Approach to Programming Logic and Design 8 Creating Methods With No Arguments (continued) Invoking a method with no arguments

9 An Object-Oriented Approach to Programming Logic and Design 9 Creating Methods With No Arguments (continued) Program flow in a method call

10 An Object-Oriented Approach to Programming Logic and Design 10 Creating Methods With No Arguments (continued)

11 An Object-Oriented Approach to Programming Logic and Design 11 Creating Methods With No Arguments (continued) Creating Methods With No Arguments (continued) Full name of a method:. Example: Hello.nameAndAddress()

12 An Object-Oriented Approach to Programming Logic and Design 12 Creating 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

13 An Object-Oriented Approach to Programming Logic and Design 13 Creating Methods With One Argument (continued)

14 An Object-Oriented Approach to Programming Logic and Design 14 Creating Methods With One Argument (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

15 An Object-Oriented Approach to Programming Logic and Design 15 Creating Methods With One Argument (continued)

16 An Object-Oriented Approach to Programming Logic and Design 16 Creating Methods With One Argument (continued)

17 An Object-Oriented Approach to Programming Logic and Design 17 Creating Methods With Multiple Arguments 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

18 An Object-Oriented Approach to Programming Logic and Design 18 Creating Methods With Multiple Arguments (continued) Method with two arguments

19 An Object-Oriented Approach to Programming Logic and Design 19 Creating Methods With Multiple Arguments (continued)

20 An Object-Oriented Approach to Programming Logic and Design 20 Creating Methods With Multiple 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

21 An Object-Oriented Approach to Programming Logic and Design 21 Creating 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)

22 An Object-Oriented Approach to Programming Logic and Design 22 Creating Methods that Return Values (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)

23 An Object-Oriented Approach to Programming Logic and Design 23 Creating Methods that Return Values (continued) Return value of a method can be used as a variable Example: print “New salary is”, predictRaise(mySalary)

24 An Object-Oriented Approach to Programming Logic and Design 24 Creating 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

25 An Object-Oriented Approach to Programming Logic and Design 25 Creating Instance Methods in a Class (continued) Mutator methods: methods which set values Accessor methods: methods which retrieve values

26 An Object-Oriented Approach to Programming Logic and Design 26 Creating Instance Methods in a Class (continued)

27 An Object-Oriented Approach to Programming Logic and Design 27 Creating Instance Methods in a Class (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:.

28 An Object-Oriented Approach to Programming Logic and Design 28 Creating Instance Methods in a Class (continued)

29 An Object-Oriented Approach to Programming Logic and Design 29 Creating Instance Methods in a Class (continued)

30 An Object-Oriented Approach to Programming Logic and Design 30 Exploring the Rationale Behind 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

31 An Object-Oriented Approach to Programming Logic and Design 31 Organizing 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

32 An Object-Oriented Approach to Programming Logic and Design 32 Organizing Classes (continued)

33 An Object-Oriented Approach to Programming Logic and Design 33 Understanding the Role of the this 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

34 An Object-Oriented Approach to Programming Logic and Design 34 Understanding the Role of the this Reference (continued)

35 An Object-Oriented Approach to Programming Logic and Design 35 An 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

36 An Object-Oriented Approach to Programming Logic and Design 36 An Introduction to Using Constructors (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

37 An Object-Oriented Approach to Programming Logic and Design 37 An Introduction to Using Constructors (continued)

38 An Object-Oriented Approach to Programming Logic and Design 38 Summary 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

39 An Object-Oriented Approach to Programming Logic and Design 39 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


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

Similar presentations


Ads by Google