3. methods. programming paradigms procedural programming ‘imperative’ assignment used to create...

9
3. 3. Methods

Upload: dennis-whitehead

Post on 17-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

3.3.

Methods

Page 2: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Programming Paradigms

Procedural Programming

‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly, Pascal int y; int x = 3; y = manipulateData(x);

Functional Programming

Functions (procedures that do not depend on outside data) are used to provided data. E.g., Lisp.

(defun check-member (input-item input-list) (cond ((null input-list) nil) ((equal input-item (first input-list)) T) (T (check-member input-item (rest input-list)))))

Page 3: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Object-Oriented Programming

All data exists in objects; interaction occurs only between objects. Even numbers are objects that know how to add themselves. E.g., SmallTalk:

| array |array := Array new: 5.rect := 0@0 corner: [email protected] to: array size do: [ :item | rect origin: item@item. array at: item put: rect copy ].

Java:

Object-oriented, but not 100% OO, since it contains primitives, and tolerates some (hopefully small) degree of procedural programming.

Programming Paradigms

There are other paradigms, but these three help explain where Java comes from

Page 4: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Java MethodsJava MethodsThere exists in Java a single construct, the method, for both procedures and functions:

• when a procedure is called for, specify the return type “void” before method name

public void printHelloWorld( ) {

System.out.println(“Hello World!”);

} // of printHelloWorld

• Note: All methods must have parentheses for parameters . . . even if no parameters!

Note the comment

Page 5: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Single construct for both procedures and functions:

• when a function is called for, specify the appropriate return type before method name

public float average (float fNum1, float fNum2, float fNum3) { float fReturnVal; fReturnVal = (fNum1 + fNum2 + fNum3)/ 3; return (fReturnVal); } // of average

• to make a procedure, one might create a method manipulating instance variables. (More on instance later…)

Java MethodsJava Methods

Page 6: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Writing Methods: A Larger LookWriting Methods: A Larger LookA Java requirement:

--All methods belong to an object (or class).--Name of object (or class) must be unambiguous when method called.--To run a program, there must be a class (whose name is the name-of-the-program), containing a special method called main:

a class method,

not aninstancemethod

visible to all

nothingreturned

Method name

for command line parameters

public static void main (String[ ] argv)

Page 7: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Method Signatures“The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile time error occurs.”

--Java Language Specification s.8.4.2

Method overloading occurs where identically named methods have subtle variations in the method parameters.

public int getCube(int iNum){return iNum*iNum*iNum;

}

public int getCube(float fNum){return (int)(fNum*fNum*fNum);

}

public int getCube(double dNum){return (int) (dNum*dNum*dNum);

}

Page 8: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Methods: Common Mistakes

public float average (float fNum1, float fNum2, float fNum3); { float fReturnVal; fReturnVal = (fNum1 + fNum2 + fNum3)/ 3; return (fReturnVal); } // of average

Note ending semicolon-- could be viewed as ‘abstract’ method

(more on abstract methods later)

-- results in unhelpful error message

Page 9: 3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,

Where do Methods Go?

Methods (and variables) are contained in Classes, as either class or instance members. More on creating classes and objects shortly . . .