Page 257 - ComputerScience_Class_11
P. 257

Let us see the following example.

                  Program 7      Write a program that takes marks as an array entered by the user and increase them by 5.

                   1      class ImpureMethod
                   2      {

                   3          void increase_marks(int m1[])
                   4          {
                   5              int i;
                   6              for(i = 0; i < m1.length; i++)

                   7              {
                   8                  m1[i] = m1[i] + 5;
                   9              }
                  10          }
                  11          public static void main(String[] args)
                  12          {

                  13              int i;
                  14              int marks[] = {45, 98, 98, 56, 34};
                  15              ImpureMethod obj = new ImpureMethod();
                  16              System.out.print("Marks before increase: \n");

                  17              for(i = 0; i < marks.length; i++)
                  18              {
                  19                  System.out.print(marks[i] + " ");
                  20              }
                  21              obj.increase_marks(marks);

                  22              System.out.println("\nMarks increased: ");
                  23              for(i = 0; i < marks.length; i++)
                  24              {
                  25                  System.out.print(marks[i] + " ");
                  26              }

                  27          }
                  28      }

                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java
                   Options


                  Marks before increase:
                  45 98 98 56 34
                  Marks increased:
                  50 103 103 61 39





                                                                                           Methods and Constructors  255
   252   253   254   255   256   257   258   259   260   261   262