Download presentation
Presentation is loading. Please wait.
Published byDennis Whitehead Modified over 9 years ago
1
3. Methods
2
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)))))
3
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: 8@9. 1 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
4
Java Methods There 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
5
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 Methods
6
Writing Methods: A Larger Look A 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 an instance method visible to all nothing returned Method name for command line parameters public static void main (String[ ] argv)
7
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); }
8
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
9
Where do Methods Go? Methods (and variables) are contained in Classes, as either class or instance members. More on creating classes and objects shortly...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.