public class Equation
extends java.lang.Object
implements java.io.Serializable
a
,b
,c
in ax2 + bx + c = 0
.
The class provides standard methods; accessors and mutators for
the coefficients; and accessors for the roots. Root computation
takes into account the possibilities of a=0
,
b=0
, and c=0
, which lead to several
cases and give rise to either two real roots, one real root,
no roots, or infinitely many roots.Modifier and Type | Field and Description |
---|---|
static double |
EPSILON
This constant is set to a small tolerance value to
test equality of real numbers.
|
Constructor and Description |
---|
Equation()
Construct an equation having all coefficients set to zero.
|
Equation(double a,
double b,
double c)
Construct an equation having the passed coefficients
|
Equation(Equation equation)
Construct a copy of the passed Equation
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object other)
Determine if
this equation is the same
as the passed one. |
double |
getCoefficient(int coefNum)
Return the coefficient whose number (0,1,2) is
passed.
|
double |
getRoot(int rootNum)
Return the root whose number is passed.
|
int |
getRootCount()
Return the number of roots that
this equation has. |
int |
hashCode()
Compute a hash code for
this Equation. |
void |
setCoefficient(double a,
double b,
double c)
Set the coefficients of
this equation to the passed
parameters and re-solve it. |
java.lang.String |
toString()
Return a string representation of
this equation. |
public static final double EPSILON
double
's
are considered equal if abs(x-y) is less than this
constant.public Equation()
public Equation(Equation equation)
equation
- the Equation to copypublic Equation(double a, double b, double c)
a
- coefficient of x2b
- coefficient of xc
- the constant termpublic void setCoefficient(double a, double b, double c)
this
equation to the passed
parameters and re-solve it.a
- coefficient of x2b
- coefficient of xc
- the constant termpublic double getCoefficient(int coefNum)
coefNum
- the coefficient number to retrieve.
2 means the coefficient of x2, 1 is the coefficient
of x, and 0 means the constant term.java.lang.RuntimeException
- if the passed
number is not in the above range.public double getRoot(int rootNum)
this
equation has (as returned
getRootCount()
)rootNum
- the root number to retrieve.
If the equation has one root, rootNum must be set to 1.
If the equation has two roots, rootNum can be either 1
or 2, with 1 indicating the smaller of the two roots and
2 the larger.java.lang.RuntimeException
- if the passed parameter is not valid.public int getRootCount()
this
equation has.public java.lang.String toString()
this
equation.toString
in class java.lang.Object
ax^2 + bx + c = 0"
,
with a
,b
,c
replaced by the actual coefficients
and their signs. Note that the equal sign is
surrounded by two spaces, and that a blank
is inserted between terms.public boolean equals(java.lang.Object other)
this
equation is the same
as the passed one. An object is considered equal to
this
one if it is indeed an Equation
object and if the two equations have the same root count
(as returned by getRootCount()
) and if any present
roots are correspondingly equal (within EPSILON).equals
in class java.lang.Object
other
- the equation to compare with this
equation.false
if the passed instance is not an Equation
or
if the two equations do not share the root count, or if the they both have
one root but that root is not the same (within EPSILON
), or if
they both have two roots but the smaller (larger) root of the first is not
equal (within EPSILON
) to the smaller (larger) root of the second.
The return is true
in all other cases.public int hashCode()
this
Equation.hashCode
in class java.lang.Object