Умножение полинома на константу в Java - PullRequest
6 голосов
/ 19 сентября 2011

У меня есть некоторые проблемы с умножением полинома на константу (double). Он работает, когда существует только один коэффициент, но когда присутствует более одного, он выдает ошибку ArrayIndexOutOfBounds и указывает на метод setCoefficient. Любая помощь? Спасибо

public class Poly {
    private float[] coefficients;

    public Poly() {
        coefficients = new float[1];
        coefficients[0] = 0;
    }

    public Poly(int degree) {
        coefficients = new float[degree+1];
        for (int i = 0; i <= degree; i++)
            coefficients[i] = 0;
    }

    public Poly(float[] a) {
        coefficients = new float[a.length];
        for (int i = 0; i < a.length; i++)
            coefficients[i] = a[i];
    }

    public int getDegree() {
        return coefficients.length-1;
    }

    public float getCoefficient(int i) {
        return coefficients[i];
    }

    public void setCoefficient(int i, float value) {
        coefficients[i] = value;
    }

    public Poly add(Poly p) {
        int n = getDegree();
        int m = p.getDegree();
        Poly result = new Poly(Poly.max(n, m));
        int i;
        for (i = 0; i <= Poly.min(n, m); i++) 
            result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
        if (i <= n) {
            //we have to copy the remaining coefficients from this object
            for ( ; i <= n; i++) 
                result.setCoefficient(i, coefficients[i]);
        } else {
            // we have to copy the remaining coefficients from p
            for ( ; i <= m; i++) 
                result.setCoefficient(i, p.getCoefficient(i));
        }
        return result;
    }

    public void displayPoly () {
        for (int i=0; i < coefficients.length; i++)
            System.out.print(" "+coefficients[i]);
        System.out.println();
    }

    private static int max (int n, int m) {
        if (n > m)
            return n;
        return m;
    }

    private static int min (int n, int m) {
        if (n > m)
            return m;
        return n;
    }

    public Poly multiplyCon (double c){
        int n = getDegree();
        Poly results = new Poly();
        // can work when multiplying only 1 coefficient
        for (int i =0; i <= coefficients.length-1; i++){
                // errors ArrayIndexOutOfBounds for setCoefficient
            results.setCoefficient(i, (float)(coefficients[i] * c)); 
        }
        return results;
    }
}

Ответы [ 2 ]

4 голосов
/ 19 сентября 2011

Заменить Poly results = new Poly(); на Poly results = new Poly(n);.

4 голосов
/ 19 сентября 2011

Я полагаю, вы должны заменить в методе multiplyCon

Poly results = new Poly();

с

Poly results = new Poly(n);

Конструктор Poly по умолчанию создает массив только с одним коэффициентом, что объясняет, почему умножение полинома с одним коэффициентом работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...