Method Statement For Hv Switchgear Installation

Method Statement For Hv Switchgear Installation – The return statement is used to exit the execution sequence of the statements of a method and, optionally, return a value. After the output of the method, the sequence of execution of the program is returned to the place of call of said method.

Syntax:

return expression;

Method Statement For Hv Switchgear Installation Download

Method Statement For Hv Switchgear Installation Download

 

8.1 DECLARATION AND USE OF METHODS
A method is a piece of code that can be called or invoked by the main program or by another method to perform some specific task. The term method in Java is equivalent to the subprogram, routine, subroutine, procedure or function in other programming languages. The method is called by its name or identifier followed by a sequence of parameters or arguments (data used by the method itself for its calculations) in parentheses. When the method finishes its operations, it usually returns a simple value to the program that calls it, which uses said value in the way that suits it. The data type returned by the return statement must match the data type declared in the method header.

Syntax for declaration of a method:

[ modifiers ] DataType identifierMethod ( formal parameters )  { 
    declarations of local variables ; 

    sentence_1 ; 

    sentence_2 ;
    ...

    sentence_n ; // at least one return is included within these statements 
}

he first line of code corresponds to the method header. The modifiers specify how the method can be called, the data type indicates the type of value that the method call returns, and the parameters (in parentheses) enter information for the execution of the method. If there are no explicit parameters, the parentheses are left empty. Then, the statements between braces make up the body of the method. Within the body of the method is located, at least, a sentence return.

A simple example

Below is an example of declaration and use of a method that returns the cube of a real numerical value with a statement return:

/ **

 * Demonstration of the cube method

 * / 
public  class Test Tube { public  static  void main ( String  [ ] args ) { 
        System . out . println ( "The cube of 7.5 is:"  + cube ( 7.5 ) ) ;  // call 
    }

 
    public  static  double cube ( double x )  { 
        // declaration 
        return x * x * x ; 
    } 
}

Unlike other programming languages, such as Pascal, in Java, the declaration of the method can be made in the source code after calling the method itself. In the previous case, publicy staticare the modifiers specified in the method header. The use of these two modifiers allows the type of method to be similar to that of a global function of Pascal or C. The identifier doublerefers to the type of data that the method call returns, it cubois the identifier of the method and it xis the identifier of the parameter in the declaration of the method header ( formal parameter ). Execution example of the previous code and corresponding output per screen:

$ > java TestCube
The cube of 7.5 is: 421,875

In Java, the methods are usually associated with the objects or instances in particular to which they operate ( instance methods ). Methods that do not need or work with objects (and yes with numbers, for example) are called static or class methods and are declared with the modifier static. The static or class methods are equivalent to the routines (functions or procedures) of languages ​​that do not use object-oriented programming. For example, sqrtthe class Mathmethod is a static method. So is the method in cubothe previous example. On the other hand, every Java program or application has a main method mainthat will always be a staticmethod .

Why is the word used staticfor class methods? The most common meaning or meaning of the static word ( which stands still in one place ) seems to have nothing to do with what staticmethods do . Java uses the word staticbecause C ++ uses it in the same context: to designate class methods. Taking advantage of its use in variables that have a single location in memory for different calls to methods, C ++ started using it in the designation of class methods to differentiate them from the instance methods and not to confuse the compiler. The problem is that nobody thought that the use of the word staticcould cause human confusion.


Method Statement For Hv Switchgear Installation

Method Statement For Hv Switchgear Installation

Method Statement For Hv Switchgear Installation

8.2 RETURN AND VOID
In some cases, it is not necessary for the static method to have to return a value at the end of its execution. In this case, the type of data that must be indicated in the declaration header of the method is the type voidand the statement is returnnot followed by any expression.

Syntax:

return ;

The following code includes an example of a method that does not return a value (of type void):

/ **

 * Demonstration of the table method
 * / 
public  class TestTable { 
    public  static  void main ( String  [ ] args ) { 
        table ( 4 ) ; 
        table ( 7 ) ; 
    }

 
    public  static  void table ( int n )  { 
        // call example 
        // of type void 
        System . out . println ( "Table of multiplication of the number"  + n ) ; 
        for  ( int i = 0 ; i <= 10 ; i ++ ) System . out . println ( n +  "x"  + i +  "="  + product( n, i ) ) ;
 
        return ;  // Does not return any value 
    }

 
    public  static  int product ( int a, int b )  { 
        return a * b ; 
    } 
}

If there is no return statement within a method, its execution continues until the end of the method is reached and then the execution sequence is returned to the place where the method was invoked.

A method whose return type is not void always needs to return something. If the code of a method contains several if statements, you must ensure that each of the possible options returns a value. Otherwise, a compilation error would be generated. For example:

/ **
 
 * Demonstration of the esPositive function 
 * / 
public  class TestPositive { public  static  void main ( String  [ ] args )  { 
        for  ( int i = 5 ; i > = - 5 ; i - ) System . out . println ( i +  "is positive:"  + esPositive ( i ) ) ; 
    } 
 
    public static  boolean esPositive ( int x )  { if  ( x < 0 )  return  false ; if  ( x > 0 )  return  true ; // Error: lost return if x equals zero. 
    } 
}

Example of attempt to compile the previous code:

$ > javac TestPositive.java 
testPositive.java: 14 : missing return statement
         } 
        ^

8.3 RESOURCES
Java allows recursion or recurrence in the programming of methods. Recursion consists of a method calling itself. A very typical example of the use of recursion can be seen in the construction of a method that returns the factorial of an integer. It is based on the fact that n! is equal to n · (n-1)! if n is greater than 1. For example:

/ ** 
 * Demonstration of the factorial recursive function 
 * / 
public  class TestFactorialR { 
    public  static  void main ( String  [ ] args ) { 
        for  ( int i = 1 ; i <= 20 ; i ++ ) 
            System . out . println ( "Factorial of"  + i +  "="  + factorialR ( i ) ) ;
        } 
    } 
 
    public  static  long factorialR ( int n )  { 
        if  ( n == 0 ) 
            return  1 ; 
        else 
            return n * factorialR ( n - 1 ) ; 
    } 
}

In the construction of recursive methods it is important to avoid the problem of infinite recursion. That is, in some cases, the execution of the recursively defined method does not imply a new call to the method itself.

8.4 OVERLOAD METHODS
Java allows assigning the same identifier to different methods, whose difference lies in the type or number of parameters they use. This is especially convenient when you want to carry out the same task in different numbers or types of variables. The overload (overloading) of the methods can be very useful when making calls to a method, since instead of having to remember identifiers of different methods, it is enough to remember only one. The compiler is responsible for finding out which of the methods that share identifier should execute. For example:

/ ** 
 * Demonstration of overloaded methods * A. Garcia-Beltran - March, 2002 
 * / 
public  class Overload Test { 
    public  static  void main ( String [ ] args )  { 
        int a = 34 ; 
        int b = 12 ; 
        int c = 56 ; 
 
        System . out . println ( "a ="  + a +  "; b ="  + b +  "; c =" + c ) ; 
        // The first 
        System method . out . println ( "The greater of a and b is:"  + greater ( a, b ) ) ; 
        // The second 
        System method . out . println ( "The greater of a, b and c is:"  + greater ( a, b, c ) ) ; 
    } 
 
    // Definition of greater than two integers 
    public  static  int major ( int x, int y )  {
        return x > y ? x : y ; 
    } 
 
    // Definition of greater than three integers 
    public  static  int greater ( int x, int y, int z )  {
         greater return ( greater ( x, y ) , z ) ; 
    } 
}

Example of output by screen:

$ > java TestSupercharge 
a = 34 ; b = 12 ; c = 56 
The greater of a and b is: 34 The greater of a, b and c is: 56

Gallery of Method Statement For Hv Switchgear Installation

( Click Image to Enlarge )

See More!