Page 592 - ComputerScience_Class_11
P. 592

The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                enter the size of the array
                2
                enter elements in the 1st array
                12
                5
                enter elements in the 2nd array
                10
                2
                the sum of the 2 arrays is:
                22 7
                The sorted array
                7 22



                Program 14     Define a class sumofdiagonals and print the sum of diagonals of a matrix. The data members
                               and member methods are defined as follows:
                                Data Members
                                int n                          :  to store the size of the array
                                int a[][]                      :  to store the array
                                Member Methods
                                void printDiagonalSums()       :  to print the sum of the diagonals
                               Write the main method to call the methods and run the program.

                 1       import java.util.*;
                 2       class sumofdiagonals {

                 3           void printDiagonalSums()
                 4           {

                 5               int sum=0;
                 6               int pd = 0, sd = 0;
                 7               Scanner sc=new Scanner(System.in);

                 8               System.out.println("enter the size of the matrix");
                 9               int n=sc.nextInt();

                10               int a[][]=new int[n][n];//declaration of the array
                11               System.out.println("enter the matrix elements");

                12               for(int i=0;i<n;i++)
                13               {

                14                   for(int j=0;j<n;j++)
                15                   {

                16                       a[i][j]=sc.nextInt();





                  590  Touchpad Computer Science (Ver. 3.0)-XI
   587   588   589   590   591   592   593   594   595   596   597