Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Classes to Implement For this assignment, you must implement three (3) Java classes: Matrix, Vector, and MarkovChain. Follow the guidelines for each one below. In

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Classes to Implement For this assignment, you must implement three (3) Java classes: Matrix, Vector, and MarkovChain. Follow the guidelines for each one below. In these classes, you may implement more private (helper) methods if you want. However, you may not implement more public methods except public static void main(String[ args) for testing purposes (this is allowed and encouraged). You may not add instance variables other than the ones specified in these instructions nor change the variable types or accessibility (i.e. making a variable public when it should be private). Penalties will be applied if you implement additional instance variables or change the variable types or modifiers from what is described here. Matrix.java This class is used to represent a matrix containing double (i.e. decimal) numbers. The class must have exactly (no more and no less) the following private instance variables: - private int numRows - private int numCols - private double[] data The class must have the following public methods: - public Matrix (int r, int c): constructor - Initialize the instance variables numRows and numCols and initialize data to have r rows and c columns - public Matrix (int r, int c, double[] linArr): constructor - Same as above, but now you have to populate the 2-dimensional array data with the elements in linArr - Note that linArr is a 1-dimensional array whereas data is 2-dimensional, so you have to map the items from linArr to data. The first values from linArr must be copied in left to right order across the first row of data, the following values of linArr must then be copied to the second row, and so on. For example, [1, 2, 3, 4] would be put into a 2 by 2 array with [1, 2] in the first row and then [3, 4] in the next row. - public int getNumRows() - Return the number of rows in the matrix - public int getNumCols() - Return the number of columns in the matrix - public double[]] getData() - Return the 2-dimensional array containing the matrix data - public double getElement(int r, int c) - Return the value from data at row r and column c - public void setElement(int r, int c, double value) - public void transpose() - Transpose the matrix and update the instance variables to store the transposed matrix as the new instance of 'this' matrix ( see explanation below about how to transpose a matrix ) - public Matrix multiply (double scalar) - Create a new Matrix object with the same dimensions as 'this' matrix. - Multiply each value in 'this' matrix by the given scalar and insert the resulting values into the new Matrix object and return it ( see explanation below ) - public Matrix multiply (Matrix other) - If the number of columns of 'this' matrix is not equal to the number of rows of other matrix, return null immediately. - If the above condition is not true, create a new Matrix object with the number of rows from 'this' and the number of columns from other. - Multiply 'this' Matrix object by other and insert the resulting values into the new matrix object and return it. ( see explanation below *) - public String toString() - If the matrix data array is empty, return "Empty matrix" - Otherwise, build a string containing the entire matrix following the format shown below and return that string ( see explanation below ) Transposing a Matrix Given a matrix M, transposing M into matrix N means reflecting all the elements from M along the diagonal to go into N. In other words, the first row of M becomes the first column of N, the second row of M becomes the second column of N, and so on, thus N[i][i]=M[j][i] for all i,j indices. This means the number of rows and columns must be swapped. Example: 83191275=transpose[81237195] Matrix-Scalar Multiplication When multiplying a matrix by a scalar (single number), each number in the matrix must be multiplied by that scalar. The resulting matrix is the same size as the original matrix and each number is just multiplied by that scalar's value. Example: 3[3201]=[33323031]=[9603] Matrix-Matrix Multiplication Multiplying two matrices together can only be done if the number of columns of the first matrix (this) is equal to the number of rows of the second matrix (other). Consider 2 matrices A and B satisfying the above condition and let M be the matrix obtained by multiplying A and B. The entry M[i][]] is obtained by multiplying the i-th row of A with the j-th column of B. To multiply a row r with a column c we add the products r[i]c[i] for all the values stored in r and c, so M[i][j]=i=0n1r[i]c[i] where n is the number of values in row r and column c. For the example below, the top-left value in the resulting matrix will be calculated by multiplying the top row of the first matrix [3,0] by the left column of the second matrix [7,4] (vertical vector) so we multiply 3 by 7 and then 0 by 4 and add them together to get 21 . For the top-right element, we multiply the top row of the first matrix by the right column of the second matrix: 32+03=6. Example: [3201][7423]=[37+0427+1432+0322+13]=[211867] Matrix toString Format When returning the string for a non-empty matrix in the toString() method, you must format it in a specific way. Each value in the matrix must be converted to a string of exactly 8 characters (that includes numbers, decimal point, and spaces needed to complete 8 characters) in which the number of decimal digits is exactly 3 . Start with an empty string S and add the string representation of the first value in the first row of the matrix; then concatenate to S the string representation of the second number in the first row, then the string for the third number and so on. After the last number in the first row, add to S a newline character "In' so if you print S the next row will be shown below the first one. Then, concatenate to S the strings for the numbers of the second row of the matrix followed by 'In'; then concatenate the strings for the numbers in the third row, and so on. Do not add any manual spaces or tabs anywhere in the string. Hint: use the String.format() method and look up how to use it to specify the total spaces and the number of decimal points. Example: This is how the string produced by this method might look like when printed: Each value takes up 8 total spaces and shows 3 decimal places Vector.java This class is used to represent a Vector. Recall that vectors are special kinds of matrices so this class must inherit from the Matrix class. In the explanation above, vectors are defined as having either one row or one column; but in this assignment we will create them to be horizontal, that is they have 1 row. No instance variables are needed in this class. The class must have the following public methods: - public Vector (int c): constructor - Use super() to call the Matrix constructor and send in 1 as the value of r - public Vector (int c, double[ linArr): constructor - Use super() to call the Matrix constructor and send in 1 as the value of r - public double getElement (int c) - Return the value at row 0 and column c (since it only has 1 row) MarkovChain.java This class represents a Markov Chain to produce a matrix of predicted future states using a state vector and a transition matrix. The class must have these private instance variables: - private Vector stateVector; - private Matrix transitionMatrix; The class must have the following public methods: - public MarkovChain (Vector sVector, Matrix tMatrix): constructor - public boolean isValid () - Check if the instance variables are valid for a Markov chain problem and return a Boolean to indicate its validity (true if valid, false if invalid): To be valid, the transition matrix must be a square (equal number of rows and columns) and that number must also match the number of columns in the state vector. Further, the sum of values in the state vector must equal 1.0 (*see note below) and the sum of values in each row of the transition matrix must equal 1.0 (*see note below) - public Matrix computeProbabilityMatrix (int numSteps) - First, call isValid() to see if this is a valid Markov chain. If not, return null immediately. - Compute the probability matrix of this Markov chain after the given numSteps by multiplying the transition matrix by itself numSteps-1 times and multiplying the state vector by the resulting matrix. Note that you must multiply the state vector by the matrix and not the other way around. Return the resulting vector (note that it will technically be a Matrix object, not a Vector object, but it will be a 1-dimensional matrix with the same dimensions as the state vector). *Note: due to possible roundoff errors, checking if the sum is equal to 1.0 should not be done using == but rather checking if the value is very close to 1.0. For simplicity, you can check if the sum value is between 0.99 and 1.01. Provided files TestMatrixVector.java and TestMarkov.java are tester files to help check if your java classes are implemented correctly. TestMatrixVector.java tests your Matrix.java and Vector.java files, and TestMarkov.java tests your MarkovChain.java file. Similar files will be incorporated into Gradescope's auto-grader. Passing all the tests within these files does not necessarily mean that your code is correct in all cases

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

3. Are our bosses always right? If not, what should we do?

Answered: 1 week ago

Question

Identify and control your anxieties

Answered: 1 week ago

Question

Understanding and Addressing Anxiety

Answered: 1 week ago