Class Polynomial
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<Integer>
-
- edu.ucsb.cs56.polynomial.Polynomial
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable<Integer>
,Collection<Integer>
,List<Integer>
,RandomAccess
public class Polynomial extends ArrayList<Integer>
Polynomial represents a polynomial from algebra. e.g. 4x3 + 3x2 - 5x + 2- Author:
- Fill In Your Name
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
debug
AssignPolynomial.debug
to @{code true} orfalse
to turn debugging output on or off.-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description Polynomial()
no-arg constructor returns a polynomial of degree 1, with value 0Polynomial(int[] coeffsHighToLow)
Construct polynomial from int array of coefficients.Polynomial(String s)
Construct polynomial from string representation that matches the output format of the Polynomial toString method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static int
degreeOfPolynomialCoeffsHighToLow(int[] coeffsHighToLow)
Given anint []
of coefficients from highest to lowest power (where the x^0 term has the highest index in the array) find the degree of the polynomial represented by the coefficients shown.static int
degreeOfPolynomialCoeffsLowToHigh(int[] coeffsLowToHigh)
Given anint []
of coefficients from lowest to highest degree (where the index in the array matches the power of the x term), find the degree of the polynomial represented by the coefficients shown.boolean
equals(Object o)
determine whether two polynomials are equal (same degree, same coefficients)int
getDegree()
Get the degree of the polynomial.static int[]
highToLow(int[] coeffsLowToHigh)
Convert a list of coefficients in order from lowest degree to highest degree (where index matches power of the x term) to one in order from highest degree to lowest degree (the order used for input to the Polynomial constructor).static int[]
lowToHigh(int[] coeffsHighToLow)
Convert a list of coefficients in order from highest degree to lowest degree (the order used for input to the Polynomial constructor) to one where the order is lowest degree to highest degree (where index matches power of thex
term).static void
main(String[] args)
Main for testing constructing Polynomials from strings, and for testing plus, minus and times.Polynomial
minus(Polynomial p)
return a new Polynomial which has as its value the this polynomial minus the one passed in as a parameter.Polynomial
plus(Polynomial p)
return a new Polynomial which has as its value the this polynomial plus the one passed in as a parameter.Polynomial
times(Polynomial p)
return a new Polynomial which has as its value the this polynomial times the one passed in as a parameter.String
toString()
Return string respresentation of Polynomial according to the following rules:static void
usage()
Print Usage message for Polynomial main-
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
Methods inherited from class java.util.AbstractCollection
containsAll
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Methods inherited from interface java.util.List
containsAll
-
-
-
-
Constructor Detail
-
Polynomial
public Polynomial()
no-arg constructor returns a polynomial of degree 1, with value 0
-
Polynomial
public Polynomial(int[] coeffsHighToLow)
Construct polynomial from int array of coefficients. The coefficients are listed in order from highest to lowest degree. The degree is the size of the array - 1.
Examples:
- 7x3 - 2x2 + 3 would be represented as {7,-2,0,3}
- x4 - 4 would be represented as {1,0,0,0,-4}
NOTE that the order of coefficients is not necessarily the way they will be stored in the array. That is, the order of coefficients in the array passed in is from highest degree down to lowest degree, so for a cubic:
-
coeffs[0]
is the x3 coefficient -
coeffs[1]
is the x2 coefficient -
coeffs[2]
is the x coefficient -
coeffs[3]
is the constant term
It is done this way so that when initializing a polynomial from an array literal, the order of coefficients mirrors the way polynomials are typically written, from highest order term to lowest order term:
Example: to represent 4x3 - 7x2 + 5,
use:Polynomial p = new Polynomial (new int [] {4,-7,0,5});
NOT:Polynomial p = new Polynomial (new int [] {5,0,-7,4});
- Parameters:
coeffsHighToLow
- array of coefficients in order from largest degree down to smallest. If array has length zero, return a polynomial of degree zero with constant value zero.
-
Polynomial
public Polynomial(String s)
Construct polynomial from string representation that matches the output format of the Polynomial toString method. That is, you should be able to do:Polynomial p = new Polynomial("0"); Polynomial p = new Polynomial("1"); Polynomial p = new Polynomial("-4"); Polynomial p = new Polynomial("2x - 3"); Polynomial p = new Polynomial("x^2 - 5x + 6"); Polynomial p = new Polynomial("x^2 - x - 1"); Polynomial p = new Polynomial("x^2 - x"); Polynomial p = new Polynomial("-x^7 - 2x^5 + 3x^3 - 4x");
And for any Polymomial object p, the following test should pass:
assertEquals(new Polynomial(p.toString()), p);
- Parameters:
s
- string representation of Polynomial
-
-
Method Detail
-
getDegree
public int getDegree()
Get the degree of the polynomial. Should be always >=1- Returns:
- degree of the polynomial
-
toString
public String toString()
Return string respresentation of Polynomial according to the following rules:
- Leading coefficient if negative, has negative sign in front, with no space.
-
Other
+
and-
signs have a space on either side. - Coefficients that are ones should be omitted (except in the constant term, i.e. x0 term).
-
Terms with zero coefficients should be omitted, except in the special
case where the polynomial is of degree zero, and the constant term
is zero. In that case,
"0"
should be returned.
Examples of String Representation of Polynomials Math notation String Representation 0 0
1 1
-4 -4
2x - 3 2x - 3
x2 - 5x + 6 x^2 - 5x + 6
x2 - x - 1 x^2 - x - 1
x2 - x x^2 - x
-x7 - 2x5 + 3x3 - 4x -x^7 - 2x^5 + 3x^3 - 4x
- Overrides:
toString
in classAbstractCollection<Integer>
- Returns:
- string representation of Polynomial
-
equals
public boolean equals(Object o)
determine whether two polynomials are equal (same degree, same coefficients)
-
degreeOfPolynomialCoeffsLowToHigh
public static int degreeOfPolynomialCoeffsLowToHigh(int[] coeffsLowToHigh)
Given an
int []
of coefficients from lowest to highest degree (where the index in the array matches the power of the x term), find the degree of the polynomial represented by the coefficients shown.This is a utility method that may be useful in converting between the low to high and high to low representations of coefficients, both in user programs that use the Polynomial class, as well as in internal routines used to implement Polynomial methods.
- Parameters:
coeffsLowToHigh
- coefficients of a polynomial in order from lowest degree to highest degree. May have trailing zeros.- Returns:
- index of the highest degree non-zero term (if any, otherwise
0
)
-
degreeOfPolynomialCoeffsHighToLow
public static int degreeOfPolynomialCoeffsHighToLow(int[] coeffsHighToLow)
Given an
int []
of coefficients from highest to lowest power (where the x^0 term has the highest index in the array) find the degree of the polynomial represented by the coefficients shown.This is a utility method that may be useful in converting between the low to high and high to low representations of coefficients, both in user programs that use the Polynomial class, as well as in internal routines used to implement Polynomial methods.
- Parameters:
coeffsHighToLow
- coefficients of a polynomial in order from highest power of x first to lowest power of x last. May have leading zeros.- Returns:
- index of the highest degree non-zero term (if any, otherwise
0
)
-
lowToHigh
public static int[] lowToHigh(int[] coeffsHighToLow)
Convert a list of coefficients in order from highest degree to lowest degree (the order used for input to the Polynomial constructor) to one where the order is lowest degree to highest degree (where index matches power of the
x
term).- Parameters:
coeffsHighToLow
- coefficients of a polynomial in order from highest degree to lowest degree. May have leading zeros.- Returns:
- An
int []
with coefficients in order from lowest degree to highest degree. No trailing zeros.
-
highToLow
public static int[] highToLow(int[] coeffsLowToHigh)
Convert a list of coefficients in order from lowest degree to highest degree (where index matches power of the x term) to one in order from highest degree to lowest degree (the order used for input to the Polynomial constructor).- Parameters:
coeffsLowToHigh
- coefficients of a polynomial in order from lowest degree to highest degree. May have trailing zeros.- Returns:
- An
int []
with coefficients in order from highest degree to lowest degree. No leading zeros.
-
plus
public Polynomial plus(Polynomial p)
return a new Polynomial which has as its value the this polynomial plus the one passed in as a parameter.- Parameters:
p
- the Polynomial to add to this one- Returns:
- sum of this Polynomial and p
-
times
public Polynomial times(Polynomial p)
return a new Polynomial which has as its value the this polynomial times the one passed in as a parameter.- Parameters:
p
- the Polynomial to multiply this one by- Returns:
- product of this Polynomial and p
-
minus
public Polynomial minus(Polynomial p)
return a new Polynomial which has as its value the this polynomial minus the one passed in as a parameter.- Parameters:
p
- the Polynomial to subtract from this one- Returns:
- the result of this Polynomial minus p
-
usage
public static void usage()
Print Usage message for Polynomial main
-
main
public static void main(String[] args)
Main for testing constructing Polynomials from strings, and for testing plus, minus and times.
At Unix command line:
- Use single quotes to make the entire Polynomial be a single argument
- Use
\*
when operator is*
(to avoid having * expanded as a wildcard.)
Example:
java -jar target/polynomial-1.0.jar 'x^2 + 2x + 3' \* 'x - 4'
- Parameters:
args
- Command Line Arguments
-
-