Chapter-5-JAVA PROGRAMMING USING GUI-III

Embed Size (px)

Citation preview

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    1/27

    CREATED BY: PRIYANKA PAREEK

    1Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    2/27

    INTRODUCTION

    Program: is a set of instructions given tocomputer. These instructions initiate some

    action and sometimes called as executable

    instructions. In Java programs instructions are specified

    through Methods or functions.

    Methods or Function is a sequence of somedeclaration statements and executable

    statements.

    2Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    3/27

    Amethod is a separatepiece of codethat

    can be called by a main program or anyother method to perform some specific

    function.

    The following are characteristics of methods:

    It can return one or no values

    It may accept as many parameters it needs or no

    parameter at all. Parameters are also called

    function arguments. After the method has finished execution, it goes

    back to the method that called it.

    3Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    4/27

    WHY METHODS?

    To Cope With Complexity

    Hiding Details

    Reuse

    4Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    5/27

    METHOD/ FUNCTION DEFINITION

    A method or a function is a sequence of

    statements that carry out specific task(s)

    Amethod or function must be defined before itis used anywhere in the program:

    [access- specifier ] [modifier] return-type

    method name (parameter list)

    {

    body of the method

    }

    5Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    6/27

    Where

    access specifier : can be either public or

    protected or private. Default is friendly modifier: can be one of: final, native,

    synchronized, transient, volatile.

    return-type: specifies the type of value that thereturn statement of the method returns. If no

    value being returned, it should be void.

    Method-name: valid java identifier.

    Parameter list: it is a comma-separated list of

    variables of a method referred to as its arguments

    or parameters.1/19/2011 Created By: PRIYANKA PAREEK 6

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    7/27

    METHOD PROTOTYPE AND SIGNATURE:

    AMethod Prototypeis the first line of the

    method definition that tells the program about the

    type of the value returned by the method and the

    number and type of arguments.

    Method Signature: it basically refers to the

    number and types of arguments. It is a part of the

    method prototype.

    1/19/2011 Created By: PRIYANKA PAREEK 7

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    8/27

    USE OF KEYWORD VOID

    Methods not returning in any value are declared void

    methods i.e., having void s their return type.

    syntax: void methods-name(parameter list);

    example:void setLabelText( )

    {

    jLabel.setText(### Thank U###);

    }

    1/19/2011 Created By: PRIYANKA PAREEK 8

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    9/27

    9

    Methods Methods in Java define the behavior of the class.

    Methods are similar to procedures or subroutines in otherlanguages.

    The real benefits of object orientation come from hiding theimplementation of a class behind its operations.

    Methods access the internal implementation details of a class

    that are hidden from other objects. Hiding data behind methods is so fundamental to object

    orientation it has a name - encapsulation.

    Methods have zero or more parameters.

    A method can have a return value.

    A method's statements appear in a block of curly braces { and }that follow the method's signature.

    public void speak () {

    System.out.println("Hey Barney ...");

    }Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    10/27

    10

    INVOKING A METHOD

    Provide an object reference and the method name,separated by a dot (.):

    fred.speak();

    Parameters are passed to methods as a comma-separated list.

    A method can return a single value as a result, suchas an int:

    public int add (int a, int b) {

    return a+b;

    }

    A method can return no result:public void setFirstName (String firstName) {

    this.firstName = firstName;

    }

    Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    11/27

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    12/27

    Example: to print cube of a given number using

    a method:

    private voidComputeActionPerformed(java.awt.event.ActionEvent

    evt) {

    double n =Double.parseDouble(jTextField1.getText());

    double res = CalcCube(n);

    jLabel3.setText("" +res); }

    double CalcCube(double b){

    double cu = b * b * b;

    return cu; }1/19/2011 Created By: PRIYANKA PAREEK 12

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    13/27

    ACTUAL AND FORMAL PARAMETERS

    Actual parameters: are

    the parameters

    appearing in the method

    call statement.

    Formal parameters:

    are the ones that appear

    in method definition.

    Example:

    Formal Parameters:

    int mult (int x, int y) {

    return x* y;

    }

    Actual parameters:

    int length = 10;

    int width =5;

    Int area = mult(length,

    width);1/19/2011Created By: PRIYANKA PAREEK

    13

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    14/27

    Arguments to Methods

    Arguments to methods can be:

    of primitive data types i.e. char, byte, short, int,

    long, float, double, boolean.

    of reference data types i.e. objects or arrays

    A method is invoked in two manners: Call by

    Value and Call by Reference. These two

    ways are also called as Pass By Value and Passby Reference.

    1/19/2011 Created By: PRIYANKA PAREEK 14

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    15/27

    Call by Value (or Pass By Value )

    When a pass-by-value occurs, the method makes a copy ofthe value of the variable passed to the method. The

    method cannot accidentally modify the original argument

    even if it modifies the parameters during calculations.

    In the given example, we called the method test and

    passed the value ofias parameter. The value of i is copied

    to the variable of the methodj. Sincej is the variable

    changed in the test method, it will not affect the variable

    value ifi in main since it is a different copy of the

    variable.

    15

    Created By: PRIYANKA PAREEK1/19/2011

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    16/27

    Call by Value (or Pass By Value )

    1/19/2011 Created By: PRIYANKA PAREEK 16

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    17/27

    Call by Reference(or Pass by Reference)

    When a pass-by-reference occurs, the reference toan object is passed to the calling method. This

    means that, the method makes a copy of the

    reference of the variable passed to the method.However, unlike in pass-by-value, the method

    can modify the actual object that the reference is

    pointing to, since, although different references

    are used in the methods, the location of the datathey are pointing to is the same.

    1/19/2011 Created By: PRIYANKA PAREEK 17

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    18/27

    Call by Reference(or Pass by Reference)

    1/19/2011 Created By: PRIYANKA PAREEK 18

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    19/27

    1/19/2011 Created By: PRIYANKA PAREEK 19

    Figure : Pass-by-referenceexample

    A common misconception about pass-by-reference in Java is when

    creating a swap method using Java references. Take note that

    Java manipulates objects 'by reference, but it passes object

    references to methods 'by value.'"As a result, you cannot write a

    standard swap method to swap objects.

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    20/27

    Returning from a method

    A method returns value through return statement. A method terminates when either a return

    statement is encountered or the last statement in

    the method is executed. Areturn statement is used to terminate a method

    whether or not it returns a value.

    1/19/2011 Created By: PRIYANKA PAREEK 20

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    21/27

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    22/27

    RETURNING VALUES

    They may of three types of methods in Java:Computational Methods: these are the methods

    that calculate or compute some value and returned

    the computed value. Example: Math.sqrt () and

    Math.cos( ) are computational methods.

    Computational methods always return a computed

    result.

    ManipulativeMethods: these are the methodsthat manipulate information and return a success or

    failure code.

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    23/27

    Procedural methods: these are the methods that

    perform an action and have no explicit returnvalue. Example: System.out.println() method.

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    24/27

    SCOPE REVISITED

    The scopedetermines where in the programthe variable is accessible. The scope also

    determines the lifetime of a variable or how

    long the variable can exist in memory. The

    scope is determined by where the variable

    declaration is placed in the program.

    Def: The program part(s) in which a particular

    piece of code or a data value(e.g.. Variable)

    can be accessed is known as the piece-of-

    codes or variables Scope.

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    25/27

    CONSTRUCTORS C

    onstructors: a member method with thesame name as its class is called Constructor

    and it is used to initialize the objects of that

    class type with a legal initial value.

    Example:

    class A( ){

    int a;

    float b;public A( ){

    a = 0;

    b = 0.0; } }

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    26/27

  • 8/8/2019 Chapter-5-JAVA PROGRAMMING USING GUI-III

    27/27