E&CE 250: Project 2 PolynomialAsArray

14
E&CE 250: Project 2 E&CE 250: Project 2 PolynomialAsArray PolynomialAsArray Michael Jarrett msjarret @ uwaterloo .ca p://www.eng.uwaterloo.ca/~msjarret/ece250/Project2.

description

E&CE 250: Project 2 PolynomialAsArray. Michael Jarrett [email protected]. 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. - PowerPoint PPT Presentation

Transcript of E&CE 250: Project 2 PolynomialAsArray

Page 1: E&CE 250: Project 2 PolynomialAsArray

E&CE 250: Project 2E&CE 250: Project 2PolynomialAsArrayPolynomialAsArray

Michael [email protected]

http://www.eng.uwaterloo.ca/~msjarret/ece250/Project2.ppt

Page 2: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 2

OverviewOverview Create a PolynomialAsArray class to represent a polynomial function.

Information about this polynomial is stored in an array of doubles.

P(x) = aP(x) = annxxnn + a + an-1n-1xxn-1n-1 + ... + a + ... + a22xx22 + a + a11x + ax + a00

Page 3: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 3

Requirements of InterfaceRequirements of Interface

Polynomial.javapublic 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();}

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.

Page 4: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 4

MethodsMethods 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.

Distilled from project description

Page 5: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 5

Methods (2)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.

Distilled from project description

Page 6: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 6

Methods (3)Methods (3)Polynomial plus (Polynomial p)

– return this + pPolynomial minus (Polynomial p)

– return this - pPolynomial times (Polynomial p)

– return this * pdouble eval (double x)

– Evaluate polynomial at x; consider running time of algorithm you choose to do this.

Distilled from project description

Page 7: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 7

Methods (4)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

Distilled from project description

Note: System.arraycopy(Object src, int srcOffset, Object dest, int destOffset, int length);

Page 8: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 8

RulesRulesYour 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.

Distilled from project description

Page 9: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 9

Marking CriteriaMarking CriteriaCorrectness 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.

Distilled from project description

Page 10: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 10

Demo ProgramDemo 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!

Page 11: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 11

How Demo WorksHow Demo WorksAsks 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 ();

Page 12: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 12

Some Possible ErrorsSome Possible ErrorsNot 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.

Page 13: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 13

Submission: Due Oct 16Submission: Due Oct 16thth, 6:00pm, 6:00pmCreate a .jar file containing:

.java source files with author id & Demo file.class files & Demo.classsubmission formoutp2.txt

Verify that .jar file will execute directly on the Sun Java 2 SDK platform prior to submission:

java –jar xxxxxxxxp2.jar PolynomialAsArraySubmit your .jar file to CourseBook.

Page 14: E&CE 250: Project 2 PolynomialAsArray

ECE250 Project2 Oct. 2nd, 2003 14

How to get helpHow to get help

[email protected]: uw.ece.ece250Office hour:

Every Tuesday, 12:30-1:30 in E2-2365.