Page 248 - IT-802_class_12
P. 248

avg= sum/2.0;

         System.out.println(“Sum “+ sum + “ and Average “+ avg);
         sum = n1+n2;
         avg= sum/2.0;
         System.out.println(“Sum “+ sum + “ and Average “+ avg);

         }
         }





























        In the above program, the sum and average marks of two students are to be calculated, separately for each of them. For
        this, the marks of the first student are input and their sum and average are found. Similarly, again the code is written
        to find the sum and average of marks of the second student. While coding this program, we clearly understand that
        we are writing similar statements twice. So, it would be wise if we have a technique that can remove this redundancy.
        Faced with such an issue, the developers found a solution and designed a ‘method’, a technique using which the
        redundant statements (i.e., the statements to be repeated) can be avoided and statements need to be written only
        once and be executed the required number of times, just by calling the method.
        So, they used the code in the following way.

        class sum_average
        {
          void calculate(int m, int n)
          {
            int sum = m + n;

            double average = sum/2.0;
            System.out.println(sum+ “ “ +average);
          }
          void main()
          {
           calculate(10,30);
           calculate(20,35);
          }

          246   Touchpad Information Technology-XII
   243   244   245   246   247   248   249   250   251   252   253