Home > OS >  Cramer's Rule Determinant return 0 why?
Cramer's Rule Determinant return 0 why?

Time:11-02

I can't seem to see what the problem is, but the getDeterminant() always return 0 when it shouldn't. Yes it sometimes occurs but not continuously. I've tried replacing double with float, same result. Here's the code:

import java.util.Scanner;
class CramersRule{
    //Numbers for the 3-Variable Linear Equation.
    private double a1, a2, a3;
    private double b1, b2, b3;
    private double c1, c2, c3;
    private double d1, d2, d3;
    
    CramersRule(){
        
    }
    //Sets the 1st Linear Equation.
    public void setLinearEquation1 (double a1, double b1, double c1, double d1){
        this.a1 = a1;
        this.b1 = b1;
        this.c1 = c1;
        this.d1 = d1;
    }
    //Sets the 2nd Linear equation.
    public void setLinearEquation2 (double a2, double b2, double c2, double d2){
        this.a2 = a2;
        this.b2 = b2;
        this.c2 = c2;
        this.d2 = d2;
    }
    //Sets the 3rd Linear Equation.
    public void setLinearEquation3 (double a3, double b3, double c3, double d3){
        this.a3 = a3;
        this.b3 = b3;
        this.c3 = c3;
        this.d3 = d3;
    }
    /*Returns the 3x3 Determinant */
    public double getDeterminant (){
        double d = a1 * ((b2 * c3) - (b3 * c2)) - a2 * ((b1 * c3) - (b3 * c1))   a3 * ((b1 * c2) - (b2 * c1));
        return d;
    }
    /*Returns the 3x3 Determinant for x */
    public double getDx(){
        double x = d1 * ((b2 * c3) - (b3 * c2)) - b1 * ((d2 * c3) - (d3 * c2))   c1 * ((d2 * b3) - (d3 * b2));
        return x;
    }
    /*Returns the 3x3 Determinant for y */
    public double getDy(){
        double y = a1 * ((d2 * c3) - (d3 * c2)) - d1 * ((a2 * c3) - (a3 * c2))   c1 * ((a2 * d3) - (a3 * d2));
        return y;
    }
    /*Returns the 3x3 Determinant for z */
    public double getDz(){
        double z = a1 * ((b2 * d3) - (b3 * d2)) - b1 * ((a2 * d3) - (a3 * d2))   d1 * ((a2 * b3) - (a3 * b2));
        return z;
    }
}

public class MyProgram{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        CramersRule CR1 = new CramersRule();
        System.out.print("Enter 4 numbers for the first equation (ie. 1 2 3 4): ");
        CR1.setLinearEquation1(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        CramersRule CR2 = new CramersRule();
        System.out.print("Enter 4 numbers for the second equation (ie. 1 2 3 4): ");
        CR2.setLinearEquation2(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        CramersRule CR3 = new CramersRule();
        System.out.print("Enter 4 numbers for the third equation (ie. 1 2 3 4): ");
        CR3.setLinearEquation3(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        
        System.out.println("The answer of the 3x3 Determinant is "   CR1.getDeterminant());
        if(CR1.getDeterminant() == 0){
            System.out.println("Cramers Rule does not apply.");
        }else{
            double x = CR1.getDeterminant() / CR1.getDx();
            double y = CR1.getDeterminant() / CR1.getDx();
            double z = CR1.getDeterminant() / CR1.getDx();
            
            System.out.println("The solution set is ("   x   ", "   y   ", "   z   ")");
        }
    }
}

CodePudding user response:

You must use a single CramersRule instance in your main method (and extract the correct values from it):

public class MyProgram{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        CramersRule CR = new CramersRule();
        System.out.print("Enter 4 numbers for the first equation (ie. 1 2 3 4): ");
        CR.setLinearEquation1(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        System.out.print("Enter 4 numbers for the second equation (ie. 1 2 3 4): ");
        CR.setLinearEquation2(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        System.out.print("Enter 4 numbers for the third equation (ie. 1 2 3 4): ");
        CR.setLinearEquation3(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        
        System.out.println("The answer of the 3x3 Determinant is "   CR.getDeterminant());
        if (CR.getDeterminant() == 0){
            System.out.println("Cramers Rule does not apply.");
        } else {
            double x = CR.getDeterminant() / CR.getDx();
            double y = CR.getDeterminant() / CR.getDy();
            double z = CR.getDeterminant() / CR.getDz();
            
            System.out.println("The solution set is ("   x   ", "   y   ", "   z   ")");
        }
    }
}
  • Related