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

My new Java assignemnt is to take a user inputted polynomial expression in the f

ID: 3818103 • Letter: M

Question

My new Java assignemnt is to take a user inputted polynomial expression in the form of:

<element> <op> <element>

<operation between polynomials>

<element> <op> <element>

(example of one polynomial input: 3.5x^3 +x^2+5)

I understand how to manipulate the polynomials to preform addition, subtraction and multiplication but I have no idea how to get the user input into the appropriate Arrays or variables to complete the rest of the assignment.

Is there any code I could create to read the inputted polynomials and operations and create an ArrayLists of coefficents, and determine the degree of each polynomial. Can be a speerate class if necessary.

Thank you!!!

Explanation / Answer

//code

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();
}

  
public int degree()
{
int d = 0;
for (int i = 0; i < coef.length; i++)
if (coef[i] != 0) d = i;
return d;
}

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

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


public Polynomial times(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 Polynomial compose(Polynomial b)
{
Polynomial a = this;
Polynomial c = new Polynomial(0, 0);
for (int i = a.deg; i >= 0; i--) {
Polynomial term = new Polynomial(a.coef[i], 0);
c = term.plus(b.times(c));
}
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 evaluate(int x)
{
int p = 0;
for (int i = deg; i >= 0; i--)
p = coef[i] + (x * p);
return p;
}


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;
}
    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;
}
public static void main(String[] args) {
Polynomial zero = new Polynomial(0, 0);

Polynomial p1 = new Polynomial(4, 3);
Polynomial p2 = new Polynomial(3, 2);
Polynomial p3 = new Polynomial(1, 0);
Polynomial p4 = new Polynomial(2, 1);
Polynomial p = p1.plus(p2).plus(p3).plus(p4);

Polynomial q1 = new Polynomial(3, 2);
Polynomial q2 = new Polynomial(5, 0);
Polynomial q = q1.plus(q2);   


Polynomial r = p.plus(q);
Polynomial s = p.times(q);
Polynomial t = p.compose(q);

StdOut.println("zero(x) = " + zero);
StdOut.println("p(x) = " + p);
StdOut.println("q(x) = " + q);
StdOut.println("p(x) + q(x) = " + r);
StdOut.println("p(x) * q(x) = " + s);
StdOut.println("p(q(x)) = " + t);
StdOut.println("0 - p(x) = " + zero.minus(p));
StdOut.println("p(3) = " + p.evaluate(3));
StdOut.println("p'(x) = " + p.differentiate());
StdOut.println("p''(x) = " + p.differentiate().differentiate());
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote