Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

java - Errors in Polynomial Class

I'm having some problems with some methods that I created for a polynomial class. For the CheckZero, we're supposed to check if there are any leading zeros in the coefficients of the polynomial. If there are, it should re-size the coefficient array but shouldn't return anything. I can't get it to run properly. For the Differentiate method I constantly get a ArrayIndexOutOfBounds Error.

import java.util.ArrayList;
public class Poly {

    private float[] coefficients;
    public static void main (String[] args){
        float[] fa = {3, 2, 4};
        Poly test = new Poly(fa);

    }

    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(n);
        for (int i =0; i <= n; i++){ // can work when multiplying only 1 coefficient
            results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient
           }

        return results;
       }

    public Poly multiplyPoly (Poly p){
        int n = getDegree();
        int m = p.getDegree();
        Poly result = null;
        for (int i = 0; i <= n; i++){
            Poly tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method
            if (result == null){
                result = tmpResult;
            } else {
                result = result.add(tmpResult);
            }
        }
        return result;
    }
    public void checkZero(){
        int newDegree = getDegree();
        int length = coefficients.length;
        float testArray[] = coefficients;
        for (int i = coefficients.length-1; i>0; i--){
            if (coefficients[i] != 0){
                testArray[i] = coefficients[i];

            } 
            }
        for (int j = 0; j < testArray.length; j++){
            coefficients[j] = testArray[j];
        }
    }    

    public Poly differentiate(){
        int n = getDegree();
        int newPolyDegree = n - 1;
        Poly newResult = new Poly();
        if (n == 0){
            newResult.setCoefficient(0, 0);
        }
        for (int i =0; i<= n; i++){
            newResult.setCoefficient(i, coefficients[i+1] * (i+1));
        }
        return newResult;
    }


    public Poly multiByConstantWithDegree(double c, int degree){ //used specifically for multiply poly
        int oldPolyDegree = this.getDegree();
        int newPolyDegree = oldPolyDegree + degree;
        Poly newResult = new Poly(newPolyDegree);
        //set all coeff to zero
        for (int i = 0; i<= newPolyDegree; i++){
            newResult.coefficients[i] = 0;
        }
        //shift by n degree
        for (int j = 0; j <= oldPolyDegree; j++){
            newResult.coefficients[j+degree] = coefficients[j] * (float)c;
        }

        return newResult;
    }
}

Edit: whoops. Copied the wrong version. Fixed

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your CheckZero() method, you never resized the array. Also note that testArray references exactly the same variable as coefficients so that no copying ever occured in the first place.

What you have to do is find the degree of the leading coefficient, create a new float[] with that size, copy over non-zero coefficients to the new array, and set coefficients equal to that.

In your differentiate() method, you attempt to call setCoefficient() on newResult, which is initialized to null. Hence the null pointer exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...