How to construct a polynomial from a string representation such as "x + 3x^2 + 5x^3".. using an arrayList
Asked
Active
Viewed 585 times
0
-
how do you plan to represent a polynomial in Java in the first place? – Valentin Ruano Oct 01 '12 at 22:25
-
first you parse the expression. – Don Roby Oct 01 '12 at 22:26
1 Answers
2
You'll need Monomial and Polynomial classes.
I'd write a PolynomialFactory class that would know how to properly parse the String and return a Polynomial.
package model;
public class Monomial {
private double coeff;
private double expon;
// You add the rest.
}
public class Polynomial {
private List<Monomial> terms;
// You add the rest
}
public class PolynomialFactory {
public static Polynomial parse(String s) {
// You add the rest
Polynomial p = new Polynomial();
return p;
}
}
Or just find a library, like this one:

duffymo
- 305,152
- 44
- 369
- 561