Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java Construct the class of non-negative 3×3 Matrices containing non-negat

ID: 3917441 • Letter: U

Question

Using Java

Construct the class of non-negative 3×3 Matrices containing non-negative double elements with the following functionalities:

Determinant method returns the double value of the matrix determinant:

isNull method returns true if the matrix is a Null Matrix (all the elements are 0). Otherwise, isNull method returns false.
isUMatrix method returns true if the matrix is a U Matrix. Otherwise the return value is false. Let

Multiplication results in a 3×3 matrix and takes a constant of double type as the only input parameter. The resulting matrix R is:

Addition method returns a 3×3 sum of two matrices (it takes the addend matrix as an input parameter). The resulting matrix R is:

Implement the member methods of class of 3×3 matrices with the functionalities above.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Matrix.java
------

public class Matrix {
private double[][] data;

public Matrix(){
data = new double[3][3];
}
public Matrix(double[][] values){
data = new double[3][3];
for(int i = 0; i < 3; i++)
for(int j =0 ; j < 3; j++)
data[i][j] = values[i][j];
}


public double determinant(){
return data[0][0] * (data[1][1] * data[2][2] - data[1][2] * data[2][1])
+ data[0][1] * (data[1][2] * data[2][0] - data[1][0] * data[2][2])
+ data[0][2] * (data[1][0] * data[2][1] - data[1][1] * data[2][0]) ;

}

public boolean isNull(){
for(int i = 0; i < 3; i++)
for(int j =0 ; j < 3; j++)
{
if(data[i][j] != 0)
return false;
}

return true;
}
public boolean isUMatrix(){
for(int i = 0; i < 3; i++)
for(int j =0 ; j < 3; j++)
{
if(data[i][j] != 1)
return false;
}

return true;
}

public Matrix multiply(double d){
Matrix result = new Matrix();
for(int i = 0; i < 3; i++)
for(int j =0 ; j < 3; j++)
{
result.data[i][j] = d * data[i][j];
}
return result;
}

public Matrix add(Matrix b){
Matrix result = new Matrix();
for(int i = 0; i < 3; i++)
for(int j =0 ; j < 3; j++)
{
result.data[i][j] = data[i][j] + b.data[i][j];
}
return result;
}

public double get(int i, int j){
return data[i][j];
}

public void set(int i, int j, double val){
data[i][j] = val;
}

public void print(){
for(int i = 0; i < 3; i++){
for(int j =0 ; j < 3; j++)
{
System.out.printf("%7.2f", data[i][j]);
}
System.out.println();
}
System.out.println();

}
}


TestMatrix.java
------

public class TestMatrix {
public static void main(String[] args) {
double[][] val ={ {1, 2, 3}, {4, 5 , 6}, {1, 1, 3}} ;
Matrix A = new Matrix(val);
System.out.println("Matrix A");
A.print();
System.out.println("A.determinant() = " + A.determinant());
System.out.println("A.isNull() = " + A.isNull());
System.out.println("A.isUMatrix() = " + A.isUMatrix());

Matrix B = A.multiply(2);
System.out.println("Matrix B = 2*A");
B.print();

Matrix C = A.add(B);
System.out.println("Matrix C = A + B");
C.print();


}
}


output
----
Matrix A
1.00 2.00 3.00
4.00 5.00 6.00
1.00 1.00 3.00

A.determinant() = -6.0
A.isNull() = false
A.isUMatrix() = false
Matrix B = 2*A
2.00 4.00 6.00
8.00 10.00 12.00
2.00 2.00 6.00

Matrix C = A + B
3.00 6.00 9.00
12.00 15.00 18.00
3.00 3.00 9.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote