Page 221 - Cs_withBlue_J_C11_Flipbook
P. 221

•  The return type of the value returned using the return statement will be the same as the data type of the value
                   entered.
                 •  If the method does not return any value, then the data type void is written.

                 9.3.5 Method Name
                 Every method should have a name, by which it can be called from the same class or different classes.


                 Example:                               public int twice (int i)

                 9.3.6 Parameter list
                 It is a list of the variables which is sent to the method for execution. When the function is called, the values are sent
                 from the calling program to the method in the same order of data types as written within the pair of parentheses.

                 Example:                               public int twice (int i)

                 In some functions, there may not be any parameters at all. Such functions are said to have an empty parameter list.

                 Example:                                 public int twice ()


                 9.3.7 Body of the Method
                 Each method is used to do certain tasks, which are a set of statements defined under the method header within a pair
                 of curly braces and is known as the body of the method.
                 Example:

                                                         public int twice (int i)
                                                          {
                                                             int tw=i*2;
                                                                                             Body of the method
                                                             return tw;
                                                          }

                 Let us take another example.

                  Program 2      Write a Java program using methods to print whether a number is an Armstrong number or not.
                                 Following are the specifications of the class to be used in the program.
                                 Class                           :   armstrong
                                 Data Members                    :   int n
                                 Member Methods
                                 void input()                    :   Inputs the number to be checked in n
                                 boolean check_armstrong()       :   Returns true if the number is an Armstrong else false
                                 void display()                  :   calls check_armstrong() and prints Armstrong or not
                                 An Armstrong number is a positive m-digit number that is equal to the sum of the mth
                                 powers of their digits. It is also known as the pluperfect number.
                                 For example,
                                              3
                                          3
                                      3
                                 153: 1  + 5  + 3  = 1 + 125 + 27 = 153
                                              3
                                          3
                                      3
                                 125: 1  + 2  + 5  = 1 + 8 + 125 = 134 (Not an Armstrong Number)
                                               4
                                           4
                                                   4
                                       4
                                 1634: 1  + 6  + 3  + 4  = 1 + 1296 + 81 + 256 = 1643
                   1      import java.util.*;
                   2      class armstrong
                                                                                                                       219
                                                                                              Methods and Constructors  219
   216   217   218   219   220   221   222   223   224   225   226