{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Project 1 - Polynomial Class - Robert J England II COS 221...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Robert J. England II COS 221 Project 1 Polynomial Class Project done for Prof. Larry Latour Wednesday, October 11, 2006
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The following is the actual code used in the program. There are two classes; one is the Polynomial class, which provides the bulk of the code; the other is the Test class, which tests the Polynomial class extensively. The main program does nothing more than create a Test object, and the constructor of that object performs all of the testing. // Polynomial.cpp : // Polynomial class created by Robert J. England II // for COS 221 with Larry Latour // at the University of Maine in Orono // Friday, October 6, 2006 // #include "stdafx.h" #include <iostream> #include <math.h> #include "chplot.h" using namespace std; class Polynomial { public : // Constructors Polynomial(); // POST: A polynomial equivalent to zero is created Polynomial( double a0); // POST: A polynomial equivalent to constant a0 is created Polynomial(Polynomial & source); // POST: A new polynomial equivalent to this polynomial is // created // Clone method Polynomial clone(); // POST: A new polynomial equivalent to this polynomial is // created and returned // Modification methods void addToCoef( double amount, int k); // PRE: k >= 0 // POST: The value amount is added to the kth coefficient of the // polynomial void assignCoef( double newCoefficient, int k); // PRE: k >= 0 // POST: The kth coefficient of the polynomial is changed to the // value newCoefficient void clear(); // POST: The polynomial is set equivalent to zero void reserve( int newSize); // PRE: size must be greater than the current degree of the // polynomial // POST: Memory is reserved for size coefficients for the // polynomial // Accessor methods double coefficient( int k); // PRE: k >= 0 // POST: The kth coefficient of the polynomial is returned // If the degree of the polynomial is less than k, 0 is returned int getDegree() { return degree;};
Image of page 2
// POST: The degree of the polynomial is created int nextTerm( int k); // POST: The next greater nonzero coefficient above k is // returned; if k is the greatest coefficient, -1 is returned; // if k is negative, function finds first nonzero coefficient // Evaluation method double evaluate( double x); // POST: The value of the polynomial at value x is returned // Arithmetic operators Polynomial operator + (Polynomial other); // POST: The algebraic sum of the polynomials is returned Polynomial operator - (Polynomial other); // POST: The algebraic difference of the polynomials is returned Polynomial operator * (Polynomial other); // POST: The algebraic product of the polynomials is returned Polynomial derivative(); // POST: The derivative of the polynomial is returned // Plot method void Polynomial::plotPoly( double low, double high, int res, char * title); // PRE: high > low; res > 0 // POST: The polynomial is plotted between values low and high, // with resolution res and graph title title private : // data fields double * coefficients; int degree; int size; }; Polynomial::Polynomial() // POST: A polynomial equivalent to zero is created { size = 0; degree = 0; coefficients = new double [size]; assignCoef(0, 0); } Polynomial::Polynomial( double a0) // POST: A polynomial equivalent to constant a0 is created { size = 0; degree = 0; coefficients = new double [size]; assignCoef(a0, 0); }
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern