Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Please Help. I need help with evaluate() and Polynomial(int[] c) been tryin

ID: 3850569 • Letter: J

Question

JAVA

Please Help. I need help with evaluate() and Polynomial(int[] c) been trying to get solutions for days no luck. I posted a picture of how the outcome should be. Thanks in advance.

public class Polynomial private int coefficients; the constructor creates a Polynomial with the specified coefficients (passed as the parameter c) It should set the coefficients instance variable to be an array which is a copy of c At the end, the constructor calls the "simplify" method, which makes sure that the first coefficient(s) of the polymonial are non-zero. public Polynomial(int[ c) coefficients new int length] complete this make sure that the first coefficient of the polynomial is not e simplify(); simplify should (if necessary) create a new array to be stored in the coefficients instance variable In the new array, the first coefficient will be nonzero. For example, int coeffs fe, e, 2, 1, Polynomial p new Polynomial (coeffs) p represents the function f(n) en 4 en 3 2n 2 n. We would like to get ride of the highest order term (s) with coefficient of 0. In this example after calling p. simplify() p's coefficients should be 12, 1, 01, which represents f(n) 2n 2 n public void simplify() int i; for (i e; i coefficients length && coefficients[ i++) fill in the code for this method System.out.println(coefficients); int[ newCoeff new int[coefficients.length i]; for (int j j newcoeff.length; j++){

Explanation / Answer

PROGRAM CODE:

public class Polynomial {

private int coefficients[];

public Polynomial(int c[]) {

coefficients = new int[c.length];

for(int i=0; i<c.length; i++)

coefficients[i] = c[i];

//simplify();

}

public int degree()

{

int degree = 0;

for(int i=0; i<=coefficients.length-1; i++)

degree = i;

return degree;

}

public int evaluate(int x)

{

int degree = degree();

int total = 0;

//Going through each coefficient and calculating the value

for(int m=0; m<coefficients.length; m++)

{

//power of x to the degree multiplied by coefficient

total += Math.pow(x, degree)*coefficients[m];

//reducing the degree for the next term

degree--;

}

return total;

}

public static void main(String args[])

{

// f(n) = n^2 - n + 2

Polynomial polynomial1 = new Polynomial(new int[]{1, -1, 2});

// f(n) = 2n^3 + n

Polynomial polynomial2 = new Polynomial(new int[]{2, 0, 1, 0});

//Printed in the same order as mentioned in the sample output above

System.out.println(polynomial1.evaluate(-6));

System.out.println(polynomial2.evaluate(-6));

System.out.println(polynomial1.evaluate(-3));

System.out.println(polynomial2.evaluate(-3));

System.out.println(polynomial1.evaluate(0));

System.out.println(polynomial2.evaluate(0));

System.out.println(polynomial1.evaluate(3));

System.out.println(polynomial2.evaluate(3));

System.out.println(polynomial1.evaluate(6));

System.out.println(polynomial2.evaluate(6));

}

}

OUTPUT:

44
-438
14
-57
2
0
8
57
32
438