java methods part1

Upload: aladdin162

Post on 07-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Java Methods Part1

    1/15

    1

    Methods:Making Your Application Modular.

    Methods:Methods:Making Your Application Modular.Making Your Application Modular.

  • 8/4/2019 Java Methods Part1

    2/15

    2

    OverviewOverview

    In these chapters you will learn: What is a method? Declaring methods. Using (or calling) methods. Writing methods that take parameters. Writing methods that return values Declaring variables in the method.

    Scope of variables. Method overloading

  • 8/4/2019 Java Methods Part1

    3/15

    3

    What is a method?What is a method?What is a method?

  • 8/4/2019 Java Methods Part1

    4/15

    4

    Java MethodsJava Methods

    In Java, a methodis a block of statements that has aname and can be executed by callingit from someother place in your program.

    You have already been using methods. For example:

    to print text to the console, you use the println or printmethods.

    to get an integer from the user, you use the nextIntmethod.

    and the granddaddy of all methods,main.

    With the exception ofmain, all the methods youve

    used so far have been methods that are defined bythe Java API and that belong to a particular Javaclass.

    Themain method belongs to the class defined by

    your application.

  • 8/4/2019 Java Methods Part1

    5/15

    5

    Why Do We Need Methods?Why Do We Need Methods?

    Modular programming: breaking a programup into smaller, manageable modules or

    methods. Motivation for modular programming:

    Simplifies the process of writing programs

    Enables a divide-and-conquer approach

    Improves maintainability of programs

    Prevents repeating code within the same

    program.

    Promotes code-reusability in later programs

  • 8/4/2019 Java Methods Part1

    6/15

    7

    Modular DesignModular Design

    public class MyClass

    { public static void main(String[] args){

    statement;statement;

    statement;statement;statement;statement;

    statement;statement;

    statement;

    statement;statement;

    }

    }

    Here is an example of non-modular design

  • 8/4/2019 Java Methods Part1

    7/15

    8

    Modular DesignModular Design

    public class MyClass{

    public static void main(String[] args){

    statement;statement;

    public static void doTask1( ){

    }

    public static void doTask2( ){

    }

    statement;statement;statement;statement;

    statement;statement;statement;

    doTask1();

    doTask2();

    statement;statement;

    }}

    This is an example of a modular design usingmethods

  • 8/4/2019 Java Methods Part1

    8/15

    9

    The Basics of Making a MethodThe Basics of Making a Method

    public static return-type method-name( parameter-list ){

    statements...

    }

    All methods including the main method mustbegin with a method declaration.

    Heres the basic form for a method declaration,

    at least for the types of methods we have seen sofar:

    This method can

    be seen byother classes.

    This method can exist even if no

    objects are created from thisclass. Will explain this more later.

    The data type of thevalue returned by the

    method when it endsThe name of

    this method.

    Lists what can be

    passed to the method

    .

    The body of the

    method

  • 8/4/2019 Java Methods Part1

    9/15

    10

    Before We Get DeeperBefore We Get Deeper

    To promote software reusability:

    every method should be limited to performing a single,

    well-defined task.

    the name of the method should express that task

    effectively.

    A small method that performs one task is easier to

    test and debug than a larger method that performsmany tasks.

    If you cannot choose a concise name that

    expresses a methods task, your method might beattempting to perform too many diverse tasks. It isusually best to break such a method into severalsmaller method declarations.

  • 8/4/2019 Java Methods Part1

    10/15

    12

    Declaring Methods in Your ClassDeclaring Methods in Your Class

    public class MyClass{

    public static void main(String[] args){

    statement;statement;

    }

    //----------------------------------------------public static int doSomething( int value)

    {statement;statement;

    }

    //----------------------------------------------

    public static double dosomethingElse( double num1){statement;statement;

    }}

  • 8/4/2019 Java Methods Part1

    11/15

    13

    Using Your Methods: Calling a MethodUsing Your Methods: Calling a MethodUsing Your Methods: Calling a Method

  • 8/4/2019 Java Methods Part1

    12/15

    14

    Calling a MethodCalling a Method

    To call (or execute) a method, use themethod name followed by () and ;

    printMsg(); // doesnt take parameters

    When called, program executes the body ofthe called method.

    After the method terminates, executionresumes in the calling method (for example

    the main) at the point of call. The main can call any number of methods

    Methods can call other methods.

  • 8/4/2019 Java Methods Part1

    13/15

    15

    ExampleExample

    public class HelloWorldMethod{

    public static void main(String[] args){

    sayHello();}

    //-------------------------------------public static void sayHello(){

    System.out.println("Hello, World!");}

    }

    public class HelloWorldMethod{

    public static void sayHello(){

    System.out.println("Hello, World!");}//-------------------------------------public static void main(String[] args){

    sayHello();}

    }

    Execution

    starts at main

  • 8/4/2019 Java Methods Part1

    14/15

    16

    Methods that Take ParametersMethods that Take ParametersMethods that Take Parameters

  • 8/4/2019 Java Methods Part1

    15/15

    17

    Methods that Accept ParametersMethods that Accept Parameters

    A parameteris a value that you can pass to amethod.

    The method can then use the parameter as if it were alocal variable initialized with the value of the variablepassed to it by the method call.

    A method that accepts parameters must list the

    parameters in the method declaration.

    The parameters are listed in a parameter list thatsin the parentheses that follow the method name.

    For each parameter used by the method, you list theparameter type followed by the parameter name.

    If you need more than one parameter, you separate

    them with commas.