140 likes | 270 Views
E&CE 250: Project 2 PolynomialAsArray. Michael Jarrett msjarret@uwaterloo.ca. http://www.eng.uwaterloo.ca/~msjarret/ece250/Project2.ppt. Overview. Create a PolynomialAsArray class to represent a polynomial function. Information about this polynomial is stored in an array of doubles.
E N D
E&CE 250: Project 2PolynomialAsArray Michael Jarrett msjarret@uwaterloo.ca http://www.eng.uwaterloo.ca/~msjarret/ece250/Project2.ppt
Overview • Create a PolynomialAsArray class to represent a polynomial function. • Information about this polynomial is stored in an array of doubles. P(x) = anxn + an-1xn-1 + ... + a2x2 + a1x + a0 ECE250 Project2 Oct. 2nd, 2003
Requirements of Interface • Your PolynomialAsArray class must implement the Polynomial interface shown below. • You must include Polynomial.java in your project, EXACTLY as shown in the project description. Polynomial.java public interface Polynomial { int getDegree (); double getCoefficient (int i); void setCoefficient (int i, double c); void assign (Polynomial p); Polynomial plus (Polynomial p); Polynomial minus (Polynomial p); Polynomial times (Polynomial p); double eval (double x); Polynomial getDerivative (); String toString(); } ECE250 Project2 Oct. 2nd, 2003
Distilled from project description Methods • Polynomial() • You must provide this constructor; create P(x)=0. • double getCoefficient (int i) • Return 0 when i > degree • Consider the case of i < 0 • void assign (Polynomial p) • Makes this polynomial have the same coefficients as p. • Make sure both polynomials can later be changed independently. ECE250 Project2 Oct. 2nd, 2003
Distilled from project description Methods (2) • int getDegree() • Polynomial getDerivative () • Return polynomial represention (d/dx) of current polynomial. • String toString () • eg. 10x^3 – 2x + 3 • Special case: print 0 if P(x) = 0 • Must start with highest-degree term. • Must only contain non-zero terms. ECE250 Project2 Oct. 2nd, 2003
Distilled from project description Methods (3) • Polynomial plus (Polynomial p) • return this + p • Polynomial minus (Polynomial p) • return this - p • Polynomial times (Polynomial p) • return this * p • double eval (double x) • Evaluate polynomial at x; consider running time of algorithm you choose to do this. ECE250 Project2 Oct. 2nd, 2003
Distilled from project description Methods (4) • void setCoefficient (int i, double c) • If degree of polynomial changes, you must resize your array to be length degree+1. • Resize the array by creating a new array, and copying the contents of the old one. • Consider: c == 0 • Consider: i > degree Note: System.arraycopy(Object src, int srcOffset, Object dest, int destOffset, int length); ECE250 Project2 Oct. 2nd, 2003
Distilled from project description Rules • Your class must not be in a package. • You cannot modify interface Polynomial. • You may add members to your PolynomialAsArray class. • Implementation must use an array of doubles. • Length of array must always be equal to the degree of the polynomial plus one. ECE250 Project2 Oct. 2nd, 2003
Distilled from project description Marking Criteria • Correctness according to project description. • Handling of error conditions. • Correct resizing of polynomial. • Execution speed. • Pay careful attention to asymptotic running time as the degree gets large. • Code quality metrics. • Your code needs to have meaningful comments. ECE250 Project2 Oct. 2nd, 2003
Demo Program • We provide class Demo on the website. • This class will generate the output you must include with your submission. • Run as “java Demo PolynomialAsArray”. • Please include Demo.java in your jar file (you may not modify this file). • We will use the same class to test project 3! ECE250 Project2 Oct. 2nd, 2003
How Demo Works • Asks the Java Virtual Machine to load a class by name. • And then uses reflection to create a new PolynomialAsArray from the class object. • We then can run tests on the object using the methods of the Polynomial interface. Class clazz = Class.forName (className); Polynomial p = (Polynomial) clazz.newInstance (); ECE250 Project2 Oct. 2nd, 2003
Some Possible Errors • Not resizing array when needed. • Not checking for illegal arguments. • toString output not in correct form. • Too much or too little commented code. • Assuming you are being passed a PolynomialAsArray when the interface only specifies a Polynomial. ECE250 Project2 Oct. 2nd, 2003
Submission: Due Oct 16th, 6:00pm • Create a .jar file containing: • .java source files with author id & Demo file • .class files & Demo.class • submission form • outp2.txt • Verify that .jar file will execute directly on the Sun Java 2 SDK platform prior to submission: java –jar xxxxxxxxp2.jar PolynomialAsArray • Submit your .jar file to CourseBook. ECE250 Project2 Oct. 2nd, 2003
How to get help • msjarret@uwaterloo.ca • Newsgroup: uw.ece.ece250 • Office hour: Every Tuesday, 12:30-1:30 in E2-2365. ECE250 Project2 Oct. 2nd, 2003