Arcadiaen
Arcadiaen

Reputation: 33

Quadratic Equation Java Class

I'm working in java for school and so far I've had some trouble wrapping my head around classes and the homework problems for them. I have the following criteria for a Quadratic Equation class:

Homework Problem Here

So far I have:

private static double coefA;
private static double coefB;
private static double coefC;


public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter the a, b and c for a Quadratic: ");
    coefA = input.nextDouble();
    coefB = input.nextDouble();
    coefC = input.nextDouble();

    double discriminant = getDiscriminant();

    if (discriminant < 0)
    {
        System.out.println("There are no real roots.");

    }
    else if (discriminant == 0)
    {
        System.out.println("The one root is: "+getRoot1());
    }
    else
    {
        System.out.println("The first root is: "+getRoot1());
        System.out.println("The second root is: "+getRoot2());
    }

}
//Construct
public QuadraticEquation(double a, double b, double c)
{
    coefA = a;
    coefB = b;
    coefC = c;
}

private static double getDiscriminant()
{
    double discriminant = (coefB * coefB) - (4 * coefA * coefC);
    return discriminant;
}
static double getRoot1()
{
    double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ 2 * coefA;
    return root1;

}
static double getRoot2()
{
    double root2 = (-coefB - Math.sqrt(getDiscriminant()))/ 2 * coefA;
    return root2;

}
}

The equation doesn't work and I don't even think I fit the criteria, but I don't completely understand what the book is asking. Can anyone assist?

Upvotes: 1

Views: 2198

Answers (2)

drowny
drowny

Reputation: 2147

Your mathematical equation's implemantation is correct but you have to put into brackets. Forexample ;

double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ 2 * coefA;

Here (-coefB + Math.sqrt(getDiscriminant())) equations is dividing by 2. After that multiplication with coefA. Be carefull. Change your logic with this example ;

double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ (2 * coefA);

So apply to other fields to get correct result.

Your two method should be changed.

static double getRoot1()
{
    double root1 = (-coefB + Math.sqrt(getDiscriminant()))/ (2 * coefA);
    return root1;

}
static double getRoot2()
{
    double root2 = (-coefB - Math.sqrt(getDiscriminant()))/ (2 * coefA);
    return root2;

}

In more detail, operator precedence table ;

Operators                                       Precedence              Associativity
postfix increment and decrement                 ++ --                   left to right
prefix increment and decrement, and unary       ++ -- + - ~ !           right to left
multiplicative                                  * / %                   left to right
additive                                        + -                     left to right

Upvotes: 1

FredK
FredK

Reputation: 4084

This is not a good design for a QuadraticEquation class. The three variables (coefA, coefB, coefC) should be instance variables, not static. You should have a constructor that takes the three values as inputs. It is much more efficient for the constructor to call getDiscriminant() and calculate the two possible answers, and use getters to retrieve them.

Upvotes: 0

Related Questions