Factors of a Polynomial
Getting Started
Install
Download the files from this repo, and execute them with your Java IDE, or from the console. This program requires JRE 1.8.0+.
Help
Just follow the prompts! The computer will ask you to input the co-efficients of the equation. Then, the three factors of the polynomial will be displayed in the terminal / output window, depending on where you run the program.
How it Works
What it is
To know how the program works, we need to first understand what it actually does. This is a simple program to find the factors of a polynomial. It does this by checking values (only between 1000 and -1000, though this can be changed) of x
for which the given equation can be satisfied.
Variables
Note: All these variables are declared and initialised in
main()
itself. The variables used in other parts of the program are for local calculations only.
int cube, square, one, none
: Integers which input and store the coefficients of the equation.int count
: Counts the number of factors that the program is currently at, hence is initialized to0
. This is important, since we know that there will be 3 factors at maximum (i.e. for a cubic equation, there are only 3 factors.)double equation
: Used to check if the current value ofi
, if substituted in the given equation with the coefficients, satisfies the equation. Initialised to-1
, to get rid of any unwanted consequences.double i
: Used as an index variable. Non-conventionally, it is a double value since the factors of the polynomial might have decimals too.
Methods
public static void main()
Most of the program code is in the main()
function. It does not return any value. It calls the other functions when and where necessary. Essentially it checks if the current value of i
satisfies the equation. If factorisable, a cubic equation should have 3 such values of i
(i.e. 3 factors). All the important variables and arrays are declared in this method, and passed to other methods at the time of calling, if required. This is also the only method which is public
in the entire program, so that abstraction is implemented and the user does not get confused as to which program he has to run.
private void Factor1(double i)
It is used to output the first factor of the polynomial. It does not return any value.
private void Factor2(double i)
It is used to output the second factor of the polynomial. It does not return any value.
private void Factor3(double i)
It is used to output the third factor of the polynomial. In case of a quadratic equation, it will not display anything, and directly exit.