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...
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...
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
Prove that the polynomial x^3 + x^2 – x + 1 has no integer roots
Prove that the polynomial x^3 + x^2 – x + 1 has no integer roots
Qu 3. In class, we discussed two measures for theory of mind: the Sally/Ann task, and...
Qu 3. In class, we discussed two measures for theory of mind: the Sally/Ann task, and the mountain task. Choose one of these two measures, and describe: (a) the procedure used to carry out the task (b) the possible outcomes (i.e. the possible responses the child might make), and how those outcomes are interpreted don't copy from the internet
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
Consider the polynomial f(x) = 3x 3 + 5x 2 − 58x − 40. Using MATLAB....
Consider the polynomial f(x) = 3x 3 + 5x 2 − 58x − 40. Using MATLAB. Find the three roots of the polynomial, i.e, x where f(x) = 0, using Newton’s method. Report the number of iterations taken by each algorithm using a tolerance of 10−8 .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT