0

How to construct a polynomial from a string representation such as "x + 3x^2 + 5x^3".. using an arrayList

1 Answers1

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:

http://www.ee.ucl.ac.uk/~mflanaga/java/Polynomial.html

duffymo
  • 305,152
  • 44
  • 369
  • 561