Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Non-Boundary Elements (OOP)

Updated 21 Oct 2026

This version finds the non-boundary (inner) elements of an m x n matrix and their sum using a class: the order is passed to a parameterised constructor, and separate methods read the matrix, print it, test for inner elements, print them and return their sum.

Definition

An element a[i][j] of a matrix with m rows and n columns is a non-boundary (inner) element if it is not in the first or last row and not in the first or last column, that is, 1 <= i <= m - 2 and 1 <= j <= n - 2. For a $3 \times 5$ matrix with rows 1 2 3 4 5, 6 7 8 9 10, 1 1 1 1 1, the inner elements are 7 8 9 and their sum is 24.

If m or n is 2 there are no inner elements. The program then prints No non-boundary elements.

The order must lie between 2 and 10 for both rows and columns.

Algorithm

  1. In main, read r and c. If either is less than 2 or greater than 10, print Invalid order. Rows and columns must be between 2 and 10. and stop.
  2. Create the object ob with new NonBoundaryElements(r, c). The constructor stores r in m, c in n and creates the array a[m][n].
  3. readMatrix() reads the elements with two nested loops, and displayMatrix() prints them row by row.
  4. hasInner() returns true only when m > 2 && n > 2.
  5. If hasInner() is true:
    • showInner() prints the elements for i from 1 to m - 2 and j from 1 to n - 2, row by row.
    • innerSum() sets s to 0, adds the same elements and returns s; main prints it as Sum of non-boundary elements.
  6. Otherwise main prints No non-boundary elements.

Program

The constructor sets up the object, readMatrix and displayMatrix handle input and output, hasInner decides whether any inner element exists, showInner prints them and innerSum returns their sum.

import java.util.Scanner;
/* Class NonBoundaryElements stores an m x n matrix and finds its
   non-boundary (inner) elements and their sum. */
public class NonBoundaryElements
{
    int m;          // number of rows of the matrix
    int n;          // number of columns of the matrix
    int a[][];      // stores the elements of the matrix

    /* Parameterised constructor: stores the order and creates the array.
       Parameters: r = number of rows, c = number of columns. */
    NonBoundaryElements(int r, int c)
    {
        m = r;
        n = c;
        a = new int[m][n];
    }

    /* readMatrix(): reads the elements of the matrix. Returns nothing. */
    void readMatrix()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the elements:");
        for (int i = 0; i < m; i++)         // i moves over the rows
        {
            for (int j = 0; j < n; j++)     // j moves over the columns
            {
                a[i][j] = sc.nextInt();
            }
        }
    }

    /* displayMatrix(): prints the matrix row by row. Returns nothing. */
    void displayMatrix()
    {
        System.out.println("The matrix is:");
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();           // new line after each row
        }
    }

    /* hasInner(): checks whether the matrix has any non-boundary element.
       No parameters. Returns true when both m and n are greater than 2. */
    boolean hasInner()
    {
        return (m > 2 && n > 2);
    }

    /* showInner(): prints the non-boundary elements (rows 1 to m-2,
       columns 1 to n-2) row by row, left to right. Returns nothing. */
    void showInner()
    {
        System.out.println("Non-boundary elements:");
        for (int i = 1; i <= m - 2; i++)            // skip first and last row
        {
            for (int j = 1; j <= n - 2; j++)        // skip first and last column
            {
                System.out.print(a[i][j] + " ");
            }
        }
        System.out.println();
    }

    /* innerSum(): adds all non-boundary elements.
       Returns the sum as an int. */
    int innerSum()
    {
        int s = 0;                          // running sum, starts at 0
        for (int i = 1; i <= m - 2; i++)
        {
            for (int j = 1; j <= n - 2; j++)
            {
                s = s + a[i][j];
            }
        }
        return s;
    }

    /* main(): reads the order, checks it, creates the object and calls the methods. */
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows and columns (2 to 10): ");
        int r = sc.nextInt();
        int c = sc.nextInt();
        if (r < 2 || r > 10 || c < 2 || c > 10)     // range check on the order
        {
            System.out.println("Invalid order. Rows and columns must be between 2 and 10.");
        }
        else
        {
            NonBoundaryElements ob = new NonBoundaryElements(r, c);
            ob.readMatrix();
            ob.displayMatrix();
            if (ob.hasInner())
            {
                ob.showInner();
                System.out.println("Sum of non-boundary elements = " + ob.innerSum());
            }
            else
            {
                System.out.println("No non-boundary elements");
            }
        }
    }
}

Variable description table

VariableData TypePurpose
mintData member: number of rows of the matrix
nintData member: number of columns of the matrix
aint[][]Data member: stores the elements of the matrix
rintConstructor parameter and local in main: number of rows
cintConstructor parameter and local in main: number of columns
scScannerLocal in readMatrix and in main: reads input from the keyboard
iintLoop counter for the rows
jintLoop counter for the columns
sintLocal in innerSum: running sum of the inner elements
obNonBoundaryElementsObject created in main to call the methods
argsString[]Command line arguments of main; not used

Sample input and output

Sample 1

Enter the number of rows and columns (2 to 10): 3 5
Enter the elements:
1 2 3 4 5
6 7 8 9 10
1 1 1 1 1
The matrix is:
1	2	3	4	5	
6	7	8	9	10	
1	1	1	1	1	
Non-boundary elements:
7 8 9 
Sum of non-boundary elements = 24

Sample 2

Enter the number of rows and columns (2 to 10): 4 4
Enter the elements:
5 -3 8 2
7 1 9 4
6 0 -2 3
10 5 1 -6
The matrix is:
5	-3	8	2	
7	1	9	4	
6	0	-2	3	
10	5	1	-6	
Non-boundary elements:
1 9 0 -2 
Sum of non-boundary elements = 8

Sample 3

Enter the number of rows and columns (2 to 10): 4 2
Enter the elements:
1 2
3 4
5 6
7 8
The matrix is:
1	2	
3	4	
5	6	
7	8	
No non-boundary elements

Sample 4

Enter the number of rows and columns (2 to 10): 1 5
Invalid order. Rows and columns must be between 2 and 10.

One value per line

Dry run

Take the first sample: $m = 3$, $n = 5$ with rows 1 2 3 4 5, 6 7 8 9 10, 1 1 1 1 1. hasInner() is true because $3 > 2$ and $5 > 2$. In innerSum(), i runs from 1 to 1 and j from 1 to 3, and s starts at 0.

ija[i][j]s
1177
12815
13924

The method returns 24, and the program prints Sum of non-boundary elements = 24, the same as the first sample run.

Exam tip: Call hasInner() before showInner() and innerSum(), so that a matrix with 2 rows or 2 columns gets a clear message instead of an empty line.

Variations

  1. Boundary elements "Accept a matrix of order m x n and display the boundary elements, with their sum."
  2. Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
  3. Inner display "Display a square matrix with only the inner elements shown and the boundary positions left blank."