Assignment 3: Polynomials (100 pts)Due Wednesday, February 10 at midnight on CatalystPolynomial Review- Recall polynomials from your math classes.
- A polynomial is an expression consisting of variables and coefficients.
- For example, consider the following polynomial
8.5x - In this example, 8.5, 4.9 and 1.6 are all coefficients.
- 2, 1, and 0 (which are typically not shown) are all exponents.
- 8.5x
^{2}, 4.9x and 1.6 are all called terms.**Terms**are the combination of a coefficient and an exponent.
Polynomial Operations: Add- We can perform a variety of operations on polynomials.
- For example, we can add two polynomials together, like this:
8.5x
4.5x ^{3} + 8.5x^{2} + 7.5x + 4.9- Notice what happened above. The coefficients of the terms with the same power of exponent were added together (aka, adding like terms). Any terms that had a different exponent from all other terms were not added together.
- When we multiply polynomials, we must multiply each term together and then add the like terms. For example:
(x + 1) (4x
- We can evaluate a polynomial by plugging in a value for x. For example if we want to evaluate the polynomial 8.5x
^{2}+ 4.9x + 1.6 for x = 2, we would perform the following calculations:
8.5(2) ^{2} + 4.9(2) + 1.6 = 83.65Representing Polynomials in this Assignment- In math, we are allowed to ignore any term with a 0 coefficient. For example, in math, we would write the polynomial 2x
^{2}+ 0x + 1 as 2x^{2}+ 1. - For the purposes of this assignment, however, we will include the 0 coefficients when storing the terms of our polynomial and when printing it to the console.
- Likewise, in math, it is acceptable not to write the exponents 0 and 1 as part of the term. For example, in math, we would write the polynomial
2x^{2}+ 0x^{1}+ 1x^{0}as 2x2 + 1. - For the purposes of this assignment, however, we will include the 0 and 1 exponents when we store our terms and when printing the polynomial.
- In this assignment, we will be representing a polynomial using a list of Term objects.
- I have provided you with the Term class below.
- Note that the Term class has private coefficient and exponent fields.
- It also has two public constructors and some public accessors and manipulation procedures for the coefficient and exponent.
#ifndef TERM_H_ #define TERM_H_ using namespace std; class Term { private: double coefficient; int exponent; public: Term(): coefficient(0.0), exponent(0) {}; Term(double newCoefficient, int newExponent): coefficient(newCoefficient), exponent(newExponent){}; double getCoefficient() { return coefficient; } int getExponent() { return exponent; } void setCoefficient(double c) { coefficient = c; } void setExponent(int e) { exponent = e; } }; #endif /* TERM_H_ */ The Polynomial Class- We will be representing our polynomial as a List of term objects.
- Below is the header file for the Polynomial class.
- Note that the only field is the private List<Term> poly.
- You must use your doubly-linked and templated List from assignment 2 for this List poly.
#include "Term.h" #include <iostream> #include <cstddef> //for NULL using namespace std; class Polynomial { private: List<Term> poly; public: /**Constructors and Destructors*/ Polynomial(); //Default constructor; initializes an empty Polynomial ~Polynomial(); //Destructor. Frees memory allocated to the list Polynomial(const Polynomial &polynomial); //Copy constructor. Initializes the Polynomial to have the same elements as another Polynomial /**Manipulation Procedures*/ void insertTerm(int exponent, double coefficient); //inserts a single term into the polynomial /**Additional Operations*/ double evaluateTerm(Term t, double value); //A helper function for evaluate. //Evaluates a single term in the Polynomial by plugging in the value //For example: If the term is 3.0x2 and the value is 5.0 //This function will return 75.0 since 3.0*5.0*5.0=75.0 double evaluate(double value); //Evaluates the Polynomial by plugging in the value at all terms //If the Polynomial is empty, returns 0.0. //Calls the evaluateTerm function on each term in the Polynomial Polynomial operator + (const Polynomial& p); //Adds existing Polynomial to p, returning the result Polynomial operator * (const Polynomial& p); //Multiplies existing Polynomial by p, returning the resulting Polynomial //Worth 15 points extra credit void print(); //Prints the Polynomial to the console in the form of //<coefficient1>x<exponent1> + <coefficient2>x<exponenent2> + ... //For example: 2.5x4 + 3x3 + 0x2 + 8.1x1 +7.5x0 }; - Your task is to implement the above functions in a file called Polynomial.cpp (do not implement them in the header file) and test them in a file called PolynomialTest.cpp.
- You will need to test each of the functions as you write it to ensure that it is working properly.
- We will not be attempting to use superscript for any of the exponents.
- Also, we will be printing out exponents that are 0 and 1 and any term with a 0 coefficient.
- Therefore, below is an example of how your polynomials should be printed:
P1: 3.50x4 + 2x3 + 7x2 + 6x1 + 1x0 - Note that there is an x in the middle of each term. This is something that you will add as part of your print function for Polynomial.
**Important Note: You should also store the 0 term coefficients in your polynomial as well as any 0 and 1 exponents. This will make your life easier when you need to add (and multiply).**
- You may assume that all coefficients are inserted in decreasing order of exponent.
- Hint: you should thus be able to write this function using a single line of code.
- You will receive 15 points extra credit for correctly overloading the * operator for a polynomial.
- All other functions and operators in the header file are mandatory.
- If you had troubles with your List from assignment 2, you may also see me during office hours with your updated list. I will run my tests again and will give you up to 10 points back on your List assignment if all functions are now working. This offer expires on the due date for this assignment.
- When you are finished, upload your List.h, Term.h, Polynomial.h, Polynomial.cpp and PolynomialTest.cpp (This file should contain all of YOUR tests, not mine. I know what my test file looks like. I want to see YOURS here.) to Catalyst before the deadline.
- No late work will be accepted, so please pay attention to the deadline and ask me if there is a problem.
- I will be testing your Polynomial class functions with my own tests.
- You will receive 100 points if all tests pass, 75 points if 3/4 of the tests pass, etc + any extra credit you may have completed for this assignment.
- I will be using this test file to evaluate your code.
- However, I want to see YOUR OWN tests inside of PolynomialTest.cpp.
- You should approach this assignment by writing a function and then testing it inside of YOUR PolynomialTest.cpp file.
- Once you are certain your functions are working properly according to your own tests, only then should you run this test file on your code.
- Writing YOUR OWN tests as you go along is one of the best strategies for avoiding bugs and is a
of programming. (I cannot emphasize this point enough - as you can see!)**fundamental part**
/* * @author Jennifer Parrish */ |