Question: Java code help with polynomail, having the user input polynomails of addition, subtraction, derivatives, scalar, etc. the polynomial needs to be inputed in a sequence
Java code help with polynomail, having the user input polynomails of addition, subtraction, derivatives, scalar, etc. the polynomial needs to be inputed in a sequence of coefficents and exponents and for them into a linked polynomial using this code.
public class Term { private int coeff; private int exp; public Term(int c, int e) { coeff = c; exp = e; }
public void setCoeff(int c) { coeff = c; } public int getCoeff() { return coeff; }
public void setExpon(int e) { exp = e; } public int getExpon() { return exp; } }
public class AddPolys { private ObjectList sum;
public AddPolys() { sum = new ObjectList(); } private void attach(int coeff, int exp) { Term t = new Term(coeff, exp); sum.addLast(t); }
public ObjectList sumTwoPolys(ObjectList list1, ObjectList list2) { ObjectListNode p = list1.getFirstNode(); ObjectListNode q = list2.getFirstNode();
while (p != null && q != null) { Term t1 = (Term) p.getInfo(); Term t2 = (Term) q.getInfo(); if (t1.getExpon() == t2.getExpon()) { int coeff = t1.getCoeff() + t2.getCoeff(); if (coeff != 0) attach(coeff, t1.getExpon()); p = p.getNext(); q = q.getNext(); } else if (t1.getExpon() > t2.getExpon()) { attach(t1.getCoeff(), t1.getExpon()); p = p.getNext(); } else { attach(t2.getCoeff(), t2.getExpon()); q = q.getNext(); } } while (p != null) { Term t1 = (Term) p.getInfo(); attach(t1.getCoeff(), t1.getExpon()); p = p.getNext(); } while (q != null) { Term t2 = (Term) q.getInfo(); attach(t2.getCoeff(), t2.getExpon()); q = q.getNext(); } return sum; }
Here is the prompt below:
Write a method that will input a sequence of coefficients and exponents and form them into a linked polynomial as described earlier.
Write a method that will subtract two polynomials.
Write a method that will compute the first, second, and third derivative of a polynomial
Write a method that will multiply a polynomial by a scalar.
Write a method that, given a polynomial and an integer, evaluates th polynomial at that number
Write a method that will print a polynomial as a sequence of coefficients a exponents, arranged attractively. Be sure to include an appropriate driver for your code and send all program
Here is my code. But the user can't input it. how do i fix my code in eclipse using java to allow users to input polynomails?
please give code and screenshot of the problem. and outcome.
public class Polynomial {
private int[] coef;
private int deg;
public Polynomial(int a, int b) {
coef = new int[b+1];
coef[b] = a;
deg = degree();
}
// return c = a + b
public Polynomial plus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
c.deg = c.degree();
return c;
}
// return (a - b)
public Polynomial minus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
c.deg = c.degree();
return c;
}
// return (a * b)
public Polynomial mul(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, a.deg + b.deg);
for (int i = 0; i <= a.deg; i++)
for (int j = 0; j <= b.deg; j++)
c.coef[i+j] += (a.coef[i] * b.coef[j]);
c.deg = c.degree();
return c;
}
public boolean eq(Polynomial b) {
Polynomial a = this;
if (a.deg != b.deg) return false;
for (int i = a.deg; i >= 0; i--)
if (a.coef[i] != b.coef[i]) return false;
return true;
}
public int degree() {
int d = 0;
for (int i = 0; i < coef.length; i++)
if (coef[i] != 0) d = i;
return d;
}
// use Horner's method to compute and return the polynomial evaluated at x
public int evaluate(int x) {
int p = 0;
for (int i = deg; i >= 0; i--)
p = coef[i] + (x * p);
return p;
}
// differentiate this Polynomial and return it
public Polynomial differentiate() {
if (deg == 0) return new Polynomial(0, 0);
Polynomial deriv = new Polynomial(0, deg - 1);
deriv.deg = deg - 1;
for (int i = 0; i < deg; i++)
deriv.coef[i] = (i + 1) * coef[i + 1];
return deriv;
}
// convert to string representation
public String toString() {
if (deg == 0) return "" + coef[0];
if (deg == 1) return coef[1] + "x + " + coef[0];
String s = coef[deg] + "x^" + deg;
for (int i = deg-1; i >= 0; i--) {
if (coef[i] == 0) continue;
else if (coef[i] > 0) s = s + " + " + ( coef[i]);
else if (coef[i] < 0) s = s + " - " + (-coef[i]);
if (i == 1) s = s + "x";
else if (i > 1) s = s + "x^" + i;
}
return s;
}
// test client
public static void main(String[] args) {
Polynomial zero = new Polynomial(0, 0);
Polynomial p1 = new Polynomial(5, 3);
Polynomial p2 = new Polynomial(3, 2);
Polynomial p3 = new Polynomial(6, 4);
Polynomial p4 = new Polynomial(7, 1);
Polynomial p = p1.plus(p2).plus(p3).plus(p4);
Polynomial q1 = new Polynomial(7, 2);
Polynomial q2 = new Polynomial(2, 0);
Polynomial q = q1.plus(q2);
/* To Add Both Polynomial */
Polynomial r = p.plus(q);
/* Multiply Both Polynomial */
Polynomial s = p.mul(q);
System.out.println("p(x) = " + p);
System.out.println("q(x) = " + q);
System.out.println("p(x) + q(x) = " + r);
System.out.println("q(x) - p(x) = " + q.minus(p));
System.out.println("p(x) * q(x) = " + s);
System.out.println("p(3) = " + p.evaluate(3));
System.out.println("p'(x) = " + p.differentiate());
System.out.println("p''(x) = " + p.differentiate().differentiate());
System.out.println("p'''(x) = " + p.differentiate().differentiate().differentiate());
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
