Java Program to Compute the Volume of a Tetrahedron Using Determinants

This is a Java Program to compute volume of tetrahedron using determinants. Call the four vertices of the tetrahedron (a, b, c), (d, e, f), (g, h, i), and (p, q, r). Now create a 4-by-4 matrix in which the coordinate triples form the columns of the matrix, with a row of 1’s appended at the bottom:
a d g p
b e h q
c f i r
1 1 1 1
The volume of the tetrahedron is 1/6 times the absolute value of the matrix determinant. For any 4-by-4 matrix that has a row of 1’s along the bottom, you can compute the determinant with a simplification formula that reduces the problem to a 3-by-3 matrix
a-p d-p g-p
b-q e-q h-q
c-r f-r i-r

Here is the source code of the Java Program to Compute the Volume of a Tetrahedron Using Determinants. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to find the volume of tetrahedron using a method of determinant

importjava.util.Random;

publicclass Volume_Tetrahedron_Determinants

{

publicstaticdouble determinant(double A[][], int N)

{

double det =0;

if(N ==1)

{

det = A[0][0];

}elseif(N ==2)

{

det = A[0][0]* A[1][1]- A[1][0]* A[0][1];

}else

{

det =0;

for(int j1 =0; j1 < N; j1++)

{

double[][] m =newdouble[N -1][];

for(int k =0; k <(N -1); k++)

{

m[k]=newdouble[N -1];

}

for(int i =1; i < N; i++)

{

int j2 =0;

for(int j =0; j < N; j++)

{

if(j == j1)

continue;

m[i -1][j2]= A[i][j];

j2++;

}

}

det +=Math.pow(-1.0, 1.0+ j1 +1.0)* A[0][j1]

* determinant(m, N -1);

}

}

return det;

}

publicstaticvoid main(String args[])

{

Random random =newRandom();

int x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4;

x1 = random.nextInt(10);

x2 = random.nextInt(10);

x3 = random.nextInt(10);

x4 = random.nextInt(10);

y1 = random.nextInt(10);

y2 = random.nextInt(10);

y3 = random.nextInt(10);

y4 = random.nextInt(10);

z1 = random.nextInt(10);

z2 = random.nextInt(10);

z3 = random.nextInt(10);

z4 = random.nextInt(10);

double[][] mat =newdouble[4][4];

mat[0][0]= x1;

mat[0][1]= x2;

mat[0][2]= x3;

mat[0][3]= x4;

mat[1][0]= y1;

mat[1][1]= y2;

mat[1][2]= y3;

mat[1][3]= y4;

mat[2][0]= z1;

mat[2][1]= z2;

mat[2][2]= z3;

mat[2][3]= z4;

mat[3][0]=1;

mat[3][1]=1;

mat[3][2]=1;

mat[3][3]=1;

System.out

.println("The matrix formed by the coordinates of the tetrahedron is: ");