0% found this document useful (0 votes)
30 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)
30 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:

You might also like