Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class called QuadraticEquation.java for a quadratic equation ax 2 + bx

ID: 3768177 • Letter: D

Question

Design a class called QuadraticEquation.java for a quadratic equation ax2 + bx + x = 0. The class contains:

Private data fields a, b, and c that represent three coefficients.

A constructor for the arguments for a, b, and c.

Three getter methods for a, b, and c.

A method named getDiscriminant() that returns the discriminant, which is b2 - 4ac.

The methods named getRoot1() and getRoot2() for returning two roots of the equation

These methods are useful only if the discriminant is non-negative. These methods should return 0 if the discriminant is negative.

Write a test program called TestQuadraticEquation.java that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display "The equation has no real roots."

Here are some sample outputs:

Explanation / Answer

import java.util.Scanner;

class QuadraticEquation

{

double a,b,c;

QuadraticEquation(double x,double y,double z)

{

a=x;b=y;c=z;

}

double getDiscriminant()

{

double discriminant = Math.pow(b, 2) - (4 * a * c);

return (discriminant);

}

double getRoot1(double discriminant)

{

if(discriminant<0)

return(0.0);

double root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a);

return(root1);

}

double getRoot2(double discriminant)

{

if(discriminant<0)

return(0.0);

double root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a);

return(root2);

}

}

class TestQuadraticEquation

{

public static void main(String args[])

{

double a, b, c;

Scanner scan = new Scanner (System.in);

System.out.print ("Enter a: ");

   a = scan.nextDouble();

   System.out.print ("Enter b: ");

   b = scan.nextDouble();

   System.out.print ("Enter c ");

   c = scan.nextDouble();

QuadraticEquation qd=new QuadraticEquation (a,b,c);

double disc=qd.getDiscriminant();

//System.out.println ("The discriminant is: "+disc);

double r1=qd.getRoot1(disc);

double r2=qd.getRoot2(disc);

if(disc>0)

System.out.println("there are 2 solutions:"+r1+" and"+r2);

else if(disc==0)

System.out.println("the equation has one root:"+r1);

else

System.out.println("the equation has no real roots");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote