Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Root of unity wikipedia , lookup

Quartic function wikipedia , lookup

Resultant wikipedia , lookup

Horner's method wikipedia , lookup

Gröbner basis wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Polynomial greatest common divisor wikipedia , lookup

Polynomial wikipedia , lookup

System of polynomial equations wikipedia , lookup

Polynomial ring wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Fundamental theorem of algebra wikipedia , lookup

Eisenstein's criterion wikipedia , lookup

Factorization wikipedia , lookup

Transcript
Assignment 1
CP-213
Winter 2017
Due: Wednesday, February 8, 5 pm
This assignment is worth 8% of the final grade.
POLYNOMIAL MANIPULATION
Write a Java program that manipulates univariate polynomials with integer coefficients. For this you
need to define a class Poly with methods that implement addition (add), subtraction (sub),
multiplication (mul), evaluation (evalAt), and integer root finding (iRoots). All polynomials
processed by your program are assumed to be in the same independent variable x. You also must
implement class Stack with elements of type Poly and the standard stack API. Finally, you need to
implement class Manipulate that processes input and produces the output according to the
following description.
INPUT SPECIFICATION
The input is a sequence of lines with each line containing one polynomial or instruction such as
ADD, SUB, MUL, EVALAT, ROOTS, QUIT, TEST, and PRINT. Every polynomial in the input
is represented as:
n an an-1 … al a0
where n>=0 – is the degree of the polynomial and ak,k=n, n-1, …,0 are integer coefficients. For
example:
4 2 0 -1 0 7
represents the polynomial 2x4-x2+7.
Input is processed line by line as follows:
 if the next line of the input stream is representation of a polynomial, create an object of
class Poly that represents this polynomial and push it on the stack;
 if the next line of the input stream is ADD, MUL, SUB, remove from the stack the two
topmost polynomials, apply the operation to them and push the result back on the stack;
 if the next line is EVALAT p (where p is an integer), evaluate the topmost polynomial
from the stack at x=p and output the result (the stack state is not changed);
 if the next line is ROOTS , find and output all integer roots of the topmost polynomial (the
stack state is not changed);
 if the next line is PRINT, output the topmost polynomial (the stack state is not changed);
 if the next line is TEST, remove from the stack the two topmost polynomials, and if they
are the same push only one of them back on the stack, otherwise push both of them back
on the stack in the original order;
 if the next line is QUIT, your program should output all polynomials that remain on the
stack and exit.
You can assume that input is always correct and valid. No exception handling is required.
Your program should not print anything else (e.g. it should not prompt user for input of lines).
Polynomial output requirements:
 monomials are printed in the descending order of their degrees;
 only monomials with non-zero coefficients are printed;
 monomial with nonzero coefficient ak is printed as ak x^k;
 monomial with nonzero coefficient a1 is printed as a1 x;
 monomial with coefficient a0 is printed as a0;
 zero polynomials is printed as 0;
 coefficients equal to 1 or -1 are not printed;
 the printed string should not contain sign sequences, such as “+-“ or “++”.
For example,
polynomial 4 2 0 -1 0 7 should be printed as 2 x^4 – x^2 + 7,
polynomial 5 0 0 -2 1 0 0 should be printed as -2 x^3 + x^2.
Class Poly API requirements:
Define immutable class Poly with the instance variables as shown below
public class Poly {
private int deg;
private int[] coef;
…
Define all necessary constructors, accessors, and
String toString()
returns pretty printed string representation of an object of class Poly.
boolean equals(Poly other)
tests equality of this and other object of class Poly.
Poly add(Poly other)
returns new object of class Poly which is the result of addition of this and other
object of class Poly.
Poly sub(Poly other)
returns new object of class Poly which is the result of subtraction of this and other
object of class Poly.
Poly mul(Poly other)
returns new object of class Poly which is the result of multiplication of this and other
object of class Poly.
int evalAt(int p)
returns result of evaluation of this polynomial at x=p.
void iRoots()
prints all integer roots of this object of class Poly.
Remarks/hints:


degree of the sum and difference of two polynomials is smaller or equal to the maximal
degree of the input polynomials; for example, degree of x^3-2x^2+1 is 3, degree of
-x^3+2x^2+x is 3, but degree of their sum x+1 is 1.
any nonzero integer root of a given polynomial (if it exists) divides the constant term; if
the constant term of a given polynomial is 0, then the same applies to the nonzero
coefficient of the monomial of the least degree; if the constant term of a given polynomial
is 0, then this polynomial has integer root equal to 0.
You cannot use Java’s class Stack but need to provide your own implementation of stack
functionality.
Use JavaDoc to document your code. Submit the code along with proper documentation.