Page 598 - ComputerScience_Class_11
P. 598

The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                enter the size of the matrix
                2
                enter array elements
                1
                2
                3
                4
                ORIGINAL MATRIX
                1 2
                3 4
                MIRROR MATRIX
                2 1
                4 3



                Program 17     Define a class Spiral_Matrix to input and print the spiral of a user-defined matrix (2-D array).
                               The data members and member methods are defined as follows:

                               Data Members
                               int A[][]                       :  to store the array
                               int n                           :  to store the size of the array
                               Member Methods
                               void input()                    :  to input the size of the array
                               void spiral()                   :  to find the spiral matrix and print it
                               Write the main method to call the methods and print the spiral of the matrix.

                 1       import java.util.*;
                 2       class Spiral_Matrix

                 3       {
                 4           int n;
                 5

                 6           void spiralprint(){
                 7               Scanner sc = new Scanner(System.in);
                 8               System.out.print("Enter the number of elements : ");
                 9               n = sc.nextInt();

                10
                11               int A[][] = new int[n][n];
                12               int k=1, c1=0, c2=n-1, r1=0, r2=n-1;

                13
                14               while(k<=n*n)
                15               {






                  596  Touchpad Computer Science (Ver. 3.0)-XI
   593   594   595   596   597   598   599   600   601   602   603