0

Can someone please help to tell why I'm getting zero in my arithmetic operations? I'm trying to solve this arithmetic problem which gives value of x and y but it's giving me java.lang.arithmeticException error or showing zero result. It will be really helpful to me.

this is my input a=6,b=10,c=8,d=12,e=800,f=900

**This linear equation to obtain the values for x and y can be solved using

x = (ed -fb)/(ad -bc), y = (fa -ec)/(ad – bc)**

this is the problem that i'm trying to solve.

public class linearequation {
    
    public static void main(String[] args){
        Scanner scn= new Scanner(System.in);
        linear lin1 = new linear(scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt());
        if(lin1.isSolvable()) {
            System.out.println(lin1.getx());
            System.out.println(lin1.gety());
        }else {
            System.out.println("No Solution");
        }
    }
}
class linear {
    private int a, b, c, d, e, f;
    int x, y;
    int den = ((a * d) - (b * c));

    public linear(int na, int nb, int nc, int nd, int ne, int nf) {
        na = a;
        nb = b;
        nc = c;
        nd = d;
        ne = e;
        nf = f;

    }

    public int geta() {
        return a;
    }

    public int getb() {
        return b;
    }

    public int getc() {
        return c;
    }

    public int getd() {
        return d;
    }

    public int gete() {
        return e;
    }

    public int getf() {
        return f;
    }

    public int getx() {
        return x = ((e * d) - (f * b)) / den;
    }

    public int gety() {
        return y = ((f * a) - (e * c)) / den;
    }


    public boolean isSolvable() {

        if (den <= 0) {
            return false;
        } else {

            return true;
        }
    }

}```
7
  • ArithmeticException means you try to divide by zero. Right ? Commented Feb 15, 2021 at 7:44
  • Can you provide the full error message? I think you are trying to divide by 0. Commented Feb 15, 2021 at 7:44
  • den is predefined to be 0 since it is calculated before the constructor can assign values to a, b, c, d, e, and f. You need to set it after assigning the values in the constructor. Commented Feb 15, 2021 at 7:45
  • no no my input is a=6,b=10,c=8,d=12,e=800,f=900 @g.momo Commented Feb 15, 2021 at 7:46
  • my input is a=6,b=10,c=8,d=12,e=800,f=900 @tibetiroka Commented Feb 15, 2021 at 7:49

1 Answer 1

2

As far as I can see your constructor for linear is the problem.

You're passing na, nb, nc, nd, ne, nf into it and reassign them to be the value of a, b, c, d, e, and f, respectively, when you actually want to assign the other way around, e.g. a = na instead of na = a.

Also, you're setting den before the values for a...f are set by way of the constructor. den is never reassigned and thus stays 0.

This is what your constructor should look like:

public linear(int na, int nb, int nc, int nd, int ne, int nf) {
    a = na;
    b = nb;
    c = nc;
    d = nd;
    e = ne;
    f = nf;
    
    den = ((a * d) - (b * c));
}
Sign up to request clarification or add additional context in comments.

2 Comments

brother if i put den in constructor then is there any way to use the same den in getx?
You'll have to define den as an instance field to the class linear. You've implicitly done this in your provided code example by this line: int den = ((a * d) - (b * c));. Reduce this line to be int den; and you'll have yourself a field den on your class linear which can be used in the class' instances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.