Modules (Methods) - PowerPoint PPT Presentation

About This Presentation
Title:

Modules (Methods)

Description:

Sometimes more than one method is required to do the same job. ... Initially this will appear to be a big change. It is a common feature in many languages. ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 12
Provided by: BillL161
Category:
Tags: about | big | changes | methods | modules | the

less

Transcript and Presenter's Notes

Title: Modules (Methods)


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 (ltargumentsgt)
  • Java
  • System.out.print(ltA Stringgt)
  • System.out.println(ltA Stringgt)

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
11
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com