-8

"3. Write a method which takes in three real numbers a, b and c representing the coefficients of a quadratic equation ax2 + bx + c and uses the quadratic formula to determine one solution of the equation ax2 + bx + c = 0. Use an if statement to ensure that the program works correctly for all numeric input."


this is my code however when i input numbers i get the answer of 0

public int quadraticEquation(int a, int b, int c)
{
    if (a <= 0 && b<=0 && c<=0){
    System.out.println("Must be a real number");
    } else if(
    (a*(x^2))+(b*x)+c==0);
    return x;
}
user2705929
  • 9
  • 1
  • 1
  • Where have declared value for `x`? Seems you have declared with value 0 and not assigning anything to it and it is returning 0. Can you post relevant code? – Pradeep Simha Aug 22 '13 at 04:46
  • 1
    First of all, why are you checking to see if a, b and c are negative and then printing they are not real? All 3 quadratic coefficients may be negative. – Kon Aug 22 '13 at 04:47
  • 1
    In Java, `x^2` is `x XOR 2` not `x raised to the power 2`. – JavaNewbie_M107 Aug 22 '13 at 04:56

4 Answers4

5

Multiple problems here:

  • your check for the solution being real is flawed. -x^2 - 2x - 1 has all negative coefficients, yet its solution is real.

  • I don't see x declared anywhere, does this compile at all?

  • x^2 is NOT "x squared", but "x bitwise XOR 2". You should learn Java before doing anything further.

  • You have a stray semicolon after your else if here: (a*(x^2))+(b*x)+c==0);

  • You have to be extremely lucky for this to return x.

  • The assignment asks you to input three real numbers, but you are declaring the arguments as int. That's not good.

  • The correct formulae for computing the solutions are:

    enter image description here

All in all, you want something like this:

public double solveQuadratic(double a, double b, double c)
{
    double D = b * b - 4 * a * c;
    if (D < 0)
        throw new RuntimeException(); // complex solution

    return (-b + Math.sqrt(D)) / (2 * a);
}

The code above is untested, I don't speak Java, just gathered stuff together from Google.

Edit: Tested here, and it seems to give the same solution as WolframAlpha.

0

You need to use the quadratic formula to solve for the zeroes, ie

(-b +- sqrt(b^2 - 4ac)) / 2a

I won't do this problem for you, but I'll point you in the right direction. All you need to know is that you can compute a square root with the Math.sqrt() function. You have all the other variables

int numerator = -b + Math.sqrt(b*b - 4*a*c);
int denominator = 2*a

int result = numerator / denominator; //(this is integer division, I hope you understand)

Now just put it into code.

Kon
  • 10,702
  • 6
  • 41
  • 58
0

First of all there are two roots(values for x) in quadratic equation. you have to find those two. That roots can be either real roots or imaginary roots based on b*b-4*a*c positive or negative. Since solution for a*x^2+b*x+c=0, is x=[-b+squareroot(b^2-4*a*c)]/2*a or x=[-b-squareroot(b^2-4*a*c)]/2*a. So there are no real values if b^2-4*a*c<0. You should learn that first. follow this link.

You can do something like this

public static void main(String[] args) {
    Double[] result = quadraticEquation(4, 8, 1);
    if(result==null){
        System.out.println("There is no real roots for x");
    }else{
        for(Double d:result){
            System.out.println("value for x:" +d);
        }
    }

}

public static Double[] quadraticEquation(int a, int b, int c) {
    Double[] arr = new Double[2];
    if ((b*b - (4 * a * c)) < 0) {
        return null;
    } else {
        arr[0] = (-b + Math.sqrt((b*b - 4 * a * c))) / 2 * a;
        arr[1] = (-b - Math.sqrt((b*b - 4 * a * c))) / 2 * a;
    }
    return arr;
}

I don't know why you want only one solution. If you really want one you can do as follows.

  public static void main(String[] args) {
    Double result = quadraticEquation(4, 8, 1);
    if(result==null){
        System.out.println("There is no real roots for x");
    }else{           
            System.out.println("a value for x:" +result);           
    }

}

public static Double quadraticEquation(int a, int b, int c) {
    Double val =0.0;
    if ((b*b - (4 * a * c)) < 0) {
        return null;
    } else {
        val = (-b + Math.sqrt((b*b - 4 * a * c))) / 2 * a;

    }
    return val;
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
-1

so you are returning x. Where is x being set? It is not in this code.

Maybe you are wanting something like:

x = (a*(x^2))+(b*x)+c;
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64