Polynomials are extremely useful mathematical objects. A polynomial can be defin
ID: 3815056 • Letter: P
Question
Polynomials are extremely useful mathematical objects. A polynomial can be defined as the sum of monomials, while monomials can be defined as a single-term quantity that consists of a coefficient, a variable, and a positive integer power. Various algebraic operations can be defined for polynomials, such as addition, subtraction, multiplication, and division, and the polynomials are also functions that can be evaluated. In this question, you will define a Polynomial class and define several methods for the Polynomial object. Implement a Polynomial class in Java that has the following features: Utilizes an array to store (double) polynomial coefficients Implements an add () method that can take doubles or Polynomial objects, and that returns a new Polynomial object that contains the sum. Implements an evaluate () method that can take a double x and returns the value of the polynomial evaluated at x. Implements a toString () method that will nicely print polynomials: toString () examples: 4.0 x^2 + 2.5 x - 1.0 x - 1 4.3 x^3 - x + 1 -4.0 x^2 + 4.0 You must implement a driver that creates a Polynomial object and tests your code. Test your add () and toString () methods by evaluating the following polynomial sum (you should be printing a Polynomial object directly using System. out. println): P (x) + R (x) for P (x) = 1/3 x^3 - 1/2 x, K (x) = 12 + 9x Test your evaluate () method by evaluating the following polynomial at the given location: P (z)=z^8 + z^6 + z^4 + z^2 + z, z = -2.5 Format Include your answers to this question with your Homework #8 submission on Canvas. Include your Java code and the console output from each of your code tests.Explanation / Answer
Java program:
public class Polynomials {
private double[] coefficients; // coefficients
private int degree;
public Polynomials(double a, int b) {
coefficients = new double[b + 1];
coefficients[b] = a;
degree = degree();
}
public int degree() {
int d = 0;
for (int i = 0; i < coefficients.length; i++)
if (coefficients[i] != 0)
d = i;
return d;
}
public Polynomials add(Polynomials b) {
Polynomials a = this;
Polynomials c = new Polynomials(0, Math.max(a.degree, b.degree));
for (int i = 0; i <= a.degree; i++)
c.coefficients[i] += a.coefficients[i];
for (int i = 0; i <= b.degree; i++)
c.coefficients[i] += b.coefficients[i];
c.degree = c.degree();
return c;
}
public Polynomials subtract(Polynomials b) {
Polynomials a = this;
Polynomials c = new Polynomials(0, Math.max(a.degree, b.degree));
for (int i = 0; i <= a.degree; i++)
c.coefficients[i] += a.coefficients[i];
for (int i = 0; i <= b.degree; i++)
c.coefficients[i] -= b.coefficients[i];
c.degree = c.degree();
return c;
}
public double evaluate(double x) {
double p = 0;
for (int i = degree; i >= 0; i--)
p = coefficients[i] + (x * p);
return p;
}
public String toString() {
if (degree == 0)
return "" + coefficients[0];
if (degree == 1)
return coefficients[1] + "x + " + coefficients[0];
String s = coefficients[degree] + "x^" + degree;
for (int i = degree - 1; i >= 0; i--) {
if (coefficients[i] == 0)
continue;
else if (coefficients[i] > 0)
s = s + " + " + (coefficients[i]);
else if (coefficients[i] < 0)
s = s + " - " + (-coefficients[i]);
if (i == 1)
s = s + "x";
else if (i > 1)
s = s + "x^" + i;
}
return s;
}
// Driver tests
public static void main(String[] args) {
Polynomials p1 = new Polynomials(1.0 / 3.0, 3);
Polynomials p2 = new Polynomials(1.0 / 2.0, 1);
Polynomials p = p1.subtract(p2); // 1/3x^3 - 1/2x
Polynomials r1 = new Polynomials(12, 0);
Polynomials r2 = new Polynomials(9, 1);
Polynomials r = r1.add(r2); // 12 + 9x
System.out.println("p(x) = " + p);
System.out.println("r(x) = " + r);
Polynomials s1 = new Polynomials(1, 8);
Polynomials s2 = new Polynomials(1, 6);
Polynomials s3 = new Polynomials(1, 4);
Polynomials s4 = new Polynomials(1, 2);
Polynomials s5 = new Polynomials(1, 1);
Polynomials s = s1.add(s2).add(s3).add(s4).add(s5);
System.out.println("s(x) = " + s);
System.out.println("s(-2.5)="+s.evaluate(-2.5));
}
}
Steps to compile,run and sample output shown in below screenshot
javac Polynomials.java
java Polynomials
p(x) = 0.3333333333333333x^3 - 0.5x
r(x) = 9.0x + 12.0
s(x) = 1.0x^8 + 1.0x^6 + 1.0x^4 + 1.0x^2 + 1.0x
s(-2.5)=1812.83203125