2011-09-20 7 views
0

다항식 클래스에 대해 만든 몇 가지 메서드에 문제가 있습니다. CheckZero의 경우 다항식의 계수에 앞에 오는 0이 있는지 확인해야합니다. 존재하는 경우 계수 배열의 크기를 조정해야하지만 아무 것도 반환하지 않아야합니다. 나는 제대로 달릴 수 없다. 차별화 방법에 대해서는 항상 ArrayIndexOutOfBounds 오류가 발생합니다.다항식 클래스의 오류

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; 
    } 
} 

편집 : whoops. 잘못된 버전을 복사했습니다.

고정

답변

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

공지 사항 newResult = null이 그. 그런 다음 newResult.setCoefficient. 명백한 NullPointerException. 상기 하나 개의 명백한 문제에

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]; 
    } 
}  

라인이 testArray [] = 계수 플로트 이고; testArray계수과 동일한 메모리 위치를 가리 키도록 설정합니다. 따라서 testArray의 값을 변경하면 계수가으로 변경됩니다.

1

CheckZero() 방법에서는 배열의 크기를 조정하지 않았습니다. testArraycoefficients과 정확히 동일한 변수를 참조하므로 복사가 처음에는 발생하지 않습니다.

선도 계수의 차수를 찾고 그 크기로 새로운 float[]을 만들고 0이 아닌 계수를 새로운 배열에 복사 한 다음 coefficients과 같게 설정하면됩니다. 당신의 differentiate() 방법에서

, 당신은 null로 초기화된다 newResultsetCoefficient() 전화를 시도합니다. 따라서 널 포인터 예외.

+0

+1 좋은 답변 인 경우, ** testArray ** 및 ** 계수 **가 동일한 메모리를 가리킬 때 복사가 발생한다고 말하면 -1이어야합니다. –

+0

Woops, 그것을 보지 못했습니다. 답변이 수정되었습니다. 좋은 캐치 :) – tskuzzy

관련 문제