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

Question: The class Polynomial as given in the Programming Example Polynomial Op

ID: 665945 • Letter: Q

Question

Question: The class Polynomial as given in the Programming Example Polynomial Operations processes polynomials with coefficients that are real numbers. Redesign this class so that it can also be used to process polynomials with complex numbers as coefficients. <Note that first you must design the class Complex to implement complex numbers.>

I finished it and it works, this is implemented using 6 java files, I will post the one that is more relevent. The problem is that the teacher says that it needs to be <Complex> instead of
public class Polynomial extends UnorderedArrayList<Double>. This has brought an underlying problem that it doesn't have real and imaginary numbers. I'm not sure how to correct it.

Work: Polynomial.java

package complex;
import java.util.*;

public class Polynomial extends UnorderedArrayList<Double>
{
       //Default constructor
       //Postcondition: An array of the size 100, and
       //             length and maxSize are set to 100
    public Polynomial()
    {
         super();
         length = 100;
    }

      //Constructor with a parameter
      //Postcondition: An array of the size specified by
      //               the parameter size is created, length
      //               and maxSize are initialized to size
    public Polynomial(int size)
    {
         super(size);
         length = size;
    }

       //Method to evaluate a polynomial at a given value
       //Postcondition: The polynomial is evaluated at x and
       //               the value is returned
    public double evaluate(Double x)
    {
        double value = 0.0;

        Double coeff;

        for (int i = 0; i < length; i++)
        {
            coeff = retrieveAt(i);

            if (coeff != 0.0)
               value = value + coeff * Math.pow(x, i);
        }

        return value;
    }

       //Method to add two polynomials
       //Postcondition: This polynomial is added with the polynomial
       //               specified by the parameter right. A
       //               reference of the result is returned.
    public Polynomial add(Polynomial right)
    {
        int size = max(length, right.length);
        int i;
        Double sumCoeff;

        Double coeffP;
        Double coeffQ;

        Polynomial temp = new Polynomial(size);

        for (i = 0; i < min(length, right.length); i++)
        {
            coeffP = retrieveAt(i);
            coeffQ = right.retrieveAt(i);

            sumCoeff = coeffP + coeffQ;
            temp.replaceAt(i, sumCoeff);
        }

        if (size == length)
            for (i = min(length, right.length); i < length; i++)
                temp.replaceAt(i, retrieveAt(i));
        else
            for (i = min(length, right.length);
                                  i < right.length; i++)
                temp.replaceAt(i, right.retrieveAt(i));

        return temp;
    }

       //Method to subtract two polynomials
       //Postcondition: The polynomial specified by the
       //               parameter right is subtracted from this
       //               polynomial. A reference of the result
       //               is returned.
    public Polynomial subtract(Polynomial right)
    {
        int size = max(length, right.length);
        int i;
        Double diffCoeff;

        Double coeffP;
        Double coeffQ;

        Polynomial temp = new Polynomial(size);

        for (i = 0; i < min(length, right.length); i++)
        {
            coeffP = retrieveAt(i);
            coeffQ = right.retrieveAt(i);

            diffCoeff = coeffP - coeffQ;
            temp.replaceAt(i, diffCoeff);
        }

        if (size == length)
           for (i = min(length, right.length); i < length; i++)
               temp.replaceAt(i, retrieveAt(i));
        else
           for (i = min(length, right.length);
                                    i < right.length; i++)
           {
               Double coeff;
               coeff = right.retrieveAt(i);
               coeff = -coeff;
               temp.replaceAt(i, coeff);
           }

        return temp;
    }

       //Method to multiply two polynomials
       //Postcondition: This polynomial is multiplied with the
       //               polynomial specified by the parameter right.
       //               A reference of the result is returned.
    public Polynomial multiply(Polynomial right)
    {
        int size = max(length, right.length);
        int i;
        Double productCoeff;

        Double coeffP;
        Double coeffQ;
       
        Polynomial temp = new Polynomial(size);
       
        for (i = 0; i < min(length, right.length); i++)
        {
        coeffP = retrieveAt(i);
            coeffQ = right.retrieveAt(i);
       
        productCoeff = coeffP * coeffQ;
        temp.replaceAt(i, productCoeff);
        }
    if (size == length)
        for (i = min(length, right.length); i < length; i++)
            temp.replaceAt(i, retrieveAt(i));
    else
        for (i = min(length, right.length);
                              i < right.length; i++)
            temp.replaceAt(i, right.retrieveAt(i));

return temp;
            }

    public Polynomial division(Polynomial right)
    {
        int size = max(length, right.length);
        int i;
        Double productCoeff;

        Double coeffP;
        Double coeffQ;
       
        Polynomial temp = new Polynomial(size);
       
        for (i = 0; i < min(length, right.length); i++)
        {
        coeffP = retrieveAt(i);
            coeffQ = right.retrieveAt(i);
       
        productCoeff = coeffP / coeffQ;
        temp.replaceAt(i, productCoeff);
        }
    if (size == length)
        for (i = min(length, right.length); i < length; i++)
            temp.replaceAt(i, retrieveAt(i));
    else
        for (i = min(length, right.length);
                              i < right.length; i++)
            temp.replaceAt(i, right.retrieveAt(i));

return temp;
            }
   
       //Method to read the coefficients of a polynomial
    public void read()
    {
        Scanner console = new Scanner(System.in);

        Double x;

        System.out.println("The degree of this polynomial is: "
                         + (length - 1));
        for (int i = 0; i < length; i++)
        {
            System.out.print("Enter the coefficient of x^"
                             + i + ": ");

            x = console.nextDouble();
            replaceAt(i, x);
        }
    }

       //Method to return the string containing the polynomial
    public String toString()
    {
        int i;
        int firstNonzeroCoeff = 0;
        Double coeff = 0.0;
        String str = "";

        for (i = 0; i < length; i++)
        {
            coeff = retrieveAt(i);

            if (coeff != 0.0)
            {
               firstNonzeroCoeff = i;
               break;
            }
        }

        if (firstNonzeroCoeff < length)
        {
           if (firstNonzeroCoeff == 0)
              str = retrieveAt(firstNonzeroCoeff) + " ";
           else
              str = retrieveAt(firstNonzeroCoeff) + "x^"
                       + firstNonzeroCoeff + " ";

           for (i = firstNonzeroCoeff + 1; i < length; i++)
           {
               coeff = retrieveAt(i);

               if (coeff != 0.0)
                  if (coeff > 0.0)
                     str += "+ " + coeff + "x^" + i + " ";
                  else
                     str += "- " + (-coeff) + "x^" + i + " ";
           }
        }

        return str;
    }

       //Method to determine the smaller of x and y
       //Postcondition: The smaller of x and y is returned
    public int min(int x, int y)
    {
        if (x <= y)
           return x;
        else
           return y;
    }

       //Method to determine the larger of x and y
       //Postcondition: The larger of x and y is returned
    public int max(int x, int y)
    {
        if (x >= y)
            return x;
        else
            return y;
    }
}

Explanation / Answer

import java.util.ArrayList; /** Polynomial represents a polynomial from algebra. e.g. 4x^3 + 3x^2 - 5x + 2 */ public class Polynomial extends ArrayList { /** no-arg constructor returns a polynomial of degree 1, with value 0 */ public Polynomial() { // invoke superclass constructor, i.e. the // constructor of ArrayList with // parameter 1 (capacity, not size) super(1); // we want capacity at least 1 this.add(0,0.0); // uses autoboxing } /** get the degree of the polynomial. Always >= 1 @return degree of the polynomial */ public int getDegree() { return this.size() - 1; } /** Construct polynomial from int array of coefficients. The coefficients are listed in order from highest to lowest degree. The degree is the size of the array - 1. Example: 7x^3 - 2x^2 + 3 would be represented as {7,-2,0,3} Example: x^4 - 4 would be represented as {1,0,0,-4} @param coeffs array of coefficients from largest degree down to smallest. */ public Polynomial(int [] coeffs) { // invoke superclass constructor, i.e. the // constructor of ArrayList with // capacity parameter super(coeffs.length); for (int i=0;i=0;i--) { this.add(coeffs[i]); } } /** Return string respresentation of Polynomial. Leading coefficient has negative sign in front, with no space. Other signs have a space on either side. Coefficients that are ones should be omitted. Coefficients that are within 0.0000001 (i.e. 10^6) of an integer value should omit the .0. Terms with zero coefficients (to same tolerance) should be omitted. Examples:
1
2x - 3
x^2 - 5x + 6
-x^7 - 2x^5 + 3x^3 - 4x
See the source code of PolynomialTest.java for more examples. @return string representation of Polynomial */ public String toString() { String result=""; // for loop that steps down from the degree to 0 for (int i=this.getDegree(); i>=0 ; i-- ) { result += (get(i) + "x^" + i + " ") ; } return result; } /** determine whether two polynomials are equal (same degree, same coefficients) Uses 10^6 as tolerance for double equality. @param o arbitrary object to test for equality @return true if objects are equal, otherwise false */ public boolean equals(Object o) { // This is boiler plate code for a equals method in Java // Always do this first if (o == null) return false; if (!(o instanceof Polynomial)) return false; Polynomial p = (Polynomial) o; // @@@ TODO: Check the size of each ArrayList. // If they don't match, return false // @@@ TODO: Check each element in the ArrayList // If any don't match, return false. // Note that you are comparing double values, // so be sure to use a tolerance rather than == final double TOL=0.000001; // If you get to the end, and haven't found a reason // why they don't match, then they must match. return false; // STUB @@@ // @@@ RESTORE THIS LATER!!! return true; } /** Add two polynomials @param p polynomial to add to this polynomial @return new Polynomial representing this polynomial plus p */ public Polynomial plus (Polynomial p) { // Note that you can't predict the degree in advance. // If I add x^2 + 3x + 5 and -x^2 -3x -5, I get zero. // So, be careful! return new Polynomial (new int [] {-42}); // @@@ STUB! } /** Multiply two polynomials @param p polynomial to multiply this polynomial by @return new Polynomial representing this polynomial times p */ public Polynomial times (Polynomial p) { // Here, you'll always know the degree for sure // the algorithm is similar to multiplying integers // by hand (like you did in 3rd or 4th grade) return new Polynomial (new int [] {-42}); // @@@ STUB! } /** Subtract two polynomials @param p polynomial to subtract from this polynomial @return new Polynomial representing this polynomial minus p */ public Polynomial minus (Polynomial p) { // HINT: This one is easy if you think about the definition // of subtraction. You can just use plus and times // and do this with a one liner. return new Polynomial (new int [] {-42}); // @@@ STUB! } }
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