You are to write a program that reads an input file containing two matrices, and that repeatedly asks the user if they want to print, add, subtract, transpose, or quit. Here is a sample run of the program: See image.

Input file:

A sample input file, matrix.txt, Here are the contents of matrix.txt:

3 5
8 19 7 15 4
21 42 9 0 26
13 7 2 1 16

3 5
2 12 6 9 0
40 22 8 4 17
3 13 8 29 10

You may assume that any input file has the same format:

  • rows and columns of first matrix (separated by a space)
  • first matrix values, separated by spaces and listed on separate rows blank line
  • rows and columns of second matrix (separated by a space)
  • second matrix values, separated by spaces and listed on separate rows

Note that the two matrices do NOT have to have the same dimensions.

Implementation Requirements:

Your program should have two classes, Matrix and Proj4.

Matrix should be a general class that represents a matrix. It should have instance variables for the number of rows, number of columns, and a two-dimensional array of the matrix values. It should also have a constructor that takes the number of rows and columns as parameters, initializes the instances variables, and allocates space for the twodimensional array. It should also have the following methods:

  • public void setElem(int row, int col, int value)
  • public String toString()
  • public Matrix plus(Matrix m)
  • public Matrix minus(Matrix m)
  • public Matrix transpose()

The setElem method should update the two-dimensional array to put that value at the specified row/column position. The toString method should return a string representation of this Matrix so that if that string was printed, it would print in the format shown in the screenshot above. The plus method should return a new Matrix object that is THIS Matrix plus m (the minus method should be similar, but with a different operation). The transpose method should return a new Matrix object that is the transposition of THIS Matrix.

The Proj4 class should contain a single main method. It should first open the matrix file specified as a command-line argument. It should then create two Matrix objects and fill them with values using setElem. Then, it should repeatedly ask the user for an option and call the appropriate method (toString, plus, minus, or transpose), and print the results.

Other requirements:

Here are some additional requirements and tips:

  • There should be no print statements in the Matrix class. To print a matrix, you will call the toString method and then print that result.
  • All user input and file input should be in the Proj4 class
  • Matrices can only be added or subtracted if they have the same number of rows and the same number of columns. In your plus and minus methods in Matrix, you should return null if the dimensions do not match. When you call plus and minus from Proj4, you will need to check to see if the returned result is null.
  • To transpose a matrix, the rows become the columns and the columns become the rows.
  • If the user does not provide an input file as a command-line argument, you should print an error and exit.
  • Remember that your program should work with ANY input file of the specified format, not just the matrix.txt file. Try creating another file to test if your program correctly handles matrices of different dimensions.

Documentation:

You should put a description of the project at the top of the file and at the top of each method. Please use this template for the top of the file:

/**
* (description of the project)
*
* @author (your name)
* @version (which number project this is)
*/

Please use this template for the top of each method:

/**
* (description of the method)
*
* @param (describe first parameter)
* @param (describe second parameter)
* (list all parameters, one per line)
* @return (describe what is being returned)
*/
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.