2010-11-23 2 views
-3

내 추가 메서드가 작동하지만 디버그 할 때 값이 변경되어 추가 메서드의 맨 아래에 새 SparsePolynomial 개체가 만들어지면 추가 정보가있는 위치를 알 수 없습니다. 에서 온다. 누군가 나를 도울 수 있습니까? ,Java의 다항식

import java.util.ArrayList; 

public class SparsePolynomial { 

    private ArrayList<Polynomial> polynomialarraylist = new ArrayList<Polynomial>(); 

    /** 
    * Constructor to get values of an arraylist of integers 
    * @param arraylist that contains the integer values used for the polynomials 
    */ 
    public SparsePolynomial(ArrayList<Integer> arrayList) 
    { 
     //MODIFIDED: polynomialarraylist 
     //EFFECT: constructs the arraylist of polynomials based off the arraylist of integers 
     insertIntoPolynomialArray(arrayList); 
    } 

    /** 
    * Converts the elements of the integer array into polynomials 
    * @param arrayList that contains the polynomials contents 
    */ 
    private void insertIntoPolynomialArray(ArrayList<Integer> arrayList) 
    { 
     //MODIFIED: polynomialarray 
     //EFFECT: inputs the values of the arrayList into the polynomial array based on the position of the digits 
     for(int i = 0; i < arrayList.size(); i++) 
     { 
      Polynomial polynomial = new Polynomial(arrayList.get(i), arrayList.get(i+1)); 
      polynomialarraylist.add(polynomial); 
      System.out.println("coef" + arrayList.get(i)); 
      System.out.println("degree" + arrayList.get(i+1)); 
      i++; 
     } 
    } 


    /** 
    * 
    */ 
    @Override 
    public String toString() 
    { 
     String result = ""; 
     sort(); 
     if (getDegree(0) == 0) 
      return "" + getCoefficient(0); 
     if (getDegree(0) == 1) 
      return getCoefficient(0) + "x + " + getCoefficient(0); 
     result = getCoefficient(0) + "x^" + getDegree(0); 

     for (int j = 1; j < polynomialarraylist.size(); j++) 
     { 
      if(j > polynomialarraylist.size()) 
      { 
       break; 
      } 
      if  
      (getCoefficient(j) == 0) continue; 
      else if 
      (getCoefficient(j) > 0) result = result+ " + " + (getCoefficient(j)); 
      else if 
      (getCoefficient(j) < 0) result = result+ " - " + (-getCoefficient(j)); 
      if(getDegree(j) == 1) result = result + "x"; 
      else if (getDegree(j) > 1) result = result + "x^" + getDegree(j); 
     } 
     return result; 


    } 

    /** 
    * Sorts array 
    * @param array to sort 
    */ 
    private void sort() 
    { 
     ArrayList<Polynomial> temp = polynomialarraylist; 
     ArrayList<Polynomial> temp2 = new ArrayList<Polynomial>(); 
     int polydegreemain = polynomialarraylist.get(0).degree(); 
     temp2.add(polynomialarraylist.get(0)); 
     for(int i = 1; i < polynomialarraylist.size(); i++) 
     { 
      if(i > polynomialarraylist.size()) 
      { 
       break; 
      } 
      int polydegreesecondary = polynomialarraylist.get(i).degree(); 

      if(polydegreemain < polydegreesecondary) 
      { 
       temp.set(i-1, polynomialarraylist.get(i)); 
       temp.set(i, temp2.get(0)); 
      } 

     } 

     polynomialarraylist = temp; 
    } 
    /** 
    * Makes object hashable 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime 
       * result 
       + ((polynomialarraylist == null) ? 0 : polynomialarraylist 
         .hashCode()); 
     return result; 
    } 

    /** 
    * Checks for equality of two objects 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     SparsePolynomial other = (SparsePolynomial) obj; 
     if (polynomialarraylist == null) { 
      if (other.polynomialarraylist != null) 
       return false; 
     } else if (!polynomialarraylist.equals(other.polynomialarraylist)) 
      return false; 
     return true; 
    } 

    public boolean equals(SparsePolynomial Sparse) 
    { 
     if(this == Sparse) 
     { 
     return true; 
     } 
     else 
     { 
     return false; 
     } 
    } 

    public SparsePolynomial add(SparsePolynomial other) 
    { 

     ArrayList<Polynomial> thisPolynomial = createPolynomial(); 
     SparsePolynomial newSparsePolynomial; 
     ArrayList<Polynomial> otherPolynomial = other.createPolynomial(); 
     Polynomial oldsum = new Polynomial(); 
     Polynomial newsum = new Polynomial(); 
     for(int i = 0; i < thisPolynomial.size();i++) 
     { 
      if(thisPolynomial.size() == 1) 
      { 
       newsum = thisPolynomial.get(i); 
       oldsum = newsum; 
       break; 
      } 
      if(i == 0) 
      { 
      newsum = thisPolynomial.get(i).add(thisPolynomial.get(i+1)); 
      oldsum = newsum; 
      i++; 
      } 
      else 
      { 
       newsum = oldsum.add(thisPolynomial.get(i)); 
       oldsum = newsum; 
      } 
     } 
     for(int i = 0; i < otherPolynomial.size(); i++) 
     {  
      newsum = oldsum.add(otherPolynomial.get(i)); 
      oldsum = newsum; 
     } 

     ArrayList<Integer> ints = new ArrayList<Integer>(); 

     for(int i = 0; i < oldsum.degree()+1; i++) 
     { 
      ints.add(oldsum.coefficient(i)); 
      ints.add(i); 
     } 
     newSparsePolynomial = new SparsePolynomial(ints); 

     return newSparsePolynomial; 
    } 

    public SparsePolynomial subtract(SparsePolynomial other) 
    { 
     ArrayList<Polynomial> thisPolynomial = createPolynomial(); 

     ArrayList<Polynomial> otherPolynomial = other.createPolynomial(); 

     Polynomial olddifference = new Polynomial(); 
     Polynomial newdifference = new Polynomial(); 
     for(int i = 0; i < thisPolynomial.size()+1;i++) 
     { 
      if(i == 0) 
      { 
       newdifference = thisPolynomial.get(i).subtract(thisPolynomial.get(i+1)); 
       olddifference = newdifference; 
       i++; 
      } 
      else 
      { 
       newdifference = olddifference.subtract(thisPolynomial.get(i)); 
       olddifference = newdifference; 
      } 
     } 
     for(int i = 0; i < otherPolynomial.size(); i++) 
     { 

      newdifference = olddifference.add(otherPolynomial.get(i)); 
      olddifference = newdifference; 

     } 

     ArrayList<Polynomial> polyarray = createArrayListOfPolynomialsFromPolynomials(olddifference); 

     ArrayList<Integer> ints = new ArrayList<Integer>(); 

     for(int i = 0; i < polyarray.size(); i++) 
     { 
     ints.add(polyarray.get(i).coefficient(polyarray.get(i).degree())); 
     ints.add(polyarray.get(i).degree()); 
     } 

     SparsePolynomial newSparsePolynomial = new SparsePolynomial(ints); 

     return newSparsePolynomial; 
    } 


    private int getDegree(int index) 
    { 
     int degree; 

     degree = polynomialarraylist.get(index).degree(); 

     return degree; 
    } 

    private int getCoefficient(int index) 
    { 
     int coefficient; 

     coefficient = polynomialarraylist.get(index).coefficient(polynomialarraylist.get(index).degree()); 

     return coefficient; 
    } 

    private ArrayList<Polynomial> createPolynomial() 
    { 
     Polynomial polynomial = null; 
     ArrayList<Polynomial> polynomialArray = new ArrayList<Polynomial>(); 
     for(int i = 0; i < polynomialarraylist.size(); i++) 
     { 
      polynomial = new Polynomial(getCoefficient(i), getDegree(i)); 
      polynomialArray.add(polynomial);   
     } 
     return polynomialArray; 
    } 
Polynomial class 

public class Polynomial { 
    // Overview: ... 
    private int[] terms; 
    private int degree; 

    // Constructors 
    public Polynomial() { 
     // Effects: Initializes this to be the zero polynomial 
     terms = new int[1]; 
     degree = 0; 
    } 

    public Polynomial(int constant, int power) { 
     // Effects: if n < 0 throws IllegalArgumentException else 
     // initializes this to be the polynomial c*x^n 
     if(power < 0){ 
      throw new IllegalArgumentException("Polynomial(int, int) constructor"); 
     } 

     if(constant == 0) { 
      terms = new int[1]; 
      degree = 0; 
      return; 
     } 

     terms = new int[power+1]; 

     for(int i=0; i<power; i++) { 
      terms[i] = 0; 
     } 

     terms[power] = constant; 
     degree = power; 
    } 

    private Polynomial(int power) { 
     terms = new int[power+1]; 
     degree = power; 
    } 

    // Methods 
    public int degree() { 
     // Effects: Returns the degree of this, i.e., the largest exponent 
     // with a non-zero coefficient. Returns 0 is this is the zero polynomial 
     return degree; 
    } 

    public int coefficient(int degree) { 
     // Effects: Returns the coefficient of the term of this whose exponent is degree 
     if(degree < 0 || degree > this.degree) { 
      return 0; 
     } 
     else { 
      return terms[degree]; 
     } 
    } 

    public Polynomial subtract(Polynomial other) throws NullPointerException { 
     // Effects: if other is null throws a NullPointerException else 
     // returns the Polynomial this - other 
     return add(other.minus()); 
    } 

    public Polynomial minus() { 
     // Effects: Returns the polynomial - this 
     Polynomial result = new Polynomial(degree); 
     for(int i=0; i<=degree; i++) { 
      result.terms[i] = -this.terms[i]; 
     } 
     return result; 
    } 

    public Polynomial add(Polynomial other) throws NullPointerException { 
     // Effects: If other is null throws NullPointerException else 
     // returns the Polynomial this + other 
     Polynomial larger, smaller; 
     if (degree > other.degree){ 
      larger = this; 
      smaller = other; 
     } 
     else { 
      larger = other; 
      smaller = this; 
     } 

     int newDegree = larger.degree; 

     if (degree == other.degree) { 
      for(int k = degree; k > 0 ; k--) { 
       if (this.terms[k] + other.terms[k] != 0) { 
        break; 
       } 
       else { 
        newDegree --; 
       } 
      } 
     } 


     Polynomial newPoly = new Polynomial(newDegree); 
     int i; 
     for (i=0; i <= smaller.degree && i <= newDegree; i++){ 
      newPoly.terms[i] = smaller.terms[i] + larger.terms[i]; 
     } 

     for(int j=i; j <= newDegree; j++) { 
      newPoly.terms[j] = larger.terms[j]; 
     } 

     return newPoly; 
    } 

    public Polynomial multiply(Polynomial other) throws NullPointerException { 
     // Effects: If other is null throws NullPointerException else 
     // returns the Polynomial this * other 
     if ((other.degree == 0 && other.terms[0] == 0) || 
       (this.degree==0 && this.terms[0] == 0)) { 
        return new Polynomial(); 
     } 

     Polynomial newPoly = new Polynomial(degree + other.degree); 

     newPoly.terms[degree + other.degree] = 0; 

     for(int i=0; i<=degree; i++) { 
      for (int j=0; j<= other.degree; j++) { 
       newPoly.terms[i+j] = newPoly.terms[i+j] + this.terms[i] * other.terms[j]; 
      } 
     } 
     return newPoly; 
    } 
+0

다항식이 다소 고밀도로 보입니다. – starblue

답변

1

빠른 눈에서이 두 번 여기 ++ 내가하고있어 문제

for(int i = 0; i < arrayList.size(); i++) 
{ 
    Polynomial polynomial = new Polynomial(arrayList.get(i), arrayList.get(i+1)); 
    polynomialarraylist.add(polynomial); 
    System.out.println("coef" + arrayList.get(i)); 
    System.out.println("degree" + arrayList.get(i+1)); 
    i++; 
} 

처럼 보이는 : 여기

내 코드의 복사본입니다.

또한 코드를 너무 많이 게시했습니다. 아무도 그렇게 많이 읽길 원하지 않습니다. 너는 운이 좋았다. 이것이 문제라고 가정하면, 나는 그걸 보았다. 당신이 갔지하고있어 이후

또한이 arrayindexoutofboundserror을 던질 것이다 (나는 + 1)

+2

는 고의적으로 보이고, 나는 생각을 좋아하지 않는다. i ++ 전에 마지막 두 줄을보세요. 홀수 지수 (x의 지수) –

+1

오, 그래, 잘 모르겠다, 또는 좋은 lol – Falmarri

+0

아니. 새로운 Sparepolynomial에서 최종 결과를 만들 때 말하십시오. 변수 상자에 정답이 있습니다. 새 스파 스 변수에서 커서를 잠시 떼어 놓은 다음 다시 놓으면 응답에 더 추가됩니다. 예 : 2x^7 + 3x^2 + 2x^7 + 3x^2 (2x^7 + 3x^2 -----> 초) 이것은 디버깅을하는 동안 add 메서드가 끝났을 때입니다. 코드를 여러 번 디버깅했으며이 버그의 원인을 찾을 수 없습니다. – Meat

0

생성자은 (는) GET (내가) 당신에게 계수와 I를 얻을 수 있기 때문에 + 1 방식을 설정 arraylist 매개 변수에서 학위를 얻습니다. 왜냐하면 arraylist 내용이 꺼져있는 상태가 아니기 때문입니다.

생성자는 Arraylist를 가져 와서 다항식의 arraylist를 넣어야한다고 가정합니다. 계수로 확률을 사용하고 다항식의 차수를로 나타냅니다.