Question

For the program described below, document your code well. Use descriptive identifier names. Use s...

For the program described below, document your code well.

Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does.

Write a program to perform operations on square matrices (i.e. equal number of rows and columns). Both matrices should have the same number of rows and columns. The user should be asked to enter the number of rows/columns of the matrix. Use 2-D arrays to represent the matrices. Your program will fill the arrays with random numbers between 1 and 10 Option 1 The program should do the following in order Fill the arrays Print the matrices Transpose first matrix Transpose second matrix Print the matrices Multiply the matrices Print the result . You should have methods for at least the following: fillArray – accepts an array and fills it with random numbers transposeArray – accepts an array and transposes it i.e. interchanges rows and columns printArray – a cepts an array and prints it multiply – accepts two arrays, multiplies them and returns the result. Use JOptionPane to interact with the user except when printing the matrices

The program should do the following in order

Fill the arrays

Print the matrices

Transpose first matrix

Transpose second

matrix Print the matrices

Multiply the matrices

Print the result .

You should have methods for at least the following: fillArray – accepts an array and fills it with random numbers

transposeArray – accepts an array and transposes it i.e. interchanges rows and columns

printArray – a cepts an array and prints it

multiply – accepts two arrays, multiplies them and returns the result.

Use JOptionPane to interact with the user except when printing the matrices

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the java program below:

///////////////////////////

import java.util.Random;
import java.util.Scanner;

/****************************************************
*
* @author
* Name:
* Program Name:
* Date:
*
***************************************************
*Program Description:
*Program to perform operations on square matrices
*
****************************************************/

public class SquareMatrixOperations {


   public static void main(String[] args) {
       int rowsOrColumns;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Enter the number of rows/columns of the matrix:");
       rowsOrColumns = keyboard.nextInt();
       int squareMatrix[][] = new int[rowsOrColumns][rowsOrColumns];
       // Fill the square matrix with random numbers
       fillArray(squareMatrix,rowsOrColumns);
       // print the square matrix
       System.out.println("Square Matrix:= ");
       printArray(squareMatrix,rowsOrColumns);
       // transpose the square matrix
       int[][] transposedMatrix= transposeArray(squareMatrix,rowsOrColumns);
       // print the transposed square matrix
       System.out.println("Transposed Matrix:= ");
       printArray(transposedMatrix,rowsOrColumns);
      
       int[][] multipledMatrix=multiply(squareMatrix,transposedMatrix,rowsOrColumns);
       System.out.println("Multiplied Matrix:= ");
       printArray(multipledMatrix,rowsOrColumns);
      
       keyboard.close();
   }

   /****************************************
   * Fills the array with random numbers
   * @param array
   * @param n
   *****************************************/
   public static void fillArray(int[][] array,int n) {
       Random r = new Random();
       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n; j++) {
               //random numbers between 1 to 10
               array[i][j] = r.nextInt(10) + 1;
           }
       }
   }

   /****************************************
   * transposes the Array
   * @param array
   * @param n
   * return transposed matrix
   *****************************************/
   public static int[][] transposeArray(int[][] array,int n) {
       int transpose[][] = new int[n][n];
       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n; j++) {
               //transpose the array
               transpose[i][j] = array[j][i];
           }
       }
       return transpose;
   }

   /****************************************
   * prints the the Array
   * @param array a
   * @param array b
   * @param n
   *****************************************/
   public static int[][] multiply(int[][] a, int[][] b,int n) {
       int c[][]=new int[n][n];

       for(int i=0;i<n;i++){
           for(int j=0;j<n;j++){
               c[i][j]=0;  
               for(int k=0;k<n;k++)  
               {  
                   c[i][j]+=a[i][k]*b[k][j];  
               }

           }
       }
       return c;
   }
  
   /****************************************
   * prints the the Array
   * @param array
   * @param n
   *****************************************/
   public static void printArray(int[][] array,int n) {

       for (int i = 0; i < n; i++) {
           for (int j = 0; j < n; j++) {
               //print the array element
               System.out.print(array[i][j] + "   ");
           }
           //print new line for new row
           System.out.println();
       }
       System.out.println("--------------------------");
   }
}

//////////////////////////

Sample output
Enter the number of rows/columns of the matrix:
2
Square Matrix:=
3   7
4   3
--------------------------
Transposed Matrix:=
3   4
7   3
--------------------------
Multiplied Matrix:=
58   33
33   25
--------------------------

Screenshot:

Refactor Navigate Search Project Appium Studio Run Window Help Run Quick Access! : E | e貵 DSquareMatrixOperations.java 3 30 3

Let me know if you need more help on this.

Kindly like the solution if you find it useful.

Hope this helps.

Thanks.

Add a comment
Know the answer?
Add Answer to:
For the program described below, document your code well. Use descriptive identifier names. Use s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the program described below, document your code well. Use descriptive identifier names. Use spaces and...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid...

    /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, BYE Enter BYE (case insensitive) to exit the program. ****************************************************/ import java.util.Scanner; public class HexUtilitySOLUTION { public static void main(String[] args) { // Maximum length of input string final byte INPUT_LENGTH = 4; String userInput = ""; // Initialize to null string Scanner input = new Scanner(System.in); // Process the inputs until BYE is entered do {...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • Please code in C++. link to continue the code is this below or you can make...

    Please code in C++. link to continue the code is this below or you can make your own code if you wish(fix any mistakes if you think there are any in it): cpp.sh/3qcekv 3. Submit a header file (project3.h), a definition file (project3.cpp), a main file (main.cpp), and a makefile to compile them together. Make sure to run the command make and produce an application file and include it with your submission. For this project, you are required to create...

  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...

  • 1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically...

    1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically test both algorithms of the matrix math program for each of the array sizes shown in Table. Also, extend your script to automatically parse the output of the program. Table (Array Sizes in Test Suite) Algorithm 1 Algorithm 2 256 256 512 512 768 768 1024 1024 1280 1280 1536 1536 1792 1792 2048 2048 C source file // Adapted from https://gustavus.edu/+max/courses/F2011/MCS-284/labs/lab3/ // Max...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT