class ReverseArray { public static void main(String[] arg (3)
class ReverseArray { public static void main(String[] arg (3)
******************************************************************************
******
Batch- A1
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];
}
}
}
/
******************************************************************************
******
Batch- A1
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];
}
}
}
/
******************************************************************************
******
Batch- A1
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;
}
/
******************************************************************************
******
Batch- A1
Aim- Write a program to do the sum of two matrices of size 3*3 using objects and classes.
******************************************************************************
******/
class Matrix {
private int[][] data;
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}};
System.out.println("Matrix 1:");
matrix1.display();
System.out.println("\nMatrix 2:");
matrix2.display();
/
******************************************************************************
******
Batch- A1
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;
}
if (cols1 != rows2) {
throw new IllegalArgumentException("Matrices cannot be multiplied, dimensions do not
match");
}
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}};
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());
}
}
}
/
******************************************************************************
******
Batch- A1
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);
System.out.println("\nTransposed Matrix:");
printMatrix(transpose);
}