110 likes | 123 Views
Modules (Methods). Functions and Procedures Parameters ...Output. Procedures and Functions. Some of our pseudocode rules translate into firm rules in Java: Functions return a single value Value returned can be any complex data type (including an Object)
E N D
Modules (Methods) Functions and Procedures Parameters ...Output
Procedures and Functions • Some of our pseudocode rules translate into firm rules in Java: • Functions return a single value • Value returned can be any complex data type (including an Object) • Procedures can “return” information by changing an object that is passed in via a parameter. • Other rules are style issues: • Functions having no side effects • Functions not doing I/O
Java Methods • Single construct for both procedures and functions: • When a function is called for, specify the appropriate return type before the method name • public float average (float fNum1, float fNum2) • { • float fReturnVal; • fReturnVal = (fNum1 + fNum2)/ 2; • return fReturnVal; • } // of average • to specify a procedure, make the return type void • (More later…)
Writing Methods A Java requirement: --All methods belong to an object (or class). --Name of object (or class) must be unambiguous when the 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: for command line parameters visible to all nothingreturned public static void main (String[ ] argv) a class method,not aninstancemethod Method name
Method Overloading • Sometimes more than one method is required to do the same job. • In Pseudocode, we frequently used a helper module Procedure Convert(Lhead iot in Ptr toa LLNode) Procedure ConvertHelper(Lhead iot Ptr toa LLNode, NewHead iot Ptr toa LLNode) • In Java, multiple methods can have the same name as long as the number, type or order of their parameters is different public void Convert(LLNode Lhead) public void Convert(LLNode Lhead, LLNode NewHead)
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 different parameter (number, type or order NOT name!) 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); }
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 -- results in unhelpful error message
Parameters • Java only has in parameters • Initially this will appear to be a big change. It is a common feature in many languages. • We can get information out of a module via two techniques • Return something from a function • Pass in a reference to some object and inside the method modify the object [side effect]
Printing to Screen • Pseudocode: print (<arguments>) • Java: System.out.print(<A String>); System.out.println(<A String>); Inserts a newline after the printing is complete
Printing to Screen int x = 10; System.out.println(5); System.out.println( ); System.out.println(“Hello World”); System.out.println(“x = “ + x); System.out.println(“y = “ + 5); System.out.print(“All on one “); System.out.println(“line”); 5 Hello World x = 10 y = 5 All on one line