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

I need help with my homework and need a comparison to see if I\'m doing it right

ID: 3888137 • Letter: I

Question

I need help with my homework and need a comparison to see if I'm doing it right. It is a simple program that needs to be in Java, but somewhere along the lines I did something wrong. Here are the instructions. Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) defube the following methods: a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to the coefficient of the specified exponent. e. public void assign_coef(double coefficient, int exponent) POSTCONDITION: Sets the coefficient for the specified exponent. f. public double coefficient(int exponent) POSTCONDITION: Returns coefficient at specified exponent of this polynomial. g. public double eval(double x) POSTCONDITION: The return value is the value of this polynomial with the given value for the variable x. Do not use power method from Math, which is very low efficient h. public boolean equals (Polynomial p) POSTCONDITION: return true if p and this polynomial is same i. public string toString() POSTCONDITION: return the polynomial as a string like “2x^2 + 3x + 4” Important only non-zero terms j. public Polynomial add(Polynomial p) POSTCONDITION: return a polynomial that is the sum of p and this polynomial k. public Polynomial multiply(Polynomial p) POSTCONDITION: returns a new polynomial obtained by multiplying this term and p. For example, if this polynomial is 2x^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function, it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28. l. Write a single main method to test ALL the methods and classes. You can read from the keyboard, or from the file. The format of the file should be: first line specifies the maximum degree, then followed by the terms, one per line. You can assume they are in the right order. For example: polynomialA.txt 100 (3,0) (-1, 3) (4, 100)

Explanation / Answer

package polynomial;
public class Polynomial {
int degree;
double[] coeff;
public Polynomial()
{
coeff=new double[1];
degree=0;
}
public Polynomial(double a0)
{
coeff=new double[1];
coeff[0]=a0;
degree=0;
}
public Polynomial(Polynomial p)
{
degree=p.degree;
for(int i=0;i<=p.degree;i++)
{
coeff[i]=p.coeff[i];
}
}
public void add_to_coef(double amount, int exponent)
{
coeff[exponent]+=amount;
}
public void assign_coef(double coefficient, int exponent)
{
coeff[exponent]=coefficient;
}
public double coefficient(int exponent)
{
return coeff[exponent];
}
public boolean equals (Polynomial p)
{
for(int i=0;i<=p.degree;i++)
{
if(coeff[i]!=p.coeff[i])
return false;
}
return true;
}
public String toString()
{
String k="";
for(int i=degree;i>0;i--)
if(coeff[i]!=0)
k=k+coeff[i]+"x^"+i+"+";
k=k+coeff[0];
return k;
}
public Polynomial add(Polynomial p)
{
Polynomial k=new Polynomial();
if(p.degree>degree)
{
k.degree=p.degree;
k.coeff=new double[p.degree+1];
}
else
{
k.degree=degree;
k.coeff=new double[degree+1];
}
for(int j=0;j<=k.degree;j++)
k.coeff[j]=0;
for(int i=0;i<=degree;i++)
k.coeff[i]=k.coeff[i]+coeff[i];
for(int di=0;di<=p.degree;di++)
k.coeff[di]=k.coeff[di]+p.coeff[di];
return k;
  
}
public Polynomial multiply(Polynomial p)
{
Polynomial k=new Polynomial();
k.degree=degree+p.degree;
k.coeff=new double[k.degree+1];
for(int l=0;l<=k.degree;l++)
k.coeff[l]=0;
for (int i = 0; i <= degree; i++)
for (int j = 0; j <= p.degree ;j++)
k.coeff[i+j] += (coeff[i] * p.coeff[j]);
return k;
}
public static void main(String[] args) {
Polynomial pol=new Polynomial();
pol.degree=100;
pol.coeff=new double[pol.degree+1];
for(int m=0;m<=pol.degree;m++)
pol.coeff[m]=0;
pol.assign_coef(3, 0);
pol.assign_coef(-1, 3);
pol.assign_coef(4, 100);
System.out.println(pol.toString());
Polynomial pol1=new Polynomial();
pol1.degree=100;
pol1.coeff=new double[pol1.degree+1];
for(int m=0;m<=pol1.degree;m++)
pol1.coeff[m]=0;
pol1.assign_coef(2, 0);
pol1.assign_coef(4, 3);
pol1.assign_coef(8, 100);
System.out.println(pol1.toString());
System.out.println(pol.add(pol1).toString());
System.out.println(pol.multiply(pol1).toString());
}
  
}

Output:

run:
4.0x^100+-1.0x^3+3.0
8.0x^100+4.0x^3+2.0
12.0x^100+3.0x^3+5.0
32.0x^200+8.0x^103+32.0x^100+-4.0x^6+10.0x^3+6.0
BUILD SUCCESSFUL (total time: 0 seconds)

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