0% found this document useful (0 votes)
30 views12 pages

Java Array and Matrix Operations

Uploaded by

ANJALI MODI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views12 pages

Java Array and Matrix Operations

Uploaded by

ANJALI MODI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/

******************************************************************************
******

Name- Anjali Modi

Batch- A1

Enrollement No.- 220146

Aim- Write a class to do the double of 1-D array and test it using main function.
******************************************************************************
******/

import [Link].*;
class double1Dmain
{
public static void main(String args[])
{
double1D t1= new double1D();
[Link]();
[Link]();
[Link]();
}
}
class double1D
{
int a[];
double1D()
{
a=new int[5];
}
void input()
{
[Link]("enter the array elements");
Scanner sc=new Scanner([Link]);
for(int i=0;i<5;i++)
{
a[i]=[Link]();
}
}
void output()
{
for(int i=0;i<5;i++)
{
[Link](a[i]);
}
}
void proc()
{
for(int i=0;i<5;i++)
{
a[i]=2*a[i];
}
}
}
/
******************************************************************************
******

Name- Anjali Modi

Batch- A1

Enrollement No.- 220146

Aim- Write a class to do the sum of 1-D array and using objects and classes.
******************************************************************************
******/

import [Link].*;

class sum1Dmain
{
public static void main(String args[])
{
sum1D t1= new sum1D();
[Link]();
[Link]();
[Link]();
}
}
class sum1D
{
int a[];
int sum=0;
sum1D()
{
a=new int[5];
}
void input()
{
[Link]("enter the array elements");
Scanner sc=new Scanner([Link]);
for(int i=0;i<5;i++)
{
a[i]=[Link]();
}
}
void output()
{
[Link](sum);
}
void proc()
{

for(int i=0;i<5;i++)
{
sum=sum+a[i];
}
}
}

/
******************************************************************************
******

Name- Anjali Modi

Batch- A1

Enrollement No.- 220146

Aim- Write a program to do the reverse of 1-D array using second array.
******************************************************************************
******/

class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int[] reversedArr = reverseArray(arr);

[Link]("Original array:");
printArray(arr);

[Link]("\nReversed array:");
printArray(reversedArr); }
public static int[] reverseArray(int[] arr) {
int n = [Link];
int[] reversedArr = new int[n];

// Loop through the original array and copy elements in reverse order
for (int i = 0; i < n; i++) {
reversedArr[i] = arr[n - i - 1];}

return reversedArr;
}

public static void printArray(int[] arr) {


for (int num : arr) {
[Link](num + " "); }
}}

/
******************************************************************************
******

Name- Anjali Modi

Batch- A1

Enrollement No.- 220146

Aim- Write a program to do the sum of two matrices of size 3*3 using objects and classes.
******************************************************************************
******/

class Matrix {
private int[][] data;

public Matrix(int[][] data) {


[Link] = data;
}

public Matrix add(Matrix other) {


int[][] result = new int[3][3];

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


for (int j = 0; j < 3; j++) {
result[i][j] = [Link][i][j] + [Link][i][j];
}
}

return new Matrix(result);


}

public void display() {


for (int[] row : data) {
for (int num : row) {
[Link](num + " ");
}
[Link]();
}
}
}

class MatrixSum {
public static void main(String[] args) {
int[][] data1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] data2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

Matrix matrix1 = new Matrix(data1);


Matrix matrix2 = new Matrix(data2);

[Link]("Matrix 1:");
[Link]();

[Link]("\nMatrix 2:");
[Link]();

Matrix sum = [Link](matrix2);


[Link]("\nSum of the matrices:");
[Link]();
}
}

/
******************************************************************************
******

Name- Anjali Modi

Batch- A1

Enrollement No.- 220146

Aim- Write a class to do the matrix multiplication and test it using main method.
******************************************************************************
******/class Matrix {
int[][] data;
Matrix(int[][] data) {
[Link] = data;
}

Matrix multiply(Matrix other) {


int rows1 = [Link];
int cols1 = [Link][0].length;
int rows2 = [Link];
int cols2 = [Link][0].length;

if (cols1 != rows2) {
throw new IllegalArgumentException("Matrices cannot be multiplied, dimensions do not
match");
}

int[][] result = new int[rows1][cols2];

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


for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += [Link][i][k] * [Link][k][j];
}
}
}

return new Matrix(result);


}

void display() {
for (int[] row : data) {
for (int num : row) {
[Link](num + " ");
}
[Link]();
}
}
}

class MatrixMultiplication {
public static void main(String[] args) {
int[][] data1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] data2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

Matrix matrix1 = new Matrix(data1);


Matrix matrix2 = new Matrix(data2);

[Link]("Matrix 1:");
[Link]();

[Link]("\nMatrix 2:");
[Link]();

try {
Matrix product = [Link](matrix2);
[Link]("\nProduct of the matrices:");
[Link]();
} catch (IllegalArgumentException e) {
[Link]([Link]());
}
}
}

/
******************************************************************************
******

Name- Anjali Modi

Batch- A1

Enrollement No.- 220146

Aim- Write a program to do the transpose of given matrix.


******************************************************************************
******/

class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

[Link]("Original Matrix:");
printMatrix(matrix);

int[][] transpose = transposeMatrix(matrix);

[Link]("\nTransposed Matrix:");
printMatrix(transpose);
}

static int[][] transposeMatrix(int[][] matrix) {


int rows = [Link];
int cols = matrix[0].length;

int[][] transpose = new int[cols][rows];

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


for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

You might also like