type.lib
Class Equation

java.lang.Object
  extended by type.lib.Equation
All Implemented Interfaces:
java.io.Serializable

public class Equation
extends java.lang.Object
implements java.io.Serializable

Encapsulates an algebraic equation of the second degree. Each such equation is identified by its three attributes being the coefficients 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.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca
See Also:
Serialized Form

Field Summary
static double EPSILON
          This constant is set to a small tolerance value to test equality of real numbers.
 
Constructor Summary
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
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EPSILON

public static final double EPSILON
This constant is set to a small tolerance value to test equality of real numbers. Two real double's are considered equal if abs(x-y) is less than this constant.

See Also:
Constant Field Values
Constructor Detail

Equation

public Equation()
Construct an equation having all coefficients set to zero.


Equation

public Equation(Equation equation)
Construct a copy of the passed Equation

Parameters:
equation - the Equation to copy

Equation

public Equation(double a,
                double b,
                double c)
Construct an equation having the passed coefficients

Parameters:
a - coefficient of x2
b - coefficient of x
c - the constant term
Method Detail

setCoefficient

public void setCoefficient(double a,
                           double b,
                           double c)
Set the coefficients of this equation to the passed parameters and re-solve it.

Parameters:
a - coefficient of x2
b - coefficient of x
c - the constant term

getCoefficient

public double getCoefficient(int coefNum)
Return the coefficient whose number (0,1,2) is passed.

Parameters:
coefNum - the coefficient number to retrieve. 2 means the coefficient of x2, 1 is the coefficient of x, and 0 means the constant term.
Returns:
the requested coefficient.
Throws:
java.lang.RuntimeException - if the passed number is not in the above range.

getRoot

public double getRoot(int rootNum)
Return the root whose number is passed. The passed root number must be valid in light of the number of roots this equation has (as returned getRootCount())

Parameters:
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.
Returns:
the requested root.
Throws:
java.lang.RuntimeException - if the passed parameter is not valid.

getRootCount

public int getRootCount()
Return the number of roots that this equation has.

Returns:
the number of roots, or 0 if there are none, or -1 if there are infinitly many (the equation is an identity).

toString

public java.lang.String toString()
Return a string representation of this equation.

Overrides:
toString in class java.lang.Object
Returns:
the string 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.

equals

public boolean equals(java.lang.Object other)
Determine if 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).

Overrides:
equals in class java.lang.Object
Parameters:
other - the equation to compare with this equation.
Returns:
true or false as defined above. The return is 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.

hashCode

public int hashCode()
Compute a hash code for this Equation.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.


Java by Abstraction: A Client-View Approach