0% found this document useful (0 votes)
7 views

Computer Project

The code defines a BoundarySum class to calculate the sum of elements on the boundary of a 2D array. The class takes row and column sizes as arguments, reads elements into an array from user input, and returns the sum of elements on the boundary in the sum() and display() methods.

Uploaded by

Sarnava Debnath
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Computer Project

The code defines a BoundarySum class to calculate the sum of elements on the boundary of a 2D array. The class takes row and column sizes as arguments, reads elements into an array from user input, and returns the sum of elements on the boundary in the sum() and display() methods.

Uploaded by

Sarnava Debnath
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

import java.util.

Scanner;

class DIasum {

        int a[][], r, c;

        DIasum(int r, int c) {

                r = r;

                c = c;

                a = new int[r][c];

    }

        void read() {

                Scanner scanner = new Scanner(System.in);

                System.out.println("Enter elements of the 2D array:");

                for (int i = 0; i < r; i++) {

                        for (int j = 0; j < c; j++) {

                                a[i][j] = scanner.nextInt();

            }

        }

    }

        int sum() {

                int sum = 0;

                for (int i = 0; i < r; i++) {

                        for (int j = 0; j < c; j++) {


                                if (i == j || i + j == r - 1) {

                                        sum += a[i][j];

                }

            }

        }

                return sum;

    }

        void display() {

                System.out.println("Sum of the first and last diagonals: " + sum());

    }

public class Main {

        public static void main(String[] args) {

                Scanner scanner = new Scanner(System.in);

        

                System.out.print("Enter the number of rows: ");

                int rows = scanner.nextInt();

        

                System.out.print("Enter the number of columns: ");

                int cols = scanner.nextInt();

        

                DIasum obj = new DIasum(rows, cols);

                obj.read();
                obj.display();

    }

Variable Name Data Type Used in Methods Description


a int[][] read(), sum() 2D array to store elements

r int DIasum(), read() Number of rows

c int DIasum(), read() Number of columns

scanner Scanner read() Scanner object to read user input

rows int main(), main() User input for the number of rows

cols int main(), main() User input for the number of columns

obj DIasum main() Object of the DIasum class

sum int sum(), display() To store the sum of diagonals


import java.util.Scanner;

class BoundarySum {

        int a[][], r, c;

        BoundarySum(int r, int c) {

                this.r = r;

                this.c = c;

                a = new int[r][c];

    }

        void read() {

                Scanner scanner = new Scanner(System.in);

                System.out.println("Enter elements of the 2D array:");

                for (int i = 0; i < r; i++) {

                        for (int j = 0; j < c; j++) {

                                a[i][j] = scanner.nextInt();

            }

        }

    }

        int sum() {

                int sum = 0;

                for (int i = 0; i < r; i++) {


                        for (int j = 0; j < c; j++) {

                                if (i == 0 || i == r - 1 || j == 0 || j == c - 1) {

                                        sum += a[i][j];

                }

            }

        }

                return sum;

    }

        void display() {

                System.out.println("Sum of boundary elements: " + sum());

    }

public class Main {

        public static void main(String[] args) {

                Scanner scanner = new Scanner(System.in);

        

                System.out.print("Enter the number of rows: ");

                int rows = scanner.nextInt();

        

                System.out.print("Enter the number of columns: ");

                int cols = scanner.nextInt();

        

                BoundarySum obj = new BoundarySum(rows, cols);


                obj.read();

                obj.display();

    }

Certainly! Here's the variable listing table for the modified code:

Variable Name Data Type Used in Methods Description

a int[][] read(), sum() 2D array to store elements

r int BoundarySum(), read() Number of rows

c int BoundarySum(), read() Number of columns

scanner Scanner read() Scanner object to read user input

rows int main(), main() User input for the number of rows

cols int main(), main() User input for the number of columns
obj BoundarySum main() Object of the BoundarySum class

You might also like