This program is based upon the quadratic equation. If I enter a negative discriminant it prints NaN instead of "No real solutions". Any help will be appreciated. Why is it printing NaN twice??? What modifications are needed in the code? Please help!!!
import java.util.Scanner;
class QE{
private double d=0;
private double e=0;
private double f=0;
private double disc=0;
private double sol1=0;
private double sol2=0;
public QE(double a, double b, double c)
{d=a;e=b;f=c;
disc=e*e-4*d*f;
}
public boolean hasSolutions(){
return d<0;
}
public double getSolution1()
{sol1=(-e-Math.sqrt(disc))/2*d;
return sol1;
}
public double getSolution2()
{sol2=(-e+Math.sqrt(disc))/2*d;
return sol2;
}
}
public class QuadraticEquation {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter value of 'a':");
double a=in.nextDouble();
System.out.println("Enter value of 'b':");
double b=in.nextDouble();
System.out.println("Enter value of 'c':");
double c=in.nextDouble();
QE qd=new QE(a,b,c);
if(qd.hasSolutions())
System.out.println("No real solutions");
else if(qd.getSolution1()==0 && qd.getSolution2()==0)
System.out.println(0+" No solutions exist");
else
{System.out.println(qd.getSolution1());
System.out.println(qd.getSolution2());
}
}
}