Question

In: Computer Science

Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...

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.

Solutions

Expert Solution

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===========


Related Solutions

Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value...
Design a simple program, using pseudocode, to implement the recursive formula you found in part (a)...
Design a simple program, using pseudocode, to implement the recursive formula you found in part (a) to compute numbers in the Fibonacci sequence. Describe in detail how your program implements the recursive formula. You may find it useful to discuss how it through a concrete example such as F(8) = 21.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT