Question

In: Computer Science

Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements...

Task 3: Class Polynomial (Version 2)

In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent.

public class Polynomial

{

// P is a linear array of Terms

private Term[ ] P;

// Re-implement the six methods of Polynomial (including the constructor)

}

Solutions

Expert Solution

public class Polynomial {
    // The coefficients of the polynomial.
    // Array index i stores the degree i coefficient.
    // For example, z^2 + 2z + 3 is stored is {3, 2, 1}.
    private final Term[] coeffs;

    public Polynomial(Term[] coeffs) {
        // Why copy the array? Well, if we don't make a copy, the person
        // who gave us the array could change the values later (which
        // could be confusing).
        this.coeffs = new Term[coeffs.length];
        
        for (int i = 0; i < coeffs.length; i++) {
            this.coeffs[i] = coeffs[i];
        }
    }

    public Polynomial() {
        this(new Term[0]);
    }

    // This is a static factory method, although it could technically be
    // made a constructor. My personal preference is to have only one "real"
    // constructor that does all the initialization work, plus maybe a few
    // "simplified" constructors that delegate to it using `this(...)` and
    // pass in default arguments (in this file, the no-argument constructor
    // is an example of a "simplified" constructor). I consider this method
    // different enough to warrant a named factory method.
    public static Polynomial linear(Term a, Term b) {
        return new Polynomial(new Term[]{b, a});
    }

    // Technically incorrect: a polynomial like 0z^2 + z + 1 should have
    // degree 1 (the highest non-zero term is the degree-1 term), but this
    // will report degree 2. How could you fix that?
    public int degree() {
        return coeffs.length - 1;
    }

    // Why use a getter here?
    public Term getCoeff(int i) {
        return coeffs[i]; // may throw IndexOutOfBoundsException
    }

    // Like Term, this class is immutable so we return a new instance.
    public Polynomial add(Polynomial other) {
        int newDeg = Math.max(degree(), other.degree());
        Term[] newCoeffs = new Term[newDeg + 1];
        for (int i = 0; i < newCoeffs.length; i++) {
            newCoeffs[i] = getCoeff(i).add(other.getCoeff(i));
        }
        return new Polynomial(newCoeffs);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int exp = 0; exp < coeffs.length; exp++) {
            if (exp > 0) {
                sb.append(" + ");
            }
            sb.append("(");
            sb.append(coeffs[exp].toString());
            sb.append(")z^");
            sb.append(Integer.toString(exp));
        }
        return sb.toString();
    }
}

Related Solutions

Task 3: a) A second-degree polynomial in x is given by the expression = + +...
Task 3: a) A second-degree polynomial in x is given by the expression = + + , where a, b, and c are known numbers and a is not equal to 0. Write a C function named polyTwo (a,b,c,x) that computes and returns the value of a second-degree polynomial for any passed values of a, b, c, and x. b) Include the function written in part a in a working program. Make sure your function is called from main() and...
c++ please Your task is to write the implementation for a class of polynomial operations. Your...
c++ please Your task is to write the implementation for a class of polynomial operations. Your will write the code for: 2 constructors, a destructor, print, addition, multiplication , differentiation and integration of polynomials. The polynomials will be comprised of linked TermNodes. struct TermNode { int exp; // exponent double coef; // coefficient TermNode * next; }; class Polynomial { public: Polynomial (); // default constructor Polynomial (int r, int c); // constructor makes a 1 node polynomial Polynomial(const Polynomial...
// TASK #2 Add an import statement for the Scanner class // TASK #2(Alternate) // Add...
// TASK #2 Add an import statement for the Scanner class // TASK #2(Alternate) // Add an import statement for the JOptionPane class /** This program demonstrates how numeric types and operators behave in Java. */ public class NumericTypes { public static void main (String [] args) { // TASK #2 Create a Scanner object here // (not used for alternate) // Identifier declarations final int NUMBER = 2 ; // Number of scores final int SCORE1 = 100; //...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
C# Only Create a class named Customer that implements IComparable interface. Create 3 Customer class fields:...
C# Only Create a class named Customer that implements IComparable interface. Create 3 Customer class fields: Customer number, customer name, and amount due. Create automatic accessors for each field. Create an Customer class constructor that takes parameters for all of the class fields and assigns the passed values through the accessors. Create a default, no-argument Customer class constructor that will take no parameters and will cause default values of (9, "ZZZ", 0) to be sent to the 3-argument constructor. Create...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes:...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes: "Bus", "Car", and "Motorcycle". They should contain a variable storing number of seats (bus) / weight (car) / number of tires (motorcycle), constructor, destructor and a print_info function that will print all the information about the vehicle. PK 3. The constructors should take as parameters: company, year and number of seats / weight / number of tires. Inside it assign the company name and...
let α ∈ C be a zero of the polynomial t^3 − 4t + 2 =...
let α ∈ C be a zero of the polynomial t^3 − 4t + 2 = 0 and let R = {a1 + bα + cα^2 : a,b,c ∈ Z}. Show that R is a integral domain and Show that α − 1 and 2α − 1 are units in R. [Hint: what if x = t + 1?
Taylor Polynomial HW 1) Evaluate cos ( 2 π / 3 ) on your calculator and...
Taylor Polynomial HW 1) Evaluate cos ( 2 π / 3 ) on your calculator and using the first 4 terms of the TP for cos x. 2) Integrate cos ( x^3 ), from 0 to π / 6, using the first 3 terms of the TP for cos x. 3) Evaluate e^x at x = .4 on your calculator and using the first 5 terms of the TP for e^ x. 4) Integrate e^x3, from 0 to .3, using...
Is there a fourth degree polynomial that takes these values? x 1 -2 0 3 -1...
Is there a fourth degree polynomial that takes these values? x 1 -2 0 3 -1 7 y -2 -56 -2 4 -16 376
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT