Categories
Class 11

Class 11 NonBound Program ISC 2020

A class NonBound contains a square matrix which finds the sum of the non-boundary elements of the matrix.

Some of the members of the class are given below:
Class name: NonBound
Data members/instance variables:
arr[][]: array to store integer elements.
n: to store the order of the matrix.
Member functions/methods:
NonBound(int mm): parameterized constructor to initialize the data member n = mm.
void fillArray(): to fill the array elements.
void sumOfNon(): finds and displays the sum of the non-boundary elements in the matrix with an appropriate message.
void display(): displays the array elements in matrix form.

Define the class NonBound giving details of the constructor, void fillArray(), void sumOfNon() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.io.*;
class NonBound{
    int arr[][];
    int n;
    public NonBound(int mm){
        n = mm;
        arr = new int[n][n];
    }
    public void fillArray()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter array elements:");
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                arr[i][j] = Integer.parseInt(br.readLine());
            }
        }
    }
    public void sumOfNon(){
        int sum = 0;
        for(int i = 1; i < n - 1; i++){
            for(int j = 1; j < n - 1; j++){
                sum += arr[i][j];
            }
        }
        System.out.println("Sum of non-boundary elements: " + sum);
    }
    public void display(){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter order of the matrix: ");
        int size = Integer.parseInt(br.readLine());
        NonBound obj = new NonBound(size);
        obj.fillArray();
        System.out.println("Given matrix:");
        obj.display();
        obj.sumOfNon();
    }
}

By admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website happycompiler.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.