Question: Quadratic Equation Class: Design a class named QuadraticEquation for a quadratic equation ax 2 + bx + c = 0. The class contains: Private data
Quadratic Equation Class:
Design a class named QuadraticEquation for a quadratic equation ax2 + bx + c = 0. The class contains:
Private data fields a, b, and c that represent the coefficients.
A constructor with arguments (e.g. newA, newB, and newC) for a, b, and c.
Three get methods (e.g. getA, getB, getC) 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:
r1 = (-b+(b^2-4ac))/2a and r2 = (-b-(b^2-4ac))/2a
These methods are useful only if the discriminant is non-negative. Let these methods return 0 if the discriminant is negative.
Write a test program (TestQuadraticEquation) that requests the user to enter three integer coefficients (a, b, and c). Use data type int for these local variables and display the results 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 roots. Use \u00B2 to display the superscript 2. At the end of your program, use the Date class from the java.util library that creates a Date object and displays the date and time using the toString() method.
Example equations: Extract coefficients, then evaluate discriminant (b2 - 4ac) to determine the number of roots.
x2 + 5x + 6 = 0
Discriminant: b2 - 4ac
52-(4*1*6)=1 (2 roots)
Factoring:
(x + 2)(x + 3)
x+2=0 x+3=0
x=-2 x=-3
Roots are -2,-3
Sample runs:
Enter three coefficients:
1 5 6
For: 1x + 5x + 6 = 0
Roots are -2.0 and -3.0
Sat Apr 18 15:29:00 EDT 2015
Sample Algorithms:
TestQuadraticEquation Class:
Get 3 coefficients from the console
Create QuadraticEquation object with the 3 coefficients
If discriminant is < 0, put No roots to the console
If discriminant is = 0, call instance method to calculate the root and put it to the console.
If discriminant is > 0, call instance methods getRoot1 and getRoot2 to calculate the 2 roots and put them to the console.
Create Date object for current date and put it to the console using the toString method.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
