1 / 11

Modules (Methods)

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)

pwomack
Download Presentation

Modules (Methods)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modules (Methods) Functions and Procedures Parameters ...Output

  2. 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

  3. 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…)

  4. 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

  5. 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)

  6. 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); }

  7. 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

  8. 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]

  9. 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

  10. 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

More Related