/
******************************************************************************
******
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 java.util.*;
class double1Dmain
{
public static void main(String args[])
{
double1D t1= new double1D();
t1.input();
t1.proc();
t1.output();
}
}
class double1D
{
int a[];
double1D()
{
a=new int[5];
}
void input()
{
System.out.println("enter the array elements");
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
}
void output()
{
for(int i=0;i<5;i++)
{
System.out.println(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 java.util.*;
class sum1Dmain
{
public static void main(String args[])
{
sum1D t1= new sum1D();
t1.input();
t1.proc();
t1.output();
}
}
class sum1D
{
int a[];
int sum=0;
sum1D()
{
a=new int[5];
}
void input()
{
System.out.println("enter the array elements");
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
}
void output()
{
System.out.println(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);
System.out.println("Original array:");
printArray(arr);
System.out.println("\nReversed array:");
printArray(reversedArr); }
public static int[] reverseArray(int[] arr) {
int n = arr.length;
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) {
System.out.print(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) {
this.data = 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] = this.data[i][j] + other.data[i][j];
}
}
return new Matrix(result);
}
public void display() {
for (int[] row : data) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
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);
System.out.println("Matrix 1:");
matrix1.display();
System.out.println("\nMatrix 2:");
matrix2.display();
Matrix sum = matrix1.add(matrix2);
System.out.println("\nSum of the matrices:");
sum.display();
}
}
/
******************************************************************************
******
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) {
this.data = data;
}
Matrix multiply(Matrix other) {
int rows1 = this.data.length;
int cols1 = this.data[0].length;
int rows2 = other.data.length;
int cols2 = other.data[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] += this.data[i][k] * other.data[k][j];
}
}
}
return new Matrix(result);
}
void display() {
for (int[] row : data) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
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);
System.out.println("Matrix 1:");
matrix1.display();
System.out.println("\nMatrix 2:");
matrix2.display();
try {
Matrix product = matrix1.multiply(matrix2);
System.out.println("\nProduct of the matrices:");
product.display();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
/
******************************************************************************
******
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}
};
System.out.println("Original Matrix:");
printMatrix(matrix);
int[][] transpose = transposeMatrix(matrix);
System.out.println("\nTransposed Matrix:");
printMatrix(transpose);
}
static int[][] transposeMatrix(int[][] matrix) {
int rows = matrix.length;
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];
}
}