|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectPolynomial
public class Polynomial
Polynomial is a class to represent polynomials over variable x. For now, limited to integer coefficients and exponents. Each term is represented by a Term object.
Constructor Summary | |
---|---|
Polynomial()
Constructor for objects of class Polynomial |
|
Polynomial(java.lang.String init)
Constructor with initial polynomial as String. |
Method Summary | |
---|---|
void |
addTerm(Term t)
Add a term to this polynomial. |
boolean |
equals(java.lang.Object obj)
Compare this object to the specified object. |
double |
evaluate(int x)
Evaluates the polynomial for given value of variable x. |
static Polynomial |
product(Polynomial a,
Polynomial b)
Produces new polynomial which is the product of the two argument polynomials. |
static Polynomial |
sum(Polynomial a,
Polynomial b)
Produces new polynomial which is the sum of the two argument polynomials. |
java.lang.String |
toString()
Produce String representation of a Polynomial. |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Polynomial()
public Polynomial(java.lang.String init) throws java.lang.IllegalArgumentException
Postcondition: Polynomial is initialized with terms based on init String.
init
- the initial polynomial. This format MUST be followed: each term consists of
two integers separated by one or more spaces: an integer coefficient and an integer
exponent. Likewise, each term must be separated by one or more spaces. The terms
can be given in any order. For example "3 0 5 -2 -1 1" represents 3+5x^-2-x
java.lang.IllegalArgumentException
- if the argument string is not properly formatted.Method Detail |
---|
public void addTerm(Term t) throws java.lang.IllegalArgumentException
Precondition: t != null
Postcondition:this.equals(sum(OLD, polynomial consisting of term t))
t
- a term to add into this polynomial.
java.lang.IllegalArgumentException
- if the argument is null.public double evaluate(int x)
x
- the x-value at which to evaluate.
public static Polynomial sum(Polynomial a, Polynomial b) throws java.lang.IllegalArgumentException
x^2+2x-1
and 2x^3+3x
is 2x^3+x^2+3x+2x-1 = 2x^3+x^2+5x-1
.
Preconditions: a != null && b != null
a
- a polynomialb
- a polynomial
java.lang.IllegalArgumentException
- if either argument is null.public static Polynomial product(Polynomial a, Polynomial b) throws java.lang.IllegalArgumentException
2x+3
and 4x-1
is
(2x * 4x) + (2x * -1) + (3 * 4x) + (3 * -1) = 8x^2-2x+12x-3 = 8x^2+10x-3
.
Preconditions: a != null && b != null
a
- a polynomialb
- a polynomial
java.lang.IllegalArgumentException
- if either argument is null.public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
obj
- The object to compare with.
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |