Question
a) Design and write the methods calculateBruteForce and calculateHorner for calculating the polynomial value by using the brute force approach and respectively Horner's technique. When implementing calculateBruteForce method, define and invoke your own method for raising x to a given power, instead of invoking a predefined method such as the Math.pow(). By analyzing the code, determine and indicate time complexity of the two methods in terms of Big-O notation. Which of the two methods would perform better in terms of execution time?
b) Write a driver program TestPolyVal.java to validate the conclusions drawn from question a). Consider a polynomial with 20 terms whose coefficients are stored in an integer array and are randomly generated in the interval -9 .. 9. In the array of coefficients, the array cell of index i corresponds to the polynomial coefficient ai, where i represents the exponent of the variable x. Implement the following actions:
(b1) Invoke the two methods and display the polynomial value returned by the methods for a random generated integer value in the interval 1 .. 9.
(b2) Measure and display the execution time of the two methods as the arithmetic mean value of 100 executions. Use the method System.nanoTime() for determining the execution time. Discuss the obtained results. Are the results in line with your answer to question a?
Note. Due to the behavior of the JIT compiler, the execution time of the repeated programs is much slower the first times they are run and therefore make sure to discard the measured values for the initial 5 runs.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
public class Polynomial {private int [] array;
public Polynomial(int[] array) {
this.array = new int[array.length];
System.arraycopy(array, 0, this.array, 0, array.length);
}
public int calculateBruteForce(int x) {
int result = 0;
for (int i = 0; i < array.length; i++) {
result += array[i] * power(x, i);
}
return result;
}...