Java Program In this lab, you will construct a class called QuadraticSolver that
ID: 3605377 • Letter: J
Question
Java Program
In this lab, you will construct a class called QuadraticSolver that will compute the real roots of a quadratic function (f(x) =ax^2 + bx +c ).
The class will implement the following methods:
A default constructor that initializes a legitimate quadratic function.
An overloaded constructor that takes the values of a, b, and c in the function f(x) = ax^2 + bx
+c . This method throws a QuadraticException in the case where the input does not
define a quadratic function.
A method that computes the discriminant ( b^2 - 4ac).
Boolean methods determining whether the solutions of the quadratic equation ax^2+ bx +c =
0 have real or non-real solutions.
Two methods, one for each real root of the equation ax^2+bx + c = 0. Each method will throw
a NonRealException when the root is a non-real value.
A toString method that (reasonably) prints the quadratic function.
The classes QuadraticException and NonRealException have been provided. Implement a main method in a separate class to test your implementation.
Explanation / Answer
QuadraticException.java
public class QuadraticException extends Exception {
QuadraticException(String mesg)
{
System.out.println(mesg);
}
}
_____________________
NonRealException.java
public class NonRealException extends Exception {
NonRealException(String mesg)
{
System.out.println(mesg);
}
}
________________________
QuadraticSolver.java
public class QuadraticSolver {
private int a;
private int b;
private int c;
public QuadraticSolver() {
super();
}
public QuadraticSolver(int a, int b, int c) throws QuadraticException {
super();
if (a == 0) {
throw new QuadraticException(
"** Coefficient X^2 must not be zero **");
} else {
this.a = a;
this.b = b;
this.c = c;
}
}
public boolean calDiscrimininant() {
boolean bool = false;
if (Math.pow(b, 2) - 4 * a * c < 0)
bool = false;
else if (Math.pow(b, 2) - 4 * a * c >= 0)
bool = true;
return bool;
}
public double findRoot1() throws NonRealException {
if (!calDiscrimininant())
throw new NonRealException("No Real Roots");
else
return ((-b + Math.pow(b, 2) - 4 * a * c) / (2 * a));
}
public double findRoot2() throws NonRealException {
if (!calDiscrimininant())
throw new NonRealException("No Real Roots");
else
return ((-b - Math.pow(b, 2) - 4 * a * c) / (2 * a));
}
@Override
public String toString() {
return "(" + a + ")x^+(" + b + ")x +" + c + " = 0";
}
}
_______________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int a = 0, b = 0, c = 0, num;
//Scanner class object is used read the inputs entered by the user
Scanner sc = new Scanner(System.in);
QuadraticSolver qs;
while (true) {
//Getting the number 'a' entered by the user
System.out.print(" Enter Number a:");
a = sc.nextInt();
//Getting the number 'b' entered by the user
System.out.print("Enter Number b:");
b = sc.nextInt();
//Getting the number 'c' entered by the user
System.out.print("Enter Number c:");
c = sc.nextInt();
try {
qs = new QuadraticSolver(a, b, c);
System.out.println("Root#1 :" + qs.findRoot1());
System.out.println("Root#2 :" + qs.findRoot2());
break;
} catch (QuadraticException e) {
continue;
} catch (NonRealException e) {
continue;
}
}
}
}
__________________
Output:
Enter Number a:0
Enter Number b:8
Enter Number c:8
** Coefficient X^2 must not be zero **
Enter Number a:3
Enter Number b:2
Enter Number c:5
No Real Roots
Enter Number a:1
Enter Number b:-3
Enter Number c:-4
Root#1 :14.0
Root#2 :5.0
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.