Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Quadratic Equation. one equation doesnt work

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Quadratic Equation. one equation doesnt work

    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();
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Quadratic Equation. one equation doesnt work

    With many other equations work, BUT with this one NOT.

    How are you trying to debug the code? Add some print statements that print out the values of the variables as the code executes. The printed values will show you what the program is doing and help you find the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Snouman (November 2nd, 2019)

  4. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Quadratic Equation. one equation doesnt work

    I understood. The problem was in brackets.
    Should be
    result1 = ( (-getB() + sqrt(discr)) / (2.0*getA()) );
    result2 = ( (-getB() - sqrt(discr)) / (2.0*getA()) );

Similar Threads

  1. Solving the Quadratic Equation
    By airac13 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2023, 12:51 PM
  2. [SOLVED] Why raiseSalary equation can't work? empolyee.java
    By kennylty in forum Object Oriented Programming
    Replies: 2
    Last Post: March 7th, 2014, 07:56 AM
  3. A couple of issues with my quadratic equation solver.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 10th, 2013, 08:52 PM
  4. quadratic equation solver help!
    By overlord in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 20th, 2011, 11:39 AM
  5. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 07:27 PM