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
- 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 NonBoundaryElements(r, c). The constructor storesrinm,cinnand creates the arraya[m][n]. readMatrix()reads the elements with two nested loops, anddisplayMatrix()prints them row by row.hasInner()returnstrueonly whenm > 2 && n > 2.- If
hasInner()is true:showInner()prints the elements forifrom 1 tom - 2andjfrom 1 ton - 2, row by row.innerSum()setssto 0, adds the same elements and returnss;mainprints it asSum of non-boundary elements.
- Otherwise
mainprintsNo 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
| 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 |
| j | int | Loop counter for the columns |
| s | int | Local in innerSum: running sum of the inner elements |
| ob | NonBoundaryElements | 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 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 = 24Sample 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 = 8Sample 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 elementsSample 4
Enter the number of rows and columns (2 to 10): 1 5
Invalid order. Rows and columns must be between 2 and 10.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.
| i | j | a[i][j] | s |
|---|---|---|---|
| 1 | 1 | 7 | 7 |
| 1 | 2 | 8 | 15 |
| 1 | 3 | 9 | 24 |
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
- Boundary elements "Accept a matrix of order m x n and display the boundary elements, with their sum."
- Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
- Inner display "Display a square matrix with only the inner elements shown and the boundary positions left blank."