QuestionQuestion

Consider a polynomial with integer coefficients and positive integer exponents P(x) = a0 * x⁰ + a1 * x¹ + ... + an-1 * xⁿ⁻¹. Consider the problem of calculating the polynomial value for an integer value of its variable x. For example, if the polynomial is P(x) = 2 * x³ - 5 * x² + 6 * x¹ + 8 * x⁰ and the integer value is x = 2 than the value of P(2) is 16.

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 PreviewSolution 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;
    }...
$60.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats