0% found this document useful (0 votes)
7 views4 pages

Java Array and Matrix Operations

The document presents two Java programs: one for sorting a one-dimensional array using the Bubble sort algorithm and another for checking if a 2D square matrix is symmetric. The first program allows user input for 10 integers, sorts them in both ascending and descending order, and displays the results. The second program takes the size of a square matrix as input, fills it with user-provided integers, and checks for symmetry, providing appropriate output based on the matrix's properties.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Java Array and Matrix Operations

The document presents two Java programs: one for sorting a one-dimensional array using the Bubble sort algorithm and another for checking if a 2D square matrix is symmetric. The first program allows user input for 10 integers, sorts them in both ascending and descending order, and displays the results. The second program takes the size of a square matrix as input, fills it with user-provided integers, and checks for symmetry, providing appropriate output based on the matrix's properties.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name: Ashley Rodrigues Class: SE CMPN-C

Subject: OOPM Roll no: 21

a) Problem Statement: Write a program to demonstrate arrays as an object and perform sorting of a
one – dimensional array using Bubble sort algorithm.

import [Link];

class ArrayDemo1 {
private int[] arr;
private Scanner sc;

public ArrayDemo1() {
arr = new int[10];
sc = new Scanner([Link]);
}

public void input() {


[Link]("Enter 10 integers:");
for (int i = 0; i < [Link]; i++) {
[Link]("Element " + (i + 1) + ": ");
arr[i] = [Link]();
}
}

public void display() {


[Link]("Array elements: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
}

private void swap(int i, int j) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public void sort_asc() {


for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
Name: Ashley Rodrigues Class: SE CMPN-C
Subject: OOPM Roll no: 21

swap(j, j + 1);
}
}
}
[Link]("Array sorted in ascending order.");
}

public void sort_desc() {


for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
swap(j, j + 1);
}
}
}
[Link]("Array sorted in descending order.");
}
}

public class Main {


public static void main(String[] args) {
ArrayDemo1 array = new ArrayDemo1();
[Link]();
[Link]("\nOriginal array:");
[Link]();
array.sort_asc();
[Link]();
array.sort_desc();
[Link]();
}
}
Output:
Name: Ashley Rodrigues Class: SE CMPN-C
Subject: OOPM Roll no: 21

Problem Statement: Write a program to check if a 2D square matrix is symmetric.

import [Link];

class MatrixSymmetryCheck {
private int[][] matrix;
private int size;
private Scanner sc;

public MatrixSymmetryCheck(int n) {
size = n;
matrix = new int[size][size];
sc = new Scanner([Link]);
}

public void input() {


[Link]("Enter the elements of the " + size + "x" + size + " matrix:");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
[Link]("matrix[" + i + "][" + j + "]: ");
matrix[i][j] = [Link]();
}
}
}

public void display() {


[Link]("The matrix is:");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
[Link]("%4d ", matrix[i][j]);
}
[Link]();
}
}

public void check_symm() {


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] != matrix[j][i]) {
[Link]("The matrix is NOT symmetric.");
return;
}
Name: Ashley Rodrigues Class: SE CMPN-C
Subject: OOPM Roll no: 21

}
}
[Link]("The matrix is symmetric.");
}
}

public class MatrixSymmetryCheckMain {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the size of the square matrix: ");


int n = [Link]();

if (n <= 0) {
[Link]("Size must be positive.");
return;
}
MatrixSymmetryCheck mat = new MatrixSymmetryCheck(n);
[Link]();
[Link]();
[Link]();
[Link]();
mat.check_symm();
[Link]();
}
}

Output:

Common questions

Powered by AI

To handle non-integer elements, the matrix could be defined as a matrix of doubles or of a comparable wrapper class like Double. Modifications would involve changing data types in method definitions and adjusting input, display, and symmetry check logic to accommodate floating-point numbers. This change increases the program's versatility but requires robust handling of floating-point precision issues and comparisons .

In ArrayDemo1, user input allows for dynamic creation and sorting of integer arrays by prompting the user to enter ten integers, affecting the array's final order after sorting operations . In MatrixSymmetryCheck, user input is crucial for defining the matrix's size and elements, directly impacting the symmetry check results. Incorrect input can lead to incorrect program behavior .

Both programs utilize class structures for encapsulation, input and display methods to manage I/O operations, and procedural methods to perform core functionalities, such as sorting and symmetry checking. They also employ constructor methods for initialization, enhancing reusability by clearly defining object properties and behaviors .

Modularization organizes code into distinct sections, each with specific responsibilities, enhancing readability and maintenance. In ArrayDemo1 and MatrixSymmetryCheck, modular design through methods like input, display, and sort fosters maintainable code, eases debugging, and supports scalability by allowing enhancements or changes to be made to individual sections without affecting the entire program .

The Scanner object facilitates user interaction by providing a simple mechanism to read input from standard input streams, enabling dynamic data entry for arrays and matrices. However, it is susceptible to input mismatch exceptions if invalid types are entered, and may lead to resource leaks if not closed properly after use, affecting program reliability .

The key features of the Java program for Bubble sort include an array of integers initialized in a constructor, an input method to fill the array, a display method to print array elements, and two sorting methods for ascending and descending order. Sorting is achieved by iterating through the array and swapping adjacent elements if they are out of order. A private method 'swap' is used for swapping .

Input validation can be improved by adding checks to ensure that the input is numeric and within a reasonable range, incorporating error handling for invalid inputs, and using loop constructs to prompt users again in cases of invalid input. Additionally, using try-catch blocks can prevent runtime errors when reading non-integer inputs .

Validating the matrix size ensures that the program operates correctly and efficiently. A non-positive matrix size is logically inconsistent with the concept of a square matrix. Thus, validation prevents runtime errors and invalid matrix configurations .

The program checks for symmetry by iterating over the matrix and comparing each element with its transposed counterpart. If all elements at position [i][j] match elements at [j][i], the matrix is symmetric. Otherwise, the program outputs that the matrix is not symmetric .

Bubble sort is a simple but inefficient sorting algorithm, particularly for large datasets. Its average and worst-case time complexity is O(n²), which can hinder performance due to repetitive adjacent element comparisons and swaps . The program's efficacy is limited as it does not take advantage of more efficient sorting strategies like QuickSort or MergeSort, which have better average time complexities.

You might also like