Page 239 - CA_Blue( J )_Class10
P. 239

Note:  Overloaded method that differs only by return type cannot be executed.
                              Overloaded method should be written within the same class.


                 10.9.2 Advantages of Method Overloading
                 •  Execution of a program becomes faster with the implementation of method overloading.
                 •  It helps in reusability of the program code. Thus reducing the memory requirement.
                                                                                        21 st
                  Some More Programs                                                 Century   #Coding & Computational Thinking
                                                                                       Skills
                               To input a number and use a function perfect() to check whether the number is a perfect number or
                  Program 1
                               not. The function returns true if the number is a perfect number, else it returns false.

                   1  import java.util.*;

                   2  class perfect_function
                   3  {

                   4      public static boolean perfect(int k)
                   5      {

                   6          int i,s=0;
                   7          for(i=1;i<k; i++)

                   8          {

                   9              if(k%i==0)
                  10                  s=s+i;
                  11          }

                  12          if(s==k)
                  13              return true;

                  14          else
                  15              return false;

                  16      }
                  17
                  18      public static void main()

                  19      {
                  20          Scanner sc= new Scanner(System.in);

                  21          int n;
                  22          boolean b;

                  23          System.out.println("Enter a number : ");
                  24          n=sc.nextInt();

                  25          b=perfect(n);
                  26          if(b)




                                                                                                                       237
                                                                                                 User-defined Methods   237
   234   235   236   237   238   239   240   241   242   243   244