Finally we want to add a method getSolution to solve the quadratic
equation. Since the number of solutions can be 0, 1 or 2, it returns an array
of appropriate length:
QuadraticEquation.java
The method computes the discriminant (i.e. the term under the square root)
and arranges the terms according to the its sign.
The standard class Math contains a lot of mathematical
functions as static methods, e.g. sqrt (square root), sin or
log.
The main routine creates a QuadraticEquation from the input parameters and
prints it, together with its solution:
TestQuadraticEquation.java
To do this, it needs to know the number of solutions. This can be extracted
directly from the result array:
An array has an integer field length that contains the number of
elements of the array.