What is wrong with my code for quadratic formula -
public class quadraticeqn { private double a, b, c; public quadraticeqn(double x, double y, double z){ a=x; b=y; c=z; } private double disc = b*b-4*a*c; public boolean hassolutions(){ if(disc>=0) return true; else return false; } public double getsolutions1(){ return (-b-math.sqrt(disc))/(2*a); } public double getsolutions2(){ return (-b+math.sqrt(disc))/(2*a); } } import java.util.scanner; public class quadraticequationtest { public static void main(string[] args) { scanner values = new scanner(system.in); system.out.println("enter values a, b, c:"); double a=values.nextdouble(); double b=values.nextdouble(); double c=values.nextdouble(); quadraticeqn qe = new quadraticeqn(a, b, c); if (qe.hassolutions()) system.out.println(qe.getsolutions1()+" "+qe.getsolutions2()); else system.out.println("no real solutions"); } }
the main class supposed print out real solutions given inputs. have been stuck on problem hours. cant seem figure out did wrong. keep getting wrong answers. please!
you have issue
private double disc = (b*b)-(4*a*c);
right away when make quadraticeqn
varible disc
given value, however, it's given value before save a, b, , c. , creates disc
though a, b, , c each 0.
to fix it, update disc
in constructor.
public class quadraticeqn { private double a, b, c, disc; //we're created before constructor //runs default values of 0! public quadraticeqn(double x, double y, double z){ a=x; b=y; c=z; disc = b*b-4*a*c; //give me value after know others are! } public boolean hassolutions(){ if(disc>=0) return true; else return false; } public double getsolutions1(){ return (-b-math.sqrt(disc))/(2*a); } public double getsolutions2(){ return (-b+math.sqrt(disc))/(2*a); } }
Comments
Post a Comment