Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 polynomials 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 } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago