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
- In
main, readrandc. If either is less than 2 or greater than 10, printInvalid order. Rows and columns must be between 2 and 10.and stop. - Create the object
obwithnew BoundaryElements(r, c). The constructor storesrinm,cinnand creates the arraya[m][n]. readMatrix()reads the elements with two nested loops.displayMatrix()prints the matrix row by row.showBoundary()visits the elements row by row, left to right, and printsa[i][j]whenisBoundary(i, j)is true.isBoundary(i, j)returnstruewhen the position is in the first or last row or the first or last column.boundarySum()setssto 0, addsa[i][j]for every position whereisBoundary(i, j)is true, and returnss.mainprints the returned value asSum 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
| Variable | Data Type | Purpose |
|---|---|---|
| m | int | Data member: number of rows of the matrix |
| n | int | Data member: number of columns of the matrix |
| a | int[][] | Data member: stores the elements of the matrix |
| r | int | Constructor parameter and local in main: number of rows |
| c | int | Constructor parameter and local in main: number of columns |
| sc | Scanner | Local in readMatrix and in main: reads input from the keyboard |
| i | int | Loop counter for the rows; also the row parameter of isBoundary |
| j | int | Loop counter for the columns; also the column parameter of isBoundary |
| s | int | Local in boundarySum: running sum of the boundary elements |
| ob | BoundaryElements | Object created in main to call the methods |
| args | String[] | 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 = 65Sample 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 = 55Sample 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 = 42Dry 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.
| i | Elements added (j = 0 to 3) | Reason | s |
|---|---|---|---|
| 0 | 5, -3, 8, 2 | First row: all four | 12 |
| 1 | 7, 4 | Only j == 0 and j == 3; 1 and 9 are inner | 23 |
| 2 | 6, 3 | Only j == 0 and j == 3; 0 and -2 are inner | 32 |
| 3 | 10, 5, 1, -6 | Last row: all four | 42 |
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
- Non-boundary elements "Accept a matrix of order m x n and display the elements that are not on the boundary, with their sum."
- Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
- Border display "Display a square matrix with only the boundary elements shown and the inner positions left blank."