1 Introduction This exam is worth 80 points. Forty points each for the class and
ID: 3673235 • Letter: 1
Question
1 Introduction This exam is worth 80 points. Forty points each for the class and unit tests For this exam you will create a single class, polynomial, which implements a fixed-capacity polynomial with integer coefficients. A polynomial object could represent the polynomial 5r3 for example. 2 Getting Started Create the class polynomial and a file for unit tests polynomials.tests.cpp. Note: Please stick with the file and method name conventions used in this document. The following sections describe the polynomial class. As you build the class, write a thorough set of unit tests for it 3 Instance variables Your class should have a static constant to represent the CAPACITY of the polynomial. The CAPACITY must be set to at least 50 to pass my unit tests. The polynomial class should have the following instance variables: int coefficients [CAPACITY)-the coefficient in each term in the polynomial starting from the zero-order term and working up. For example, the polynomial 3r-5r+2 would be represented by the array (2.-5,0.3 std:: sizet used: number of coefficients being used. In the example above, used-4. Note that used is one more than the degree of the polynomial. 4 Instance methods Your class should define the following instance methods polynomial int c std sizet n) construct the polynomial with the n specified coefficients c int get.degree 0 const - return the degree of the polynomial. For example, 5 +2r4 +1 has degree 9. int get.coefficient (std: sizeconst-(precondition:Explanation / Answer
main.cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main() {
double coef1[3] = {1, 2, 3};
Polynomial poly1(coef1, 2);
cout << "Poly1: ";
poly1.displayPolynomial();
double coef2[6] = {5, 3, 2, 6, 4, 3};
Polynomial poly2(coef2, 5);
cout << "Poly2: ";
poly2.displayPolynomial();
Polynomial poly3 = poly1 + poly2;
cout << "Poly1 + Poly2 (Copy Constructor): ";
poly3.displayPolynomial();
poly3 = poly1 - poly2;
cout << "Poly1 - Poly2: ";
poly3.displayPolynomial();
poly3 = poly1 * poly2;
cout << "Poly1 * Poly2: ";
poly3.displayPolynomial();
poly1 += poly2;
cout << "Poly1 += Poly2: ";
poly1.displayPolynomial();
poly1 -= poly1;
cout << "Poly1 -= Poly1: ";
poly1.displayPolynomial();
poly2 *= poly1;
cout << "Poly2 *= Poly1: ";
poly1.displayPolynomial();
return 0;
}
Polynomial.cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
// Default Constructor
Polynomial::Polynomial() {
degree = 0;
coefficients = new double[degree + 1];
coefficients[0] = 0;
}
Polynomial::Polynomial(int _degree) {
degree = _degree;
coefficients = new double[degree + 1];
for(int i = 0; i <= degree; i++)
coefficients[i] = 0;
}
Polynomial::Polynomial(double *_coefficients, int _degree) {
degree = _degree;
coefficients = new double[degree + 1];
for(int i = 0; i <= degree; i++)
coefficients[i] = _coefficients[i];
}
Polynomial::~Polynomial() {
delete[] coefficients;
}
void Polynomial::setCoefficient(double _coeff, int _degree) {
if(degree < _degree || _degree < 0) {
cout << "Degree Value out of Range" << endl;
return;
}
coefficients[_degree] = _coeff;
}
double Polynomial::getCoefficient(int _degree) const {
if(degree < _degree || _degree < 0) {
return 0;
}
return coefficients[_degree];
}
int Polynomial::getDegree() const {
return degree;
}
void Polynomial::displayPolynomial(void) {
for(int i = degree; i >= 0; i--)
cout << coefficients[i] << "x^" << i << " ";
cout << endl;
}
Polynomial::Polynomial(const Polynomial& arg) {
degree = arg.getDegree();
coefficients = new double[degree + 1];
for(int i = 0; i <= degree; i++)
coefficients[i] = arg.getCoefficient(i);
}
Polynomial& Polynomial::operator=(const Polynomial& arg) {
if(this == &arg)
return *this;
degree = arg.getDegree();
coefficients = new double[degree + 1];
for(int i = 0; i <= degree; i++)
coefficients[i] = arg.getCoefficient(i);
return *this;
}
Polynomial operator+(const Polynomial& arg1, const Polynomial& arg2) {
int deg = arg1.getDegree() > arg2.getDegree() ? arg1.getDegree() : arg2.getDegree();
Polynomial Sum(deg);
for(int i = 0; i <= deg; i++)
Sum.setCoefficient(arg1.getCoefficient(i) + arg2.getCoefficient(i), i);
return Sum;
}
Polynomial operator-(const Polynomial& arg1, const Polynomial& arg2) {
int deg = arg1.getDegree() > arg2.getDegree() ? arg1.getDegree() : arg2.getDegree();
Polynomial Difference(deg);
for(int i = 0; i <= deg; i++)
Difference.setCoefficient(arg1.getCoefficient(i) - arg2.getCoefficient(i), i);
return Difference;
}
Polynomial operator*(const Polynomial& arg1, const Polynomial& arg2) {
int degree1 = arg1.getDegree();
int degree2 = arg2.getDegree();
double coef1;
double coef2;
Polynomial Product(degree1 + degree2);
for(int i = 0; i <= degree1; i++)
for(int j = 0; j <= degree2; j++) {
coef1 = arg1.getCoefficient(i);
coef2 = arg2.getCoefficient(j);
Product.setCoefficient(coef1*coef2+Product.getCoefficient(i+j), i+j);
}
return Product;
}
Polynomial& Polynomial::operator+=(const Polynomial& arg) {
int deg = degree > arg.getDegree() ? degree : arg.getDegree();
Polynomial Sum(deg);
for(int i = 0; i <= deg; i++)
if(i <= degree)
Sum.setCoefficient(coefficients[i] + arg.getCoefficient(i), i);
else
Sum.setCoefficient(arg.getCoefficient(i), i);
*this = Sum;
return *this;
}
Polynomial& Polynomial::operator-=(const Polynomial& arg) {
int deg = degree > arg.getDegree() ? degree : arg.getDegree();
Polynomial Difference(deg);
for(int i = 0; i <= deg; i++)
if(i <= degree)
Difference.setCoefficient(coefficients[i] - arg.getCoefficient(i), i);
else
Difference.setCoefficient(0 - arg.getCoefficient(i), i);
*this = Difference;
return *this;
}
Polynomial& Polynomial::operator*=(const Polynomial& arg) {
int degree1 = degree;
int degree2 = arg.getDegree();
double coef1;
double coef2;
Polynomial Product(degree1 + degree2);
for(int i = 0; i <= degree1; i++)
for(int j = 0; j <= degree2; j++) {
coef1 = coefficients[i];
coef2 = arg.getCoefficient(j);
Product.setCoefficient(coef1*coef2+Product.getCoefficient(i+j), i+j);
}
*this = Product;
return *this;
}
Polynomial.h
#ifndef _Polynomial_h
#define _Polynomial_h
class Polynomial {
private:
double *coefficients;
int degree;
public:
Polynomial(); // Default Constructor
Polynomial(int); // Constructor
Polynomial(double[], int); // Constructor
~Polynomial(); // Destructor
void setCoefficient(double, int); // Set Coefficient Function
double getCoefficient(int) const; // Get Coefficient Function
int getDegree() const; // Get Degree Function
void displayPolynomial(); // Displays the Polynomial
Polynomial(const Polynomial& arg); // Copy Constructor
Polynomial& operator=(const Polynomial&); // Assignment Operator
// Overloaded Operators
friend Polynomial operator+(const Polynomial&, const Polynomial&);
friend Polynomial operator-(const Polynomial&, const Polynomial&);
friend Polynomial operator*(const Polynomial&, const Polynomial&);
Polynomial& operator+=(const Polynomial&);
Polynomial& operator-=(const Polynomial&);
Polynomial& operator*=(const Polynomial&);
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.