Cubic
edu.rit.numeric

## Class Cubic

• `public class Cubicextends Object`
Class Cubic solves for the real roots of a cubic equation with real coefficients. The cubic equation is of the form

ax3 + bx2 + cx + d = 0

To solve a cubic equation, construct an instance of class Cubic; call the Cubic object's solve() method, passing in the coefficients a, b, c, and d; and obtain the roots from the Cubic object's fields. The number of (real) roots, either 1 or 3, is stored in field nRoots. If there is one root, it is stored in field x1, and fields x2 and x3 are set to NaN. If there are three roots, they are stored in fields x1, x2, and x3 in descending order.

The same Cubic object may be used to solve several cubic equations. Each time the solve() method is called, the solution is stored in the Cubic object's fields.

The formulas for the roots of a cubic equation come from:

E. Weisstein. "Cubic formula." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/CubicFormula.html

• ### Field Summary

Fields
Modifier and TypeField and Description
`int``nRoots`
The number of real roots.
`double``x1`
The first real root.
`double``x2`
The second real root.
`double``x3`
The third real root.
• ### Constructor Summary

Constructors
Constructor and Description
`Cubic()`
Construct a new Cubic object.
• ### Method Summary

Methods
Modifier and TypeMethod and Description
`static void``main(String[] args)`
Unit test main program.
`void``solve(double a, double b, double c, double d)`
Solve the cubic equation with the given coefficients.
• ### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### nRoots

`public int nRoots`
The number of real roots.
• #### x1

`public double x1`
The first real root.
• #### x2

`public double x2`
The second real root.
• #### x3

`public double x3`
The third real root.
• ### Constructor Detail

• #### Cubic

`public Cubic()`
Construct a new Cubic object.
• ### Method Detail

• #### solve

`public void solve(double a,         double b,         double c,         double d)`
Solve the cubic equation with the given coefficients. The results are stored in this Cubic object's fields.
Parameters:
`a` - Coefficient of x3.
`b` - Coefficient of x2.
`c` - Coefficient of x.
`d` - Constant coefficient.
Throws:
`DomainException` - (unchecked exception) Thrown if a is 0; in other words, the coefficients do not represent a cubic equation.
• #### main

`public static void main(String[] args)                 throws Exception`
Unit test main program.

Usage: java edu.rit.numeric.Cubic a b c d

Throws:
`Exception`