-1

My returning values are not coming up as right, and I'm just wondering what I am doing wrong, my code looks fine?!

some of the test that are coming up are:

Test failed: expected was not equal to actual

Expected = 9.000000 and actual = 0.000000

The discriminant is not being computed correctly

Test failed: expected was not equal to actual

Expected = -3.000000 and actual = 0.000000

public class QuadraticEquation {

//coefficients
private double a;
private double b;
private double c;
// created a discriminant instance variable so it's easier to access
// rather than tying out "Math.pow(b, 2) - 4 * a * c" every time
// the discriminant is required
private double discriminant = Math.pow(b, 2) - (4 * a * c);

//constructor
public QuadraticEquation(double a, double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

//getter for A
public double getA() {
    return a;
}

//getter for B
public double getB() {
    return b;
}

//getter for C
public double getC() {
    return c;
}

// get discriminant
// the discriminant is inside of the square root,
//  and b^2 - 4ac is the expression for the discriminant
public double getDiscriminant() {
    //b^2 - 4ac
    return discriminant;
}

//get root0
public double getRoot0(){
    if (discriminant > 0) {
        return -b + Math.sqrt(discriminant) / 2*a;
    }
    return Double.NaN;
}

public double getRoot1() {
    if (discriminant > 0) {
        return -b - Math.sqrt(discriminant) / 2*a;
    }
    return Double.NaN;
}
}
Justin Rose
  • 1,165
  • 2
  • 10
  • 6
  • 4
    possible duplicate of [Are fields initialized before constructor code is run in Java?](http://stackoverflow.com/questions/14805547/are-fields-initialized-before-constructor-code-is-run-in-java) - see especially the [second answer](https://stackoverflow.com/a/14806340/3614835) and apply it to your `discriminant` variable. – Jeffrey Bosboom Jan 12 '15 at 03:09
  • _"created a discriminant instance variable so it's easier to access rather than tying out "Math.pow(b, 2) - 4 * a * c" every time "_ . Why not just put the equation `return Math.pow(b, 2) - 4 * a * c;` inside method?..Okay, I know you just want to do it in a challenging way :) – DnR Jan 12 '15 at 03:14
  • 1
    @DnR Presumably because this is what the teacher has told them to do. – Dawood ibn Kareem Jan 12 '15 at 03:16
  • @DavidWallace ah of course – DnR Jan 12 '15 at 03:17

1 Answers1

5

You're trying to calculate the discriminant before you've set a, b and c, so naturally it's coming out zero.

Move the line that calculates the discriminant down into the constructor.

public class QuadraticEquation {

    //coefficients
    private double a;
    private double b;
    private double c;
    private double discriminant;

    //constructor
    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
        discriminant =  b * b - (4 * a * c);
    }

    // etc.

Note also that b * b generally outperforms Math.pow(b, 2) because of the way the latter is calculated.

Also, the operations inside getRoot0 and getRoot1 are happening in the wrong order. You need parentheses to do something like this.

public double getRoot0(){
    if (discriminant > 0) {
        return ( -b + Math.sqrt(discriminant)) / (2 * a);
    }
    return Double.NaN;
}

and similarly in getRoot1.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • In other words, Java is an [imperative programming](http://en.wikipedia.org/wiki/Imperative_programming) language, *not* a [functional programming](http://en.wikipedia.org/wiki/Functional_programming) language. – gknicker Jan 12 '15 at 03:17