Arithmetics

$\triangleright$
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.

$\bullet$
The standard class Math contains a lot of mathematical functions as static methods, e.g. sqrt (square root), sin or log.

$\triangleright$
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:

$\bullet$
An array has an integer field length that contains the number of elements of the array.

previous    contents     next

Peter Junglas 8.3.2000