0

I need to implement the quadratic formula and my method is expecting a double array and should return a int array with the exact solution, that is, when the solution are two numbers the int array should be two fields long, when there is just one solution number the int array should be one field long and when there is no solution the int array should be empty.

I am quiet new at java and have a problem with returning the solution. Eclipse sais: "quadsol cannot be resolved to a variable." Can you help me please - where is my mistake?

This is my code:

 public static int[] quadraticFormula (double[] coefficients) {
    int i = 0;
    int j = 0;
    double discriminant;
    double solution1;
    double solution2;
    int[] quadsol = new int[j];
    discriminant = (Math.pow(coefficients[i+1],2)-(4*coefficients[i]*coefficients[i+2]));
    if(discriminant >= 0) {
        solution1 = (((Math.sqrt(discriminant)) - (coefficients[i+1]))/2*coefficients[i]);
        solution2 = ((-(Math.sqrt(discriminant)) - (coefficients[i+1]))/2*coefficients[i]);
        if(solution1 == solution2) {
            quadsol[j] = (int) solution1;
        } else {
            quadsol[j] = (int) solution1;
            quadsol[j+1] = (int) solution2;
        }
    } else {

        quadsol[j] = 0;
    }

    return quadsol;
Z.Wo
  • 101
  • 2
  • 10

0 Answers0