0

This question is for my intro to Java class and the question is asking me to use classes to solve a quadratic equation.

I am trying to fix my class so that it doesn't return NaN. I have used Math.abs() to try and fix any situation where the number under the radical would be a negative number, but I'm still getting NaN. Here is my code for the class:

    public class Quadratic
{
//Private data members
private double a;
private double b;
private double c;
private double posX;
private double negX;

//Deafault constructor
public void Quadratic()
{
    a = 1;
    b = 0;
    c = 0;
    posX = 0;
    negX = 0;
}

//The mutators
public void setQuad(Double alpha, double bravo, double charlie)
{
    alpha = a;
    bravo = b;
    charlie = c;
    getQuad();
}

//The accessors
public double getQuad()
{
    double temp = (Math.pow(b, 2) - (4 * a * c));//getting the number inside the root

    if(temp < 0)
        temp = Math.abs(temp);
    //ensures that the function can run until complex numbers are sorted

    posX = (-b + (Math.sqrt(temp)))/(2 * a);//calculates when added
    negX = (-b + (Math.sqrt(temp)))/(2 * a);//calculates when subtracted
//error: Keep getting NaN for answers, already accounted for negative inside the root
//       not a 0 in the descriminant.
    return 0;
}

//My toString which is what will be output at System.out.println(N)
public String toString()
{
    if(negX == posX)
        return "X = "+ negX;
    else
        return "X = "+ negX +" and "+ posX;
}
}

Is my math incorrect, or am I using the math utilities incorrectly?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
eecenja
  • 15
  • 5
  • Can you give us the `a`, `b` and `c` values? If they are all 0 you will get `NaN` – Peter Lawrey Jul 20 '16 at 14:22
  • 1
    What is the value of `a`? As a side note, you probably want `negX = (-b - ( ...` – bradimus Jul 20 '16 at 14:26
  • 1
    Don't use `Math.pow((b,2)` to square a number. Just use `b*b` – FredK Jul 20 '16 at 14:31
  • Why does your setQuad funtion throw away its arguments? and why does it call getQuad() and then throw away the result? and why does getQuad() always return (double) zero? And why does setQuad() always set posX and negX to the same value? – Solomon Slow Jul 20 '16 at 14:32
  • Reverse the operands of the assignments in `setQuad`: `a = alpha`, not `alpha = a`. – Andy Turner Jul 20 '16 at 14:38
  • a, b, and c are supposed to be inputs from the user from the main method. As far as getQuad() throwing away its arguments, I tried to get it to return negX and posX, but I couldn't get it to return 2 values. I'm not sure how to fix that. – eecenja Jul 20 '16 at 14:40

1 Answers1

0
  1. Your construct was assigning your fields to the local parameters in the constructor
  2. usually you would want to allow th ability to assign fields from constructor, thus i put one in
  3. Your negX assignment was the same as posX
  4. getQuad doesn't need to return anything with your implementation
  5. Your accesor getQuad wasn't really an accesor, it was more of a mutator changing posX and negX, implemented what accessors would be below

public class Quadratic { private double a; private double b; private double c; private double posX; private double negX;

//Default constructor
public Quadratic()
{
    //1.
    a = 0;
    b = 0;
    c = 0;
    posX = 0;
    negX = 0;
}

public Quadratic(double a, double b, double c){
    //2.
    this.a = a;
    this.b = b;
    this.c = c; 
    this.posX = 0;
    this.negX = 0;
}


//The mutators
public void setQuad(Double alpha, double bravo, double charlie)
{
   a = alpha;
   b = bravo;
   c = charlie;
   getQuad();
}

public void getQuad()
{
   //4.
    double temp = (Math.pow(b, 2) - (4 * a * c));//getting the number inside the root

    if(temp < 0)
        temp = Math.abs(temp);
    //ensures that the function can run until complex numbers are sorted

    posX = (-b + (Math.sqrt(temp)))/(2 * a);

    //3.
    negX = (-b - (Math.sqrt(temp)))/(2 * a);
}

//Accesors  5.
public double getA(){
    return this.a
}

public double getB(){
    return this.b
}

public double getC(){
    return this.c
}
//Overriding toString
public String toString()
{
    if(negX == posX)
        return "X = "+ negX;
    else
        return "X = "+ negX +" and "+ posX;
}

}

Javant
  • 1,009
  • 10
  • 17