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

ISC Practicals and Theory - Boundary Elements (OOP)

Updated 21 Oct 2026

This version finds the boundary 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, print the boundary elements and return their sum.

Definition

An element a[i][j] of a matrix with m rows and n columns is a boundary element if it is in the first row, the last row, the first column or the last column, that is, if i == 0 || i == m - 1 || j == 0 || j == n - 1. All other elements are inner (non-boundary) elements. For a $3 \times 4$ matrix with rows 1 2 3 4, 5 6 7 8, 9 10 11 12, the inner elements are 6 and 7, so the boundary sum is $78 - 13 = 65$.

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 BoundaryElements(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.
  4. displayMatrix() prints the matrix row by row.
  5. showBoundary() visits the elements row by row, left to right, and prints a[i][j] when isBoundary(i, j) is true.
  6. isBoundary(i, j) returns true when the position is in the first or last row or the first or last column.
  7. boundarySum() sets s to 0, adds a[i][j] for every position where isBoundary(i, j) is true, and returns s.
  8. main prints the returned value as Sum of boundary elements.

Program

The constructor sets up the object, readMatrix and displayMatrix handle input and output, isBoundary tests one position, showBoundary prints the boundary elements and boundarySum returns their sum.

import java.util.Scanner;
/* Class BoundaryElements stores an m x n matrix and finds the sum of
   its boundary elements (first row, last row, first column, last column). */
public class BoundaryElements
{
    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. */
    BoundaryElements(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
        }
    }

    /* isBoundary(): checks whether the position (i, j) is on the boundary.
       Parameters: i = row index, j = column index.
       Returns true if it is in the first/last row or first/last column. */
    boolean isBoundary(int i, int j)
    {
        return (i == 0 || i == m - 1 || j == 0 || j == n - 1);
    }

    /* showBoundary(): prints the boundary elements row by row, left to right.
       Returns nothing. */
    void showBoundary()
    {
        System.out.println("Boundary elements:");
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (isBoundary(i, j))
                {
                    System.out.print(a[i][j] + " ");
                }
            }
        }
        System.out.println();
    }

    /* boundarySum(): adds all boundary elements.
       Returns the sum as an int. */
    int boundarySum()
    {
        int s = 0;                          // running sum, starts at 0
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (isBoundary(i, 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
        {
            BoundaryElements ob = new BoundaryElements(r, c);
            ob.readMatrix();
            ob.displayMatrix();
            ob.showBoundary();
            System.out.println("Sum of boundary elements = " + ob.boundarySum());
        }
    }
}

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; also the row parameter of isBoundary
jintLoop counter for the columns; also the column parameter of isBoundary
sintLocal in boundarySum: running sum of the boundary elements
obBoundaryElementsObject 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 4
Enter the elements:
1 2 3 4
5 6 7 8
9 10 11 12
The matrix is:
1	2	3	4	
5	6	7	8	
9	10	11	12	
Boundary elements:
1 2 3 4 5 8 9 10 11 12 
Sum of boundary elements = 65

Sample 2

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

Sample 3

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

Sample 4

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	
Boundary elements:
5 -3 8 2 7 4 6 3 10 5 1 -6 
Sum of boundary elements = 42

One value per line

Dry run

Take the last sample: $m = 4$, $n = 4$ with rows 5 -3 8 2, 7 1 9 4, 6 0 -2 3, 10 5 1 -6. In boundarySum(), s starts at 0.

iElements added (j = 0 to 3)Reasons
05, -3, 8, 2First row: all four12
17, 4Only j == 0 and j == 3; 1 and 9 are inner23
26, 3Only j == 0 and j == 3; 0 and -2 are inner32
310, 5, 1, -6Last row: all four42

The method returns 42, and the program prints Sum of boundary elements = 42, the same as the last sample run.

Exam tip: Keep the boundary test in its own method isBoundary so that both the printing method and the sum method use the same condition.

Variations

  1. Non-boundary elements "Accept a matrix of order m x n and display the elements that are not on the boundary, with their sum."
  2. Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
  3. Border display "Display a square matrix with only the boundary elements shown and the inner positions left blank."