Quadratic Equation Class: Design a class named QuadraticEquation for a quadratic
ID: 3693950 • Letter: Q
Question
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.
Explanation / Answer
//add imports
import java.util.Date;
import java.util.Scanner;
//class for methods
class QuadraticEquation
{
private double a,b,c; //declaring private variables
QuadraticEquation(double a,double b,double c)//calling variables into constructor
{
this.a = a;
this.b = b;
this.c = c;
}
//implementing getter methods
double getA()
{
return a;
}
double getB()
{
return b;
}
double getC()
{
return c;
}
//method to calculate discriminant
double getDiscriminant()
{
double sq,res;
sq = b*b;
res= sq - (4*a*c);
return res;
}
//methods to calculate roots
double getRoot1()
{
double sum,res;
sum = getDiscriminant();
sum = sum - b;
res = sum /(2*a);
return res;
}
double getRoot2()
{
double sum,res;
sum = getDiscriminant();
sum = sum + b;
res = sum /(2*a);
return res;
}
}
public class TestQuadraticEquation
{
public static void main(String args[])
{
int a,b,c;
//input values from user
Scanner s = new Scanner(System.in);
System.out.println("Enter three coefficients:");
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
Double x,y,z;//convert them to double
x = (double)a;
y = (double)b;
z = (double)c;
QuadraticEquation e = new QuadraticEquation(x,y,z);
Double d = e.getDiscriminant();//get the value of discriminant
System.out.println("For: "+a+"x^2 +"+b+"x +"+c+" =0");
if(d<0)
System.out.println("no roots");
else if(d == 0)
{
Double root = e.getRoot1();
System.out.println(" Root is "+root);
}
else
{
Double root1,root2;
root1 = e.getRoot1();
root2 = e.getRoot2();
System.out.println(" Roots are "+root1+" and "+root2);
}
//print current system time and date
Date date = new Date();
System.out.println(date.toString());
}
}
Output:
Enter three coefficients:
1 5 6
For: 1x^2 +5x +6 =0
Roots are -2.0 and 3.0
Mon Apr 25 23:17:57 IST 2016