Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to design and implement a Java Class named Matrix, here is what you need

ID: 2247096 • Letter: Y

Question

You are to design and implement a Java Class named Matrix, here is what you need to do. Include 3 attributes, rows, columns and a 2D array for the actual matrix A constructor that takes the number of rows and columns of the 2D array as parameters from client and instantiates it. A method called copyMatrix that returns a copy of a Matrix object. (Do not clone!!) A method called equals that returns true if two Matrix objects are the same, otherwise returns false. Two Matrix objects are the same if they have the same number of rows and columns, and the value Ai, j of Matrix A's 2D array is equal to value Bi, j of Matrix B's 2D array. A toString method that returns a properly formatted String that contains at- tributes of a Matrix object. A toString method in a Java class always returns a String that contains the current state of the Object. The current state is represented by Object's attributes. A method called scalarMultiply to multiply a Matrix by scalar in which every value Ai j in Matrix A's 2D array is multiplied by a scalar value k. See details in Figure 2. You only need to pass value k as a parameter to this method. A method to multiply called multiply a Matrix by another Matrix. If the number of columns in Matrix A's 2D array is not equal

Explanation / Answer

import java.util.Scanner;

public class JavaProgram

{

public static void main(String args[])

{

int row, col, i, j;

int arr[][] = new int[10][10];

Scanner scan = new Scanner(System.in);

  

System.out.print("Enter Number of Row for Array (max 10) : ");

row = scan.nextInt();

System.out.print("Enter Number of Column for Array (max 10) : ");

col = scan.nextInt();

  

System.out.print("Enter " +(row*col)+ " Array Elements : ");

for(i=0; i<row; i++)

{

for(j=0; j<col; j++)

{

arr[i][j] = scan.nextInt();

}

}

  

System.out.print("The Array is : ");

for(i=0; i<row; i++)

{

for(j=0; j<col; j++)

{

System.out.print(arr[i][j]+ " ");

}

System.out.println();

}

}

}

import java.util.Arrays;

public class MatrixPrinter {

public static void main(String[] args) {

final int[][] matrix = new int[4][4];

printMatrix(matrix);

}

public static void printMatrix(int[][] matrix) {

Arrays.stream(matrix)

.forEach(

(row) -> {

System.out.print("[");

Arrays.stream(row)

.forEach((el) -> System.out.print(" " + el + " "));

System.out.println("]");

}

);

}

}

import java.util.Arrays;

import java.util.function.Consumer;

public class MatrixPrinter {

public static void main(String[] args) {

final int[][] matrix = new int[3][3];

Consumer<int[]> noDelimiter = (row) -> {

Arrays.stream(row).forEach((el) -> System.out.print(" " + el + " "));

System.out.println();

};

Consumer<int[]> pipeDelimiter = (row) -> {

Arrays.stream(row).forEach((el) -> System.out.print("| " + el + " "));

System.out.println("|");

};

Consumer<int[]> likeAList = (row) -> {

System.out.print("[");

Arrays.stream(row)

.forEach((el) -> System.out.print(" " + el + " "));

System.out.println("]");

};

printMatrix(matrix, noDelimiter);

System.out.println();

printMatrix(matrix, pipeDelimiter);

System.out.println();

printMatrix(matrix, likeAList);

}

public static void printMatrix(int[][] matrix, Consumer<int[]> rowPrinter) {

Arrays.stream(matrix)

.forEach((row) -> rowPrinter.accept(row));

}

}

public static void printMatrix(int[][] arr) {

if (null == arr || arr.length == 0) {

return;

}

int idx = -1;

StringBuilder[] sbArr = new StringBuilder[arr.length];

for (int[] row : arr) {

sbArr[++idx] = new StringBuilder("( ");

for (int elem : row) {

sbArr[idx].append(elem + " ");

}

sbArr[idx].append(")");

}

for (int i = 0; i < sbArr.length; i++) {

System.out.println(sbArr[i]);

}

System.out.println(" DONE ");

}

import java.util.Scanner;

class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();

System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

System.out.println("Sum of entered matrices:-");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+" ");

System.out.println();
}
}
}

public class TwoDimArrayMethods
{
public void printMatrix(int[][] matrix)
{
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
{
System.out.printf("%7d", matrix[row][col]);
}

System.out.println();
}
}

public void sumRows(int[][] matrix)
{
int sum;

  
for (int row = 0; row < matrix.length; row++)
{
sum = 0;

for (int col = 0; col < matrix[row].length; col++)
{
sum = sum + matrix[row][col];
}

System.out.println("The sum of row " + (row + 1) + " = "
+ sum);
}
}

public void largestInRows(int[][] matrix)
{
int largest;

for (int row = 0; row < matrix.length; row++)
{
  
largest = matrix[row][0];

for (int col = 1; col < matrix[row].length; col++)
{
if (largest < matrix[row][col])
{
largest = matrix[row][col];
}
}

System.out.println("The largest element of row " + (row + 1)
+ " = " + largest);
}
}
}
Program (TwoDimArrayMethodsDemo.java)
public class TwoDimArrayMethodsDemo
{
public static void main(String[] args)
{
TwoDimArrayMethods operate = new TwoDimArrayMethods();
int[][] board =
{
{ 20, 15, 6, 19, 18 }, { 4, 46, 24, 17, 18 },
{ 12, 50, 23, 16, 31 }
};

operate.printMatrix(board);
System.out.println();
operate.sumRows(board);
System.out.println();
operate.largestInRows(board);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote