Question

I'm not sure what I'm doing wrong in my code but I can't get it to run properly...the tester keeps returning NaN instead of what's supposed to be expected. The goal of this exercise is to print all real solutions to the quadratic equation. solution1 should return the smaller solution and solution2 should return the smaller solution. Here's my class.

   public class QuadraticEquation
   {
    private int a;
    private int b;
    private int c;
    private boolean hasSolutions;
    private double discriminant = (b * b) - (4 * a * c);
    private double solution1;
    private double solution2;
   /**
 * Constructs a quadratic equation
 * @param a coefficient a 
 * @param b coefficient b
 * @param c coefficient c
 */

    public QuadraticEquation(int a, int b, int c)
    {
        a = a;
        b = b;
        c = c;
    }
     /**
 * Checks if there is a solution
 * @return true if there is a real solution
 */

    public boolean hasSolutions()
    {
        if(discriminant < 0)
            hasSolutions = false;
        else
            hasSolutions = true;
        return hasSolutions;
    }
    /**
  * Returns first solution
  * @return first solution
  */

    public double getSolution1()
    {
        if(hasSolutions)
        {
            if((-b + Math.sqrt(discriminant) / (2 * a)) < (-b - Math.sqrt(discriminant) / (2 * a)))
                solution1 = (-b + Math.sqrt(discriminant) / (2 * a));
            else
                solution1 = (-b - Math.sqrt(discriminant) / (2 * a));
        }
            return solution1;
    }
    /**
    * Returns second solution
    * @return second solution
    */  

    public double getSolution2()
    {
        if(hasSolutions)
        {
            if((-b + Math.sqrt(discriminant) / (2 * a)) > (-b - Math.sqrt(discriminant) / (2 * a)))
                solution2 = (-b + Math.sqrt(discriminant) / (2 * a));
            else
                solution2 = (-b - Math.sqrt(discriminant) / (2 * a));
        }
            return solution2;
    }

  }

Here's my tester:

    public class QuadraticEquationTester
    {
    public static void main(String[] args)
      {
          QuadraticEquation equation = new QuadraticEquation(2, 2, -4);
          System.out.println(equation.hasSolutions());
          System.out.println("Expected: true");
          System.out.println(equation.getSolution1());
          System.out.println("Expected: -2");
          System.out.println(equation.getSolution2());
          System.out.println("Expected: 1");

          QuadraticEquation equation2 = new QuadraticEquation(2, 2, 4);
          System.out.println("\n" + equation2.hasSolutions());
          System.out.println("Expected: false");
          System.out.println(equation2.getSolution1());
          System.out.println("Expected: 0");
          System.out.println(equation2.getSolution2());
          System.out.println("Expected: 0");
        }
     }

Thank you so much! :)

Was it helpful?

Solution

  1. You are computing discriminant in the object initializing section of the class context which is being computed with default value of a, b, and c. Do not compute until initializing a,b, and c

  2. You are hiding variable in your constructor method as a, b, c are parameter fields. Use this:

    public QuadraticEquation(int a, int b, int c)
    {
      this.a = a;
      this.b = b;
      this.c = c;
    }
    
  3. The NaN results for floating point computation with an operation like deviding a zero by zero. SO find out such if still it happens with the above fix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top