Question & Answer: It's a java matrix problem. Could you help me solve this thank y…..

It's a java matrix problem. Could you help me solve this thank you.
class to model a 3 x 4 matrix of doubles <br>
Supports Gaussian row operations
 <br> In this version, rows and columns start at 0
public class Matrix {
    
    public static final int ROW = 3;
    public static final int COL = 4;
    
    // declare the instance variable that will hold the 2-dim array
    
    
    /**Instantiate a ROW x COL matrix, empty
      */
    public Matrix() {
        
    }
    
    
    /** set the value of a particular element in the matrix
      * @param row the row of the element. 0 <= row < Matrix.ROW
      * @param col the column of the element.   0 <= col < Matrix.COL
      * @param value the value to be stored
      * @throws ArrayIndexOutOfBoundsException for invalid row or column
      */
    public void setValue(int row, int col, double value) {
        // Why don't we have to test row/col for validity?
        
    }
    
    /** returns the value of a particular element in the matrix
      * @param row the row of the element. 0 <= row < Matrix.ROW
      * @param col the column of the element.   0 <= col < Matrix.COL
      * @throws ArrayIndexOutOfBoundsException for invalid row or column
      */
    public double getValue(int row, int col) {
        
    }
    
    
    /** swap 2 rows of this matrix
      * @param r1 one of the rows to swap.  0 <= r1 < Matrix.ROW
      * @param r2 the other row to swap.   0 <= r2 < Matrix.ROW
      * @throws ArrayIndexOutOfBoundsException for invalid row or column
      */
    public void swapRows(int r1, int r2) {
        
    }
    
    /** multiply one row by a non-zero constant
      * @param multiple the non-zero constant
      * @param row the row to change
      * @throws IllegalArgumentException if multiple is 0
      * @throws ArrayIndexOutOfBoundsException for invalid row
      */
    public void multiplyRow(double multiple, int row) {
        
    }
    
    /** add row r1 into row r2. Equivalent to r2 += r1 
     * @param r1 the row to add  0 <= r1 < Matrix.ROW
     * @param r2 the row to add into.  0 <= r2 < Matrix.ROW.  This row will change.
     * @throws ArrayIndexOutOfBoundsException for invalid row
     */
    public void addRows (int r2, int r1) {
        
    }
    
    /**
     * set new row in the matrix
     * @param row the new row to insert
     * @param rIdx the index of this new row  0 <= rIdx < Matrix.ROW
     * @return the old row
     * @throws IllegalArgument exception if row is not the correct length of Matrix.COL
     * @throws ArrayIndexOutOfBoundsException for invalid row
     */
    public double[] replace(double[] row, int rIdx){
        
    }
    
    /**
     * Add 2 Matrices together and return a new Matrix
     * @param m the 2nd Matrix
     * @return the matrix sum of this + m
     */
    
    public Matrix sum(Matrix m){
        
    }
    
    
    /** Return this matrix as a String of 3 rows of numbers in 4 columns
      */
    public String toString() {
        
    }
}  

Expert Answer

 

package Online;

//Creates a class Matrix

public class Matrix

{

//Final instance variable for maximum number of row and column

public static final int ROW = 3;

public static final int COL = 4;

 

//Declare the instance variable that will hold the 2-dim array

double myMatrix[][];

 

 

/**Instantiate a ROW x COL matrix, empty */

public Matrix()

{

//Creates an empty matrix

myMatrix = new double[ROW][COL];

}//End of constructor

 

 

/** set the value of a particular element in the matrix

* @param row the row of the element. 0 <= row < Matrix.ROW

* @param col the column of the element. 0 <= col < Matrix.COL

* @param value the value to be stored

* @throws ArrayIndexOutOfBoundsException for invalid row or column

*/

public void setValue(int row, int col, double value)

{

// Why don’t we have to test row/col for validity?

//Try block begins

try

{

//Validates the row and column

if((row >= 0 && row < Matrix.ROW) && (col >= 0 && col < Matrix.COL))

//Assigns the value in the matrix

myMatrix[row][col] = value;

//For invalid row or column

else

//Throws an exception

throw new ArrayIndexOutOfBoundsException();

}//End of try block

//Handles ArrayIndexOutOfBoundsException

catch(ArrayIndexOutOfBoundsException ae)

{

//Displays error message

System.out.println(“Error: Wrong Index number. Row = ” + row + ” Column = ” + col);

}//End of catch

}//End of method

 

/** returns the value of a particular element in the matrix

* @param row the row of the element. 0 <= row < Matrix.ROW

* @param col the column of the element. 0 <= col < Matrix.COL

* @throws ArrayIndexOutOfBoundsException for invalid row or column

*/

public double getValue(int row, int col)

{

//To store the row value

double rowValue = 0;

//Try block begins

try

{

//Validates the row and column

if((row >= 0 && row < Matrix.ROW) && (col >= 0 && col < Matrix.COL))

//Assigns the value in the matrix

rowValue = myMatrix[row][col];

//For invalid row or column

else

//Throws an exception

throw new ArrayIndexOutOfBoundsException();

}//End of try block

//Handles ArrayIndexOutOfBoundsException

catch(ArrayIndexOutOfBoundsException ae)

{

System.out.println(“Error: Wrong Index number. Row = ” + row + ” Column = ” + col);

}//End of catch

//Returns the row value

return rowValue;

}//End of method

 

 

/** swap 2 rows of this matrix

* @param r1 one of the rows to swap. 0 <= r1 < Matrix.ROW

* @param r2 the other row to swap. 0 <= r2 < Matrix.ROW

* @throws ArrayIndexOutOfBoundsException for invalid row or column

*/

public void swapRows(int r1, int r2)

{

//Try block begins

try

{

//Validates the row

if((r1 >= 0 && r1 < Matrix.ROW))

{

//Loops till maximum number of columns

for(int c = 0; c < Matrix.COL; c++)

{

//Swapping process

double temp = myMatrix[r1][c];

myMatrix[r1][c] = myMatrix[r2][c];

myMatrix[r2][c] = temp;

}//End of loop

}//End of if

//For invalid row

else

//Throws an exception

throw new ArrayIndexOutOfBoundsException();

}//End of try block

//Handles ArrayIndexOutOfBoundsException

catch(ArrayIndexOutOfBoundsException ae)

{

//Displays the error message

System.out.println(“Error: Wrong Index number. Row1 = ” + r1 + ” Row 2 = ” + r2);

}//End of catch

}//End of method

 

/** multiply one row by a non-zero constant

* @param multiple the non-zero constant

* @param row the row to change

* @throws IllegalArgumentException if multiple is 0

* @throws ArrayIndexOutOfBoundsException for invalid row

*/

public void multiplyRow(double multiple, int row)

{

//Try block begins

try

{

//Validates the number to be multiplied

if(multiple == 0)

//If it is zero then throw an exception

throw new IllegalArgumentException();

//Validates the row

if((row >= 0 && row < Matrix.ROW))

{

//Loops till maximum column

for(int c = 0; c < Matrix.COL; c++)

{

//Multiply the value with the matrix value and store it in same index position

myMatrix[row][c] *= multiple;

}//End of loop

}//End of if

//For invalid number

else

//Throws an exception

throw new ArrayIndexOutOfBoundsException();

}//End of try block

//Handles IllegalArgumentException

catch(IllegalArgumentException ae)

{

//Displays the error message

System.out.println(“Error: Number cannot be zero.”);

}//End of catch

//Handles ArrayIndexOutOfBoundsException

catch(ArrayIndexOutOfBoundsException ae)

{

//Displays the error message

System.out.println(“Error: Wrong Index number. Row1 = ” + row);

}//End of catch

}//End of method

 

/** add row r1 into row r2. Equivalent to r2 += r1

* @param r1 the row to add 0 <= r1 < Matrix.ROW

* @param r2 the row to add into. 0 <= r2 < Matrix.ROW. This row will change.

* @throws ArrayIndexOutOfBoundsException for invalid row

*/

public void addRows (int r2, int r1)

{

//Try block begins

try

{

//Validates the row

if((r1 >= 0 && r1 < Matrix.ROW))

{

//Loops till maximum number of columns

for(int c = 0; c < Matrix.COL; c++)

{

//Adds the second matrix value with first matrix value and store it in second matrix

myMatrix[r2][c] += myMatrix[r1][c];

}//End of loop

}//End of if

//For invalid row

else

//Throws an exception

throw new ArrayIndexOutOfBoundsException();

}//End of try block

//Handles ArrayIndexOutOfBoundsException

catch(ArrayIndexOutOfBoundsException ae)

{

//Displays the error message

System.out.println(“Error: Wrong Index number. Row1 = ” + r1 + ” Row 2 = ” + r2);

}//End of catch

}//End of method

 

/**

* set new row in the matrix

* @param row the new row to insert

* @param rIdx the index of this new row 0 <= rIdx < Matrix.ROW

* @return the old row

* @throws IllegalArgument exception if row is not the correct length of Matrix.COL

* @throws ArrayIndexOutOfBoundsException for invalid row

*/

public double[] replace(double[] row, int rIdx)

{

//Creates a array to store the old row

double oldRow[] = new double[Matrix.COL];

//Try block begins

try

{

//Validates the new row length

if(row.length != Matrix.COL)

//Throws an IllegalArgumentException exception

throw new IllegalArgumentException();

//Validates the new row and column

if((rIdx >= 0 && rIdx < Matrix.ROW))

{

//Loops till maximum column

for(int c = 0; c < Matrix.COL; c++)

{

//Stores the old row value

oldRow[c] = myMatrix[rIdx][c];

//Replaces the row value

myMatrix[rIdx][c] = row[c];

}//End of loop

}//End of if

//For invalid row and column

else

//Throws an ArrayIndexOutOfBoundsException

throw new ArrayIndexOutOfBoundsException();

}//End of try block

//Handles IllegalArgumentException

catch(IllegalArgumentException ae)

{

//Displays error message

System.out.println(“Error: Number cannot be zero.”);

}//End of catch

//Handles ArrayIndexOutOfBoundsException

catch(ArrayIndexOutOfBoundsException ae)

{

//Displays error message

System.out.println(“Error: Wrong Index number. Row1 = ” + row);

}//End of catch

//Returns the old row

return oldRow;

}//End of method

 

/**

* Add 2 Matrices together and return a new Matrix

* @param m the 2nd Matrix

* @return the matrix sum of this + m

*/

 

public Matrix sum(Matrix m)

{

//Creates an object of Matrix class to store result

Matrix m3 = new Matrix();

//Loops till maximum row

for(int r = 0; r < Matrix.ROW; r++)

{

//Loops till maximum column

for(int c = 0; c < Matrix.COL; c++)

{

//Adds the first and second matrix and stores it in third matrix

//this is used for first matrix

//m is the parameter used for second matrix

//m3 is the third or result matrix

m3.myMatrix[r][c] = this.myMatrix[r][c] + m.myMatrix[r][c];

}//End of for loop

}//End of if

//Returns the result matrix contained by the object

return m3;

}//End of method

 

 

/** Return this matrix as a String of 3 rows of numbers in 4 columns */

public String toString()

{

//To store the result

String result = “”;

//Loops till maximum row

for(int r = 0; r < Matrix.ROW; r++)

{

//Loops till maximum column

for(int c = 0; c < Matrix.COL; c++)

{

//Concatenates the matrix value with space and stores it in result

result += myMatrix[r][c] + ” “;

}//End of inner loop

//Concatenates the new line character

result += “n”;

}//End of outer loop

//Returns the result

return result;

}//End of method

//Main method definition

public static void main(String ss[])

{

//Creates an object of the Matrix class

Matrix m = new Matrix();

//Set the value of 2nd row and 2nd column to 10

m.setValue(2, 2, 10);

//Displays the value

System.out.println(“Result ” + m.getValue(2, 2));

//Set the value of -1 row and 4th column to 50

m.setValue(-1, 4, 50);

//Displays the value

System.out.println(“Result ” + m.getValue(3, 4));

//Loops till maximum row

for(int r = 0; r < Matrix.ROW; r++)

{

//Loops till maximum column

for(int c = 0; c < Matrix.COL; c++)

{

//Stores value in matrix

m.myMatrix[r][c] = (r + c);

}//End of inner loop

}//End of outer loop

System.out.println(“Before Swap n” + m);

//Swapping operation

m.swapRows(2, 1);

System.out.println(“After Swap n” + m);

System.out.println(“Before Multiplication n” + m);

//Multiply operation

m.multiplyRow(2, 2);

System.out.println(“After Multiplication n” + m);

System.out.println(“Before Addition n” + m);

//Add operation

m.addRows(1, 2);

System.out.println(“After Addition n” + m);

//Creates a second object for second matrix

Matrix second = new Matrix();

//Loops till maximum row

for(int r = 0; r < Matrix.ROW; r++)

{

//Loops till maximum column

for(int c = 0; c < Matrix.ROW; c++)

{

second.myMatrix[r][c] = (r + c);

}//End of inner loop

}//End of outer loop

//Creates an object to store the result matrix

Matrix result = new Matrix();

System.out.println(“Before Matrix Sumn”);

System.out.println(“Frist Matrix n” + m);

System.out.println(“Second Matrix n” + second);

//Sum operation

result = m.sum(second);

System.out.println(“After Matrix Sum n”);

System.out.println(“Frist Matrix n” + m);

System.out.println(“Second Matrix n” + second);

System.out.println(“Result Matrix n” + result);

//Creates an array to store four double values

double replaceRow[] = {10.2, 20.2, 30.3, 40.4};

//Creates an array to store the old row

double oldRow[] = new double[Matrix.COL];

System.out.println(“Before Replacement n” + m);

//Replacement operation

oldRow = m.replace(replaceRow, 1);

System.out.println(“After Replacement n” + m);

System.out.print(“Old Row: “);

//Loops till length of old row

for(int c = 0; c < oldRow.length; c++)

//Displays the value

System.out.print(oldRow[c] + ” “);

}//End of main

} //End of class

Sample Run:

Result 10.0
Error: Wrong Index number. Row = -1 Column = 4
Error: Wrong Index number. Row = 3 Column = 4
Result 0.0
Before Swap
0.0 1.0 2.0 3.0
1.0 2.0 3.0 4.0
2.0 3.0 4.0 5.0

After Swap
0.0 1.0 2.0 3.0
2.0 3.0 4.0 5.0
1.0 2.0 3.0 4.0

Before Multiplication
0.0 1.0 2.0 3.0
2.0 3.0 4.0 5.0
1.0 2.0 3.0 4.0

After Multiplication
0.0 1.0 2.0 3.0
2.0 3.0 4.0 5.0
2.0 4.0 6.0 8.0

Before Addition
0.0 1.0 2.0 3.0
2.0 3.0 4.0 5.0
2.0 4.0 6.0 8.0

After Addition
0.0 1.0 2.0 3.0
4.0 7.0 10.0 13.0
2.0 4.0 6.0 8.0

Before Matrix Sum

Frist Matrix
0.0 1.0 2.0 3.0
4.0 7.0 10.0 13.0
2.0 4.0 6.0 8.0

Second Matrix
0.0 1.0 2.0 0.0
1.0 2.0 3.0 0.0
2.0 3.0 4.0 0.0

After Matrix Sum

Frist Matrix
0.0 1.0 2.0 3.0
4.0 7.0 10.0 13.0
2.0 4.0 6.0 8.0

Second Matrix
0.0 1.0 2.0 0.0
1.0 2.0 3.0 0.0
2.0 3.0 4.0 0.0

Result Matrix
0.0 2.0 4.0 3.0
5.0 9.0 13.0 13.0
4.0 7.0 10.0 8.0

Before Replacement
0.0 1.0 2.0 3.0
4.0 7.0 10.0 13.0
2.0 4.0 6.0 8.0

After Replacement
0.0 1.0 2.0 3.0
10.2 20.2 30.3 40.4
2.0 4.0 6.0 8.0

Old Row: 4.0 7.0 10.0 13.0

Still stressed from student homework?
Get quality assistance from academic writers!