Question: [JAVA] Your assignment is to write a class called Polynomial. A polynomial is a function of the following form: f(n) = ck nk + ck-1

[JAVA] Your assignment is to write a class called Polynomial. A polynomial is a function of the following form: f(n) = 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). The coefficient ck should be nonzero. I have placed a file named hw1.jar in the D2L Homework assignment 1 dropbox folder. It contains 2 source files called Polynomial.java and TestHW1.java. You may use the code in Polynomial.java as a starting point for your program, or if you prefer you can write your own code from scratch. The TestHW1 class tests the methods of the Polynomial class on a few cases. Please upload the Polynomial.java file once you have completed it. 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 coefficient 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 is usually the case in Java, toString is passed 0 parameters and returns a String representation of an object. Examples of the kind of string that toString should return for polynomials 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. As I said, I have written some test code in the file TestHW1.java. You may use this to your own code; however, you may wish to write your own main method which tests your Polynomial class more thoroughly. I may use different examples than the ones in TestHW1 when I grade your assignment. Here are the contents of TestHW1.java: package hw1; public class TestHW1 { public static void main(String[] args) { int coeffs1[] = {1, -1, 2}; // f(n) = n^2 - n + 2 int coeffs2[] = {2, 0, 1, 0}; // f(n) = 2n^3 + n int coeffs3[] = {0, 0, 1, 0}; // f(n) = n (after simpliciation) int coeffs4[] = { }; // f(n) = 0 Polynomial p1 = new Polynomial(coeffs1); Polynomial p2 = new Polynomial(coeffs2); Polynomial p3 = new Polynomial(coeffs3); Polynomial p4 = new Polynomial(coeffs4); System.out.println("Polynomial 1: " + p1); System.out.println("Polynomial 2: " + p2); System.out.println("Polynomial 3: " + p3); System.out.println("Polynomial 4: " + p4); System.out.println(" Values for f(n) = " + p1); for (int i=-6; i<=6; i+=3) System.out.println("f(" + i + ") = " + p1.evaluate(i)); System.out.println(" Values for f(n) = " + p2); for (int i=-6; i<=6; i+=3) System.out.println("f(" + i + ") = " + p2.evaluate(i)); System.out.println(" Values for f(n) = " + p3); for (int i=-6; i<=6; i+=3) System.out.println("f(" + i + ") = " + p3.evaluate(i)); System.out.println(" Values for f(n) = " + p4); for (int i=1; i<100; i+=50) System.out.println("f(" + i + ") = " + p4.evaluate(i)); } } Note that in Java, an array can be declared and initialized in one statement, by placing { } around the arrays initial contents. For example:: int x[] = {1, 2, 3, 4}; is the same as: int x[] = new int[4]; for (int i=0; i<4; i++) x[i] = i+1; Also note the effects of the simplify method: the coefficients {0, 0, 1, 0} should be changed by the simplify method to {1, 0}. Here is sample output which my own solution produces when I run the above main method. Polynomial 1: n^2 - n + 2 Polynomial 2: 2n^3 + n Polynomial 3: n Polynomial 4: 0 Values for f(n) = n^2 - n + 2 f(-6) = 44 f(-3) = 14 f(0) = 2 f(3) = 8 f(6) = 32 Values for f(n) = 2n^3 + n f(-6) = -438 f(-3) = -57 f(0) = 0 f(3) = 57 f(6) = 438 Values for f(n) = n f(-6) = -6 f(-3) = -3 f(0) = 0 f(3) = 3 f(6) = 6 Values for f(n) = 0 f(1) = 0 f(51) = 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!