In: Computer Science
Write a program in Java
Design and implement simple matrix manipulation techniques program in java.
Project Details:
Your program should use 2D arrays to implement simple matrix operations. Your program should do the following:
• Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make sure you format as a matirx • Repeat the previous steps for a second matrix M2 • Create a matrix M3 that is the transpose of M1 and print it to the console • Check if M1 and M2 can be added (should have the same dimensions). If possible, add M1 and M2 and print the result to the console. Otherwise print an error message. • Extra credit: Multiply M1 and M2 if possible and print to the console. If the matrices cannot be multiplied, print an error message. Implementation requirements: • Use a helper method for reading a positive integer using an input validation loop. • Use a helper method for printing a matrix. Your helper methods should be private and static, and called in the main method.
Hi,
I have added the code below:
==================Code including extra credit===============
import java.util.Scanner;
public class MatrixManipulation {
static int row=0;
static int coloumn=0;
static Scanner sc = new Scanner(System.in);
/*This method will ask the dimensions of 2D array
* It will create and returns the 2D array
*/
private static int[][] create2DMatrix() {
System.out.println("Please enter row counts: ");
row = sc.nextInt();
/* In this block we are forcing user to enter row value more than 0
* This runs until user enter more than equal 1
*/
while (row == 0 || row < 0) {
System.out.println("You entered 0 or less than 0, Please enter row counts again: ");
row = sc.nextInt();
}
System.out.println("Please enter coloumn counts: ");
coloumn = sc.nextInt();
/* In this block we are forcing user to enter column value more than 0
* This runs until user enter more than equal 1
*/
while (coloumn == 0 || coloumn < 0) {
System.out.println("You entered 0 or less than 0, Please enter coloumn counts again: ");
coloumn = sc.nextInt();
}
System.out.println("row " + row + " coloumn " + coloumn);
//creating 2D array with the user input
int[][] M1 = new int[row][coloumn];
// return matrix
return M1;
}
/*
* This method will let user to fill the 2D matrix with the integer values
*/
private static void askUserToEnterDataInMatrix(int [][] matrix2d) {
System.out.println("please enter all " + row * coloumn + " values, ");
for (int i = 0; i < row; i++) {
for (int j = 0; j < coloumn; j++) {
matrix2d[i][j] = sc.nextInt();
}
}
}
/*
* This method will be used to display the 2d Matrix
*/
private static void showMatrix(int [][] matrix2D) {
for (int i = 0; i < matrix2D.length; i++) { // matrix2D.length is equal to the row count
for (int j = 0; j < matrix2D[0].length; j++) { // matrix2D[0].length is equal to the column count
System.out.print(matrix2D[i][j] + " ");
}
System.out.println();
}
}
/*
* This method is use to transpose the matrix and return a transposed matrix
* To transpose the matrix we need to interchange the value of row with column
* Hence, matrix2D[0].length (Which was used as column is above method) here act as row
* and matrix2D.length (Which was used as row is above method) here it act as column
*/
private static int[][] createTranspose(int [][] matrix2D){
int [][] transpose=new int[matrix2D[0].length][matrix2D.length];
System.out.println("colo for real"+matrix2D[0].length);
System.out.println("row for real "+matrix2D.length);
for(int i=0;i<matrix2D[0].length;i++){
for(int j=0;j<matrix2D.length;j++){
transpose[i][j]=matrix2D[j][i];
}
}
return transpose;
}
/*
* This method will have 2 parameters(2D Matrix)
* We are creating a addedMat of row size = M1's row (M1.length)
* and column size=M1's column (M1[0].length)
*
*
*/
private static int[][] addMatrix(int[][] M1,int [][] M2){
int [][] addedMat=new int[M1.length][M1[0].length];
for(int i=0;i<M1.length;i++){
for(int j=0;j<M1[0].length;j++){
addedMat[i][j]=M1[i][j]+M2[i][j];
}
}
return addedMat;
}
/*
* This method will expect 2 2D array as parameter
* It will return 2D array, after completing matrix multiplication
*
*/
private static int[][] multiplyMatrix(int[][] M1, int [][] M2){
int [][] multipliedMat=new int[M1.length][M1[0].length];
for(int i=0;i<M1.length;i++){
for(int j=0;j<M1[0].length;j++){
multipliedMat[i][j]=0;
for(int k=0;k<M1[0].length;k++)
{
multipliedMat[i][j]+=M1[i][k]*M2[k][j];
}
}
}
return multipliedMat;
}
public static void main(String[] args) {
int [][] M1=create2DMatrix();
askUserToEnterDataInMatrix(M1);
System.out.println("M1 matrix");
showMatrix(M1);
int [][] M2=create2DMatrix();
askUserToEnterDataInMatrix(M2);
System.out.println("M2 matrix");
showMatrix(M2);
int [][] M3=createTranspose(M1);
System.out.println("Transposed of M1");
showMatrix(M3);
//This block is to check whether the dimension on M1 and M2 matrix are different
if(M1.length==M2.length && M1[0].length==M2[0].length ) {
int [][] addedMatrix=addMatrix(M1,M2);
System.out.println("Added matrix");
showMatrix(addedMatrix);
}
else {
System.out.println("The dimemsions of both the arrays are different.. hence we cant add both the matrices");
}
//This block is to check whether the dimension on M1 and M2 matrix are different
if(M1.length==M2.length && M1[0].length==M2[0].length ) {
int [][] mulMatrix=multiplyMatrix(M1,M2);
System.out.println("Multiplied matrix");
showMatrix(mulMatrix);
}
else {
System.out.println("The dimemsions of both the arrays are different.. hence we cant multiply both the matrices");
}
}
}
========Please consider upvoting me=========================Thanks===========