With many other equations work, BUT with this one NOT.
Have no idea why )
Result shoud be -0.5 and -1/3(in double)
BUT console shows me -18.0 and -12.0
Here my class
package task1; //ax^2 + bx + c = 0 import java.util.Scanner; import static java.lang.Math.*; public class QuadraticEquation { private double a; private double b; private double c; private double discriminant; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } private double getDiscriminant() { this.discriminant = pow(getB(),2.0) - 4*getA()*getC(); return discriminant; } /* public double scanDouble(){ Scanner scanner = new Scanner(System.in); double number = scanner.nextDouble(); return number; } */ private void showEquation(){ System.out.println("("+getA()+")x^2 +("+getB() + ")x + (" + getC() + ") = 0"); } public void findResult() { showEquation(); System.out.println("Розв'язки рівняння: "); double discr = getDiscriminant(); if(discr < 0.0){ System.out.println("x1 = "); System.out.println("x2 = "); } else if(discr == 0.0){ double result = -getB() / 2.0*getA(); System.out.println("x1 = " + result); System.out.println("x2 = " + result); } else { double result1 = ((double)-getB() + sqrt(discr) ) / 2.0*getA(); double result2 = ((double)-getB() - sqrt(discr) ) / 2.0*getA(); System.out.println("x1 = " + result1); System.out.println("x2 = " + result2); } } }
Here my class-tester
package task1; public class TestQE { public static void main(String[] args){ QuadraticEquation qe1 = new QuadraticEquation(); qe1.setA(-6); qe1.setB(-5); qe1.setC(-1); qe1.findResult(); } }