
Clarissa M.
asked 12/07/20NEED HELP ASAP JAVA CODE
Quadratic equations ax2 + bx + c = 0. Class consists of:
- private data fields a, b,c that represent 3 coeffiecients
- no org constructor
- 3 arg constructor
- 3 getter methods for a,b,c
- a method name GetDiscriminate() that returns the discriminant - b2-4ac
- method names getRoot1() and getROOT2() for returning 2 roots of the equations
EQUATIONS: r1 = -b + squareroot discriminant
—————————————
2a
r2 = -b + squareroot discriminant
—————————————
2a
Both method should return as 0 if discriminant is negative
- use Exception handling in all the setter methods
- method toString() - to represent the state of the object. If discriminant is positive, display two roots. If 0 then 1 root. if not, then "the equation has no real roots"
- method equals() - to compare 2 objects for equality
- write a client that prompts the user iteratively to enter values for a,b,c and display results on discriminant. program should end if the user enters 0 for a.
1 Expert Answer

Patrick B. answered 12/07/20
Math and computer tutor/teacher
import java.lang.Exception;
import java.io.*;
class IllegalArgumentException extends Exception { IllegalArgumentException( String msg) { super(msg); } }
class QuadraticEquation
{
private double A;
private double B;
private double C;
private double discriminant;
private void calcDiscriminant() { discriminant = B*B-4*A*C; }
//defaults to the parent quadratic function y=x^2
public QuadraticEquation()
{
A=1; B=C=0;
this.calcDiscriminant();
}
public QuadraticEquation(double a, double b, double c) throws IllegalArgumentException
{
if (a==0)
{
A=1; B=C=0;
this.calcDiscriminant();
throw (new IllegalArgumentException("A is not zero"));
}
else
{
A=a; B=b; C=c;
this.calcDiscriminant();
}
}
public void SetA( double a) throws IllegalArgumentException
{
if (a==0)
{
A=1; B=C=0;
throw (new IllegalArgumentException("A is not zero"));
}
else
{
A=a;
this.calcDiscriminant();
}
}
public void SetB( double b) { B=b; calcDiscriminant(); }
public void SetC( double c) { C=c; calcDiscriminant(); }
public double GetA() { return(A); }
public double GetB() { return(B); }
public double GetC() { return(C); }
public double GetDiscriminant() { return(discriminant); }
public double f( double x) { return(A*x*x+B*x+C); }
public double yIntercept() { return(this.f(0)); }
public double symmetryAxis() { return( -B/(2*A)); }
public boolean Equals( QuadraticEquation quadEq)
{
return ((this.A == quadEq.GetA()) && (this.B==quadEq.GetB()) && (this.C==quadEq.GetC()));
}
public QuadraticEquation( QuadraticEquation quadEq)
{
this.A = quadEq.GetA();
this.B=quadEq.GetB();
this.C = quadEq.GetC();
this.calcDiscriminant();
}
public String toString()
{
return(
new String(
"A= " + this.A
+ " : B= " + this.B
+ " : C = " + this.C
+ " : discriminant = " + this.discriminant
)
);
}
public String GetRoot1()
{
String strReturn;
double root1=0;
if (discriminant>0)
{
root1 = ((-B + Math.sqrt(discriminant))/(2*A));
strReturn = Double.toString(root1);
}
else if (discriminant==0)
{
root1 = (-B)/(2*A);
strReturn = Double.toString(root1);
}
else
{
double realPart = (-B)/(2*A);
double imaginaryPart = Math.sqrt(-discriminant)/(2*A);
strReturn = Double.toString(realPart)+ "+" + Double.toString(imaginaryPart)+"i";
}
return(strReturn);
}
public String GetRoot2()
{
String strReturn;
double root2=0;
if (discriminant>0)
{
root2 = ((-B - Math.sqrt(discriminant))/(2*A));
strReturn = Double.toString(root2);
}
else if (discriminant==0)
{
root2 = (-B)/(2*A);
strReturn = Double.toString(root2);
}
else
{
double realPart = (-B)/(2*A);
double imaginaryPart = Math.sqrt(-discriminant)/(2*A);
strReturn = Double.toString(realPart)+ "-" + Double.toString(imaginaryPart)+"i";
}
return(strReturn);
}
public static void main(String args[])
{
Console console = System.console();
double A,B,C;
String inbuff;
try
{
do
{
System.out.print(" Please input A :> ");
inbuff = console.readLine();
A = Double.parseDouble(inbuff);
}
while (A==0);
System.out.print(" Please input B :> ");
inbuff = console.readLine();
B = Double.parseDouble(inbuff);
System.out.print(" Please input C :> ");
inbuff = console.readLine();
C = Double.parseDouble(inbuff);
QuadraticEquation quadraticEquation = new QuadraticEquation(A,B,C);
System.out.println(quadraticEquation.toString());
double discr = quadraticEquation.GetDiscriminant();
String rootSoln1 = quadraticEquation.GetRoot1();
String rootSoln2 = quadraticEquation.GetRoot2();
if (discr>0)
{
System.out.println(" the two real solutions are " + Double.parseDouble(rootSoln1)
+ " and " + Double.parseDouble(rootSoln2));
}
else if (discr==0)
{
System.out.println(" the only solution is " + Double.parseDouble(rootSoln1)
+ " and " + Double.parseDouble(rootSoln2) + " has multiplicity 2 , is a double solution");
}
else
{
System.out.println(" the two imaginary/complex solutions are " + rootSoln1 + " and " + rootSoln2);
}
}
//catch(IOException ex) { System.out.println(" IO Exception occurs on input "); }
catch(IllegalArgumentException ex) { System.out.println(" Illegal argument exception occurs "); }
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Hi Clarissa . I uploaded the source code for you under this link in the RESOURCES section12/08/20