In this article we will learn about some of the frequently asked TypeScript programming questions in technical like “public class QuadraticEquation { public static Roots findRoots” Code Answer. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on typescript was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Typescript. Below are some solution about “public class QuadraticEquation { public static Roots findRoots” Code Answer.
public class QuadraticEquation { public static Roots findRoots
xxxxxxxxxx
public class QuadraticEquation {
public static Roots findRoots(double a, double b, double c) {
Roots root = new Roots(0,0);
double root1, root2;
if(a==0 ||b==0||c==0) return root;
// calculate the determinant (b2 - 4ac)
double determinant = (b * b) - (4 * a * c);
if (determinant > 0) {
// two real and distinct roots
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
// System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
} // check if determinant is equal to 0
else if (determinant == 0) {
// two real and equal roots then determinant is equal to 0
// so -b + 0 == -b
root1 = -b / (2 * a);
root2 = root1;
} else {
// roots are complex number and distinct
root1= -b / (2 * a);
root2 = Math.sqrt(-determinant) / (2 * a);
}
root = new Roots(root1,root2);
return root;
}
public static void main(String[] args) {
Roots roots = QuadraticEquation.findRoots(2, 10, 8);
System.out.println("Roots: " + roots.x1 + ", " + roots.x2);
//check for equal roots
roots = QuadraticEquation.findRoots(1, -18, 81);
System.out.println("Roots: " + roots.x1 + ", " + roots.x2);
}
}
class Roots {
public final double x1, x2;
public Roots(double x1, double x2) {
this.x1 = x1;
this.x2 = x2;
}
}