Java Snippets

Hamburger icon

Cubic Evaluation

The program CubicEval evaluates a cubic equation. In mathematics, a cubic function is a function of the form:

f(x)=ax3+bx2+cx+d

where a is nonzero; or in other words, a function defined by a polynomial of degree three.

Polynomialdeg3

The program asks for the values of the coefficients a, b, c, and d. Then it asks for a minimum and maximum value of x in order to set the range of the calculation (xmin and xmax). Finally it asks for the size of the step, the separation between evaluations (deltax).

They are used in a loop:

for (double x = xmin; x <= xmax; x += deltax) {...}

and then the results of y for each x are printed on screen. The function that makes the evaluation is in the class CCubicEval.

Function evaluateX()

This is the function that evaluates y for different values of x. It is defined as static, so that there is no need to instantiate the class to invoke it.

public static double evaluateX(CCubicEval f, double x) {
	double y = f.a*Math.pow(x,3) + f.b*Math.pow(x,2) + f.c*x +  f.d;
	return y;
}

In the application, we can call it by first creating an object of type CCubicEval that contains the values of the coefficients. Then we can reuse it to calculate the function in different regions:

CCubicEval f = new CCubicEval(a, b, c, d);

...

double y = CCubicEval.evaluateX(f, x);
View Cubic Eval's repo