Create a class called Quadratic for representing a one-variable quadratic expression of the form:
ax2 + bx + c
a,b and c here are the coefficients.
The class should contain the following methods:
• A constructor that accepts values for a, b, and c.
• method getA()
• method getB()
• method getC()
• method evaluate (x) -- int
-will return the value of the expression at point x
• method discriminant()
- that will return (b2 – 4ac)
• method boolean isImaginaryRoots()
-roots are imaginary if (b2 – 4ac) < 0
• method boolean isRealRoots()
-roots are real if (b2 – 4ac) >= 0
// these methods can only be invoked if the roots are not imaginary
• method firstRoot() --> float
− b + b 2 − 4ac
x1 =
2a
• method secondRoot() --> float
− b − b 2 − 4ac
x2 =
2a
• method isPerfectSquare() --> boolean
// If the first and second roots are equal
Write a sample main program that will work as shown below.
Example run of the program: (Test Case No. 1)
Enter coefficient a: 4
Enter coefficient b: 4 Input from the user
Enter coefficient c: 1
Quadratic expression: 4x2 + 4x + 1
The roots are real: x1 = -0.5 ; x2 = -0.5 Output
It is a perfect square.
Evaluating the expression:
Enter x: 2 Input from the user
Result : 25 Output
Example run of the program: (Test Case No. 2)
Enter coefficient a: 2
Enter coefficient b: 5 Input from the user
Enter coefficient c: 10
Quadratic expression: 2x2 + 5x + 10 Output
The roots are imaginary.
Evaluating the expression:
Enter x: 1 Input from the user
Result : 17 Output
Example run of the program: Test Case No. 3)
Enter coefficient a: 1
Enter coefficient b: 0 Input from the user
Enter coefficient c: -1
Quadratic expression: x2 + 0x + -1
The roots are real. x1 = 1 ; x = -1 Output
It is not a perfect square.
Evaluating the expression:
Enter x: 5 Input from the user
Result : 24 Output