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

JAVA(everything in Bold is what must be filled in/replaced) Your assignment is t

ID: 3850294 • Letter: J

Question

JAVA(everything in Bold is what must be filled in/replaced)

Your assignment is to write a class called Polynomial. A polynomial is a function of the following form:

f ) = ck nk + ck-1 nk-1 + ... + c1 n + c0

c0, c1, c2, ..., ck are called coefficients. We call k the degree of the polynomial. For our purposes, we will assume that the coefficients are all integers (positive, negative, or 0).

I recommend that you store the coefficients of a Polynomial object in an instance variable which is an array of integers.

You should write the following methods for the Polynomial class

A. (1/2 point) A constructor. It is passed 1 parameter: an array of integers which represents the coefficients of the polynomial.

B. (1 point) A method called simplify. It is a void method which is passed 0 parameters. It ensures that the polynomial’s kth co-efficient is not 0. If a polynomial is created which has no non-zero coefficients, then it should be represented as an array of length 0. The simplify method should be called within the constructor.

C.(2 points) A toString method. As always, toString is passed 0 parameters and returns a String representation of an object. An example of the kind of string that toString should return can be found below.

D. (1/2 point) A degree method. It returns an integer, which is the degree of the polynomial.

E. (1 point) An evaluate method. It is passed 1 parameter x, which is an integer. It should return an integer, which represents f(x), the value of the polynomial f(n) when n is equal to x.

package hw1;

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[c.length];
       // complete this

      
      
       // make sure that the first coefficient of the polynomial is not 0
       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[] = {0, 0, 2, 1, 0};
       Polynomial p = new Polynomial(coeffs);
       p represents the function f(n) = 0n^4 + 0n^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 {2, 1, 0}, which
       represents f(n) = 2n^2 + n
   */
   public void simplify() {
       // fill in the code for this method

      
      
   }
  
   /*
       return a String that represents the Polynomial. The
       toString method should be written as specified in
       the homework write-up
   */
   public String toString() {
       int p = degree();
       if (coefficients.length == 0)
           return "0";
       StringBuilder b = new StringBuilder("");
       // fill in the rest

      
      
      
      
      
      
      
      
       return b.toString();
   }

   // return the degree of the polynomial
   public int degree() {
       return 0;    // replace this
   }

   // return the value of the Polymonial f(n) when n is equal to x
   public int evaluate(int x) {
       // fill this in

      
      
      
       return 0;   // remove this line
  
   }
}

Explanation / Answer

The answer is as follows:

import java.io.*;
import java.lang.*;

public class Polynomial
{
private int[] coefficients;
  
   public Polynomial(int[] c) {
      int i;
      coefficients = new int[c.length];
      for (i=0; i<c.length; i++){
          coefficients[i] = c[i];
      }
      simplify();
   }
   public void simplify() {
      int i;
      int count;
      int start;
     
      temp = new int[coefficients.length];
      for (i=0; i<coefficients.length; i++)
          temp[i] = coefficients[i];
      count = 0;
      for (i=0; i<coefficients.length; i++){
          if (coefficients[i] == 0)
             count++;
          else{
             start = i;
             break;
          }
      }
      coefficients = new int[temp.length - count];
      for (i=start; i<temp.length; i++){
          coefficients[i - start] = temp[i];
      }     
     
   }
   public String toString() {
      int p = degree();
      int i;
      if (coefficients.length == 0)
         return "0";
      StringBuilder b = new StringBuilder("");
      b.ensureCapacity(p+1);
      for(i = 0; i < coefficients.length; i++){
         b.append(Integer.toString(coefficients[i]));
      }
      return b.toString();
   }
   public int degree() {
      return coefficients.length-1;
   }
   public int evaluate(int x){
      int sum = 0;
      int i;
      int p = degree();
      for (i=0; i<coefficients.length; i++)
          sum = sum + coefficients[i] * Math.pow(x,p-i);
      return sum;
   }
}